VBA for cell division

sburge

New Member
Joined
Apr 8, 2005
Messages
7
I’m trying to come up with a macro that will divide the values in a cell range (of an active cell row), by the value of the active cell.
In other words: A1 = 4 (active cell), A2=8 and A3=16. The result would be A2=2 and A3=4. I would select the first cell in any given row, and then run the macro.

Thanks in advance, Steve
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
I assume you mean column because A2 and A3 are in the same column not the same row.

Code:
Sub test()
On Error Resume Next
Dim c As Range, d As Range, e As Range
Set c = ActiveCell
Set d = c.Offset(1).Resize(c.End(xlDown).Row - 1)
For Each e In d
    If IsNumeric(e) And e <> "" Then e = e / c
Next
End Sub
 
Upvote 0
Code:
Sub CalculateColValue()
    Dim myColIndex As Integer
    Dim myRowIndex As Integer
    Dim rng, cell As Range
    Dim divisor As Integer
    myColIndex = Application.ActiveCell.Column
    myRowIndex = Application.ActiveSheet.Cells(Rows.Count, myColIndex).End(xlUp).Row
    Set rng = Range(Cells(2, myColIndex), Cells(myRowIndex, myColIndex))
    divisor = ActiveCell.Value
    
    For Each cell In rng
        cell.Value = cell.Value / divisor
    Next cell
End Sub
 
Upvote 0
I really screwed that one up. It should read: A1 = 4 (active cell), B1=8 and C1=16. The result would be B1=2 and C1=4.
Sorry...
 
Upvote 0
Code:
Sub test()
ActiveCell.Copy
ActiveCell.Offset(, 1).Resize(, ActiveCell.End(xlToRight).Column - 1).PasteSpecial , xlPasteSpecialOperationDivide
Application.CutCopyMode = False
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,222,682
Messages
6,167,610
Members
452,123
Latest member
tstefanakis

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