Use match to define range

Emperor

Board Regular
Joined
Mar 25, 2010
Messages
225
Hi all,

I'm trying to create a dynamic range but am failing somehow.

Code:
Worksheets("data").Activate
Dim Colmn
Colmn = Application.Match("Eindtotaal", Range("2:2"), False)
 
Dim lastr As Range
Set lastr = ActiveSheet.Cells(2, Colmn)
 
Dim KGrng
KGrng = Worksheets("data").Range("G2:" & lastr)

So what I need is to look in row 2 for "Eindtotaal", and then create a range from G2 to the cell where "Eindtotaal" is found.

What am I doing wrong?

Mathijs.
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Try removing Set from:

Rich (BB code):
Set lastr = ActiveSheet.Cells(2, Colmn)

The Set keyword is only used for assigning an object to a variable. You need it in this line:

Rich (BB code):
Set KGrng = Worksheets("data").Range("G2:" & lastr)
 
Upvote 0
Hi Mathijs

If you are looking for that exact string then I like to use the Find method. See example below:

Code:
Sub Demo()
    Dim KGrng As Range
    
    With Sheets("data")
        On Error Resume Next
            Set KGrng = .Range(.Range("G2"), .Rows(2).Find(What:="Eindtotaal", LookAt:=xlWhole, MatchCase:=True))
        On Error GoTo 0
        If KGrng Is Nothing Then
            MsgBox "Eindtotaal not found in row 2", vbExclamation
        Else
            MsgBox "Eindtotaal: " & KGrng.Address
        End If
    End With
End Sub
 
Upvote 0
Andrew and Jon, thanks for the quick replies!

Andrew;
When I remove the set from the first part Excel stops saying it needs a 'with', could this be correct? If yes, where? don't get it.

Jon;
When I use the find statement, Excel states there is now "Eindtotaal" in row 2, well there is...how could this be?
I use this formula to get "Eindtotaal" in row 2;
Code:
=IF(I8="Eindtotaal",I8,IF(I8="","",(I8& " "& VLOOKUP(I8,data2!$D$24:$E$56,2,FALSE))))
(formula is in I2)
When I link to row 8, your code does work, but not in row2, very weird, at least for me...

Gr. Mathijs
 
Upvote 0
What am I doing wrong?

Mathijs.
Did you want to know what's wrong with your own code?
Try it with the indicated modifications
Rich (BB code):
Dim Colmn
Colmn = Application.Match("Eindtotaal", Range("2:2"), False)
 
Dim lastr As Range
Set lastr = ActiveSheet.Cells(2, Colmn)
 
Dim KGrng as Range
Set KGrng = Worksheets("data").Range("G2", lastr)

Although it could usefully do with a bit of tidying up in other ways too.
 
Upvote 0

Forum statistics

Threads
1,224,599
Messages
6,179,828
Members
452,946
Latest member
JoseDavid

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