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
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