![]() |
![]() |
|
|||||||
| Excel Questions All Excel/VBA questions - formulas, macros, pivot tables, general help, etc. Please post to this forum in English only. |
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Board Regular
Join Date: Feb 2002
Location: Chicago, IL USA
Posts: 306
|
I use the below code to divide selected cells across 12 visible columns. It works great, except that it puts values in and people viewing the worksheet like to see the formulas. Is there anyway for a formula to show in each of the 12 cells.
Thanks Sub DivideItUp() Dim c%, area As Range, colLetter$, avg#, Cell As Range, rng As Range, col%, x%, dest As Range c = 4 'Column number in which cells are to be selected (change as required) For Each area In Selection.Areas If area.Columns.Count > 1 Or area.Column <> c Then With ActiveSheet.Columns(c) colLetter = Left(.Address(False, False), InStr(.Address(False, False), ":") - 1) End With MsgBox "You must make a selection only in Column " & colLetter & " before running this macro." Exit Sub End If Next Set rng = Intersect(ActiveSheet.UsedRange, Selection) Application.ScreenUpdating = False For Each Cell In rng avg = Application.Sum(Cell) / 12 col = c + 1 x = 0 Do Set dest = Cells(Cell.Row, col) If dest.EntireColumn.Hidden = False Then dest.Value = avg col = col + 1 x = x + 1 Else: col = col + 1 End If Loop While x < 12 dest.Offset(0, 1).Formula = "=sum(" & Cells(Cell.Row, c + 1).Address & ":" & dest.Address & ")" Next End Sub |
|
|
|
|
|
#2 |
|
MrExcel MVP
Join Date: Feb 2002
Location: Monterrey, Mexico
Posts: 1,433
|
ActiveWindow.DisplayFormulas = True
Will turn on Formulas for the worksheet.
__________________
Kind regards, Al Chara |
|
|
|
|
|
#3 |
|
Board Regular
Join Date: Feb 2002
Location: Chicago, IL USA
Posts: 306
|
I'm sorry, I didn't explain very well. This code will turn everything into a formula and that will be the value in the cell. What I actually want is to have the result of the formula in the cell, but when the user clicks on a given cell, they can see the formula in the formula bar.
Is this possible? |
|
|
|
|
|
#4 |
|
MrExcel MVP
Join Date: Feb 2002
Location: Monterrey, Mexico
Posts: 1,433
|
What formula do you want displayed in the cells...Application.Sum(Cell) / 12 ?
|
|
|
|
|
|
#5 |
|
MrExcel MVP
Join Date: Feb 2002
Location: Monterrey, Mexico
Posts: 1,433
|
If it is, then try the following:
Dim c%, area As Range, colLetter$, avg#, Cell As Range, rng As Range, col%, x%, dest As Range c = 4 'Column number in which cells are to be selected (change as required) For Each area In Selection.Areas If area.Columns.Count > 1 Or area.Column <> c Then With ActiveSheet.Columns(c) colLetter = Left(.Address(False, False), InStr(.Address(False, False), ":") - 1) End With MsgBox "You must make a selection only in Column " & colLetter & " before running this macro." Exit Sub End If Next Set rng = Intersect(ActiveSheet.UsedRange, Selection) Application.ScreenUpdating = False For Each Cell In rng col = c + 1 x = 0 Do Set dest = Cells(Cell.Row, col) If dest.EntireColumn.Hidden = False Then dest.Formula = "=sum(" & Cell & ")/12" col = col + 1 x = x + 1 Else: col = col + 1 End If Loop While x < 12 dest.Offset(0, 1).Formula = "=sum(" & Cells(Cell.Row, c + 1).Address & ":" & dest.Address & ")" Next
__________________
Kind regards, Al Chara |
|
|
|
|
|
#6 |
|
Board Regular
Join Date: Feb 2002
Location: Chicago, IL USA
Posts: 306
|
Thanks Al!
|
|
|
|
![]() |
| Bookmarks |
| Thread Tools | |
| Display Modes | |
|
|