I have the following definition in the beginning of my macro
'Define This Workbook Dim wbk As Workbook Set wbk = ThisWorkbook 'Define Main Sheet Dim wks As Worksheet Set wks = Sheet2
I don’t understand why the following line of code doesn’t work. I get the error message “object doesnt support this property or method”.
lastRow = wbk.wks.Range("B:B").Find("Timmar", [B1], , , xlByRows, xlPrevious).row
So instead I have to write the following lines of codes. They work:
wbk.Activate wks.Activate lastRow = Range("B:B").Find("Timmar", [B1], , , xlByRows, xlPrevious).row
Do you understand why my original approach does not work?
As I mentioned in comment above, change
Workbook object and it indeed doesn’t support property with name
wks. Check it here.
If you want to specify that
wks belongs to
wbk, use something like this:
Set wks = wbk.Worksheets("Sheet2")
Also if range
wks.Range("B:B") doesn’t contain cells with
Timmer value – you’d get runtime error, since you’re trying to get
.Row property of unfound cell (when result of
Nothing). Use this one instead:
Dim rng As Range Dim lastRow As Long Set rng = wks.Range("B:B").Find("Timmar", wks.[B1], , , xlByRows, xlPrevious) If Not rng Is Nothing Then lastRow = rng.Row Else lastRow = 1 ' change to suit End If