Select method of Range Class failed using ThisWorkbook

634 views Asked by At
 Dim wb As Workbook
    Set wb = Application.Workbooks("Book2.xlsx")
    wb.Activate
    wb.Sheets("Sheet1").Range("A1").Select 

Ps: Some times above code working properly ,most of times throwing exception

Looking at this link Run Time Error '1004': Select method of Range Class failed using ThisWorkbook i did the same as above still failing

2

There are 2 answers

3
PetLahev On

try to fist activate the sheet and then use the select but first of all do you really need the select? It's the most expensive method you can call (still sometimes needed)

If you just need to read/write a value into the cell you can do it this way

Dim wkb as Workbook
Dim wks as Worksheet

wkb = Application.Workbooks("Book2.xlsx")
wks = wb.Sheets("Sheet1")

System.Windows.Forms.MessageBox.Show(wks.Range("A1").Value2)
' or use this code for selecting
' With wks
'   .Activate()  ' not really sure you need it, test it
'   .Range("A1").Select
' End With  
0
Volkan Yurtseven On

if you want to reference a range in a different sheet, you can't use select metod. you should write your code as following

Application.Goto Sheets(1).Range("A1")