Macro to Run on Specific Rows

Papi

Well-known Member
Joined
May 22, 2007
Messages
1,592
The following is a simple way I found on this site to enter dates for various date types. How can I limit the macros to only run on specific rows 6:14?

Code:
Sub DateStamp()
    Range("B" & ActiveCell.Row).Select
    ActiveCell.Value = Date
End Sub
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Code:
Sub DateStamp()
  Dim i As Long
  For i = 6 to 14
    Range("B" & i).Value = Date
  Next i
End Sub
 
Upvote 0
Thanks for such quick responses. What I need it to do is run on one row at a time but only run on either row 6 or 7 etc. Basically I do not want the macro to run on rows 1 to 5 and 15 and higher. Sorry for the confusion in my wording
 
Upvote 0
Thanks for such quick responses. What I need it to do is run on one row at a time but only run on either row 6 or 7 etc. Basically I do not want the macro to run on rows 1 to 5 and 15 and higher. Sorry for the confusion in my wording

Ok, that means that you have to somehow input a row number. You could do something like:
Put "Choose Row" in A1 and then in B1 add the row number.

Your internal macro lines would then be:
x =Range("B1").value
If x<15 and x>5 then
Range("B" & x).value = date
end if

Obviously there is an input box method if you wish that but that may not suit your purposes.
 
Upvote 0
Is this what you are looking for?
Code:
Sub DateStamp()
  With ActiveCell
    If .Row > 5 And .Row < 15 Then Range("B" & .Row).Value = Date
  End With
End Sub
 
Upvote 0
Thanks very much gentlemen. I went with Peters code as it was exactly what I was in need of.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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