Replacement for using Intersect Function and UsedRange to find variable range

spidaman

Board Regular
Joined
Jul 26, 2015
Messages
116
Office Version
  1. 365
Platform
  1. Windows
Can anyone help me with a replacement method for assigning a variable range please?

In the data the length of Row 1 is variable and the location of TargetStr1 is also variable.

At the moment I am using the Intersect function and UsedRange but don't think it's the best way to do it:

VBA Code:
Sub Estab_Range ()

Dim ws1 As Worksheet
Dim lcol As Long
Dim foundCell As Range, titRng As Range, ListRng As Range
Dim TargetStr1 As String

Set ws1 = ActiveWorkbook.ActiveSheet
lcol = ws1.Range("A1").End(xlToRight).Column
Set titRng = ws1.Range(("A1"), Cells(lcol))

TargetStr1 = "List"

    Set foundCell = titRng.Find(what:=TargetStr1, LookIn:=xlValues, _
    lookat:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlPrevious, _
    MatchCase:=True, SearchFormat:=False)

    Set ListRng = Intersect(foundCell.EntireColumn, ws1.UsedRange)

End Sub

Thanks in advance.
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
Nothing wrong with using Intersect, although, depending on your data, you could also use
VBA Code:
   Set ListRng = Range(foundCell, Cells(Rows.Count, foundCell.Column).End(xlUp))
 
Upvote 0
Nothing wrong with using Intersect, although, depending on your data, you could also use
VBA Code:
   Set ListRng = Range(foundCell, Cells(Rows.Count, foundCell.Column).End(xlUp))
Thanks Fluff that's what I was looking for. Would the .end(xlUp) method still work ok if there are empty cells in the column within the ListRng?
 
Upvote 0
It was more UsedRange I was trying to avoid I suppose....
 
Upvote 0
Depends on what exactly you mean.
That will find the last cell in the column that is not blank, but other columns may have data in rows further down the sheet.
 
Upvote 0
Depends on what exactly you mean.
That will find the last cell in the column that is not blank, but other columns may have data in rows further down the sheet.
That's perfect, thanks for your help.
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,214,929
Messages
6,122,317
Members
449,081
Latest member
tanurai

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