Dynamic Macro Range

rhmkrmi

Active Member
Joined
Aug 17, 2012
Messages
348
Office Version
  1. 365
Platform
  1. Windows
Hello,
How can I make a range in a macro dynamic? For example, in the macro below, how can I make it pick upnew rows, like P15, added to the range P1:P14?

Private Sub Worksheet_Change(ByVal Target As Range)

Dim changed As Range,c As Range
Dim cVal
Const myR As String ="P1:P14" '<- Your range(s)

Set changed =Intersect(Target, Range(myR))

If Not changed IsNothing Then
Application.EnableEvents = False
For Each c In changed
cVal = c.Value
Select Case True
Case IsEmpty(cVal),IsNumeric(cVal), _
IsDate(cVal),IsError(cVal)
' Do nothing
Case Else
c.Value = UCase(cVal)
End Select
Next c
Application.EnableEvents = True
End If

End Sub
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Hi,
If you want your code to apply to all rows in Column P then change this line

Code:
 Set changed =Intersect(Target, Range(myR))


to this

Code:
Set changed = Intersect(Target, Me.Columns(16))

and see if this does what you want

Dave
 
Upvote 0
Thanks Dave.

I actually want the code below to start in P1but go over P14 f users insert rows.

Const myR As String ="P1:P14" '<- Your range(s)
 
Upvote 0
Maybe one way...
Code:
Const myR As String =Range("P1:P" & Range("P" & Rows.count).End(xlUp).Row).Address
 
Upvote 0
Thanks Dave.

I actually want the code below to start in P1but go over P14 f users insert rows.

Const myR As String ="P1:P14" '<- Your range(s)

If you want a variable range then need to change from Const (fixed) statement to a variable

Hope Mark858 does not mind but I have amended his suggestion (where I suspect he posted in haste) which may be what you are trying to achieve.


Code:
Dim myR As String
myR = Range("P1:P" & Range("P" & Rows.Count).End(xlUp).Row).Address

Dave
 
Upvote 0
Hope Mark858 does not mind but I have amended his suggestion (where I suspect he posted in haste) which may be what you are trying to achieve.

Dave

No problem, it was just as I was leaving the house :rofl:
 
Upvote 0
Thank you MARK858 and dmt32 but it is getting applied to the whole column.
What I need is range P1:P14 and then as they insert rows in this range, P14 expands.
I have other data in P15 onward which should not be upper case.

Thanks again.
 
Upvote 0
Create a named range for P1:P14 then use the named range in the code.
 
Upvote 0
Excellent.
Thank you.
Works perfectly.

Is there a macro that I can use on a button to add rows from a certain row? For example, when we click on the button, it always inserts a row after row 13?

Thanks
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,175
Members
453,021
Latest member
Justyna P

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