Forum Discussion
Deleted
Oct 06, 2019Excel Macro, Can Range() refer to a variable/dynamic column?
Hi there, I need a bit help on the Range() in Excel VBA. Result = Sht2.Worksheets(Company.Value2).Range("a:a").Find(Sht1.Worksheets(Company.Value2).Cells(i, FindKey1.Column), LookIn:=xlValue...
Subodh_Tiwari_sktneer
Oct 06, 2019Silver Contributor
Deleted
Just to give you an idea, you may adopt the following approach...
Dim ws As Worksheet
Dim Rng As Range
Set ws = Worksheets("Sheet1")
Set Rng = ws.Range("A10:G20").Find(what:="Time", LookIn:=xlValues, lookat:=xlPart)
If Not Rng Is Nothing Then
Set Rng = ws.Columns(Rng.Column)
MsgBox Rng.Address
End If
Whenever you use Range.Find method, always check if the value being searched was found as showed in the above code with the line If Not Rng Is Nothing Then otherwise if the value being searched is not found, the code will throw an error if you rely on range obtained from the Range.Find method and use it further in your code.
- DeletedOct 06, 2019Hi Subodh,
Thank you for your advice even though it doesn't address my issue. While it is still a very good tip. Will pack it up for late usage.
Cheers.