Using macro to divide across cells

Adrae

Active Member
Joined
Feb 19, 2002
Messages
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
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
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?
 
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,213,487
Messages
6,113,941
Members
448,534
Latest member
benefuexx

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top