Vba refer to range in different workbook with “Cells()”

rangevba

i want to refer to a range in a different book, that is defined from
this works:

Workbooks(macrofilename).Sheets(IDsheetname).Activate
lookRange = Range(Cells(startrow, startcol), Cells(rows, cols))
colno = Application.Match(myName, lookRange, 0)

but i don't want to activate the other sheet so i tried it to add the workbook&sheet to the range definition:

lookRange = Workbooks(filename).Sheets(sheetname).Range(Cells(startrow, startcol), Sheets(Cells(rows, cols))

sadly, this results in in a 1004 error (application-defined or object-defined error).

any ideas how to solve this?

thanx, Gijs.

Best Answer

You can try first make reference to worksheet:

Dim Ws As Worksheet
Set Ws =  Workbooks(filename).Sheets(sheetname)

And then make reference to range:

With Ws
    Set rLookRange = Range(.Cells(startrow, startcol), .Cells(rows, cols))
End With

Pay attention on dots: you refer to Cells in specific Worksheet, so paste dots before .Cells in your statement.

Related Topic