Name a Dynamic Range in VBA with varying Rows and Columns

dversloot1

Board Regular
Joined
Apr 3, 2013
Messages
113
Hello,

I'm trying to create a range in vba where the row is equal to a match found in column B and the columns are CV, DF, DP, and DZ.

I've managed to get the Row number by doing the following:

Row = Application.WorksheetFunction.Match(Sheets("Sheet1").Range("A4"), Sheets("Data").Range("B1:B2000"), 0)

I'd like to get a Range based on the row number and the columns

Range = Sheets("Data").Range("CV" & Row),Sheets("Data").Range("DF" & Row), Sheets("Data").Range("DP" & Row), Sheets("Data").Range("DZ" & Row)

This was my poor attempt to get the range. Any suggestions for this?
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Two ways of doing this. I am assuming you are goint to use this range more than once in your code, in which case we don't want to calculate it every time, if it is only once then the first line gives the result.

Code:
Sub test()
    Dim rDyn As Range, lRow As Long
    
    lRow = Application.WorksheetFunction.Match(Sheets("Sheet1").Range("A4"), _
            Sheets("Data").Range("B1:B2000"), 0)
    Set rDyn = Sheets("Data").Range("CV" & lRow & ",DF" & lRow & ",DP" & lRow & ",DZ" & lRow)
    
    'now you can use rDyn to do everything with:
    rDyn.Value = "Yipee it works!"
    rDyn.Font.Bold = True
    MsgBox rDyn.Address
    
    
    'clean up when finished
    Set rDyn = Nothing
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

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