Run Macro from current cursor location

hgufrin

Board Regular
Joined
Apr 19, 2004
Messages
177
Office Version
  1. 365
Platform
  1. Windows
Hi All,

How can I adapt the code below to start running this procedure from whichever cell I select. The code below works great in column three (just as I asked it to do). The problem is that at times I have multiple columns and would like to run this in any one of my columns.

Oh yeah, please note: I am running this macro from a 'Button' and do not want to have to physically change the code every time I run it.

Any help is much appreciated!



Sub Macro1()
'
' Macro1 Macro
'

x = 1
Do
ActiveSheet.Cells(x, 3).Activate
If ActiveSheet.Cells(x, 3) = "" Then
ActiveCell.FormulaR1C1 = "=+AVERAGE(R[-4]C[-1]:RC[-1])"
Selection.Offset(0, 0) = "Name"
Else
End If
x = x + 5
Loop Until ActiveSheet.Cells(x, 1) = ""



End Sub
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Code:
Sub Macro1()
'
' Macro1 Macro
'
 
Dim i As Integer
i = Selection.Column
 
 
x = 1
Do
If ActiveSheet.Cells(x, i) = "" Then
ActiveCell.FormulaR1C1 = "=+AVERAGE(R[-4]C[-1]:RC[-1])"
Selection.Offset(0, 0) = "Name"
Else
End If
x = x + 5
Loop Until ActiveSheet.Cells(x, 1) = ""
 
 
 
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,587
Messages
6,179,733
Members
452,939
Latest member
WCrawford

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