I am getting an Application or Object Defined Error when I try to import values from one workbook to another. I have been able to resolve it by explicitly activating the workbooks and selecting the sheets before referencing the range on each, but I would like to avoid that if possible. Both workbooks are open at this point in the code. Any thoughts?
This generates errors for me:
Dim wbImport As Workbook
Dim wbReceive As Workbook
Const sExcept = "Sheet2 Name"
Const sSht = "Sheet1 Name"
Dim rExceptions As Range
wbReceive.Sheets(sExcept).Rows(1).Insert shift:=xlDown
Set rExceptions = wbImport.Sheets(sSht).Range(Cells(rCell.Row, iHeadCol), Cells(rCell.Row, iLastCol))
wbReceive.Sheets(sExcept).Range(Cells(1, iHeadCol), Cells(1, iLastCol)).Value = rExceptions.Value 'error occurs here
This runs fine, but I'd like to avoid the .Select
and .Activate
wbReceive.Sheets(sExcept).Rows(1).Insert shift:=xlDown
wbImport.Activate
wbImport.Sheets(sSht).Select
Set rExceptions = wbImport.Sheets(sSht).Range(Cells(rCell.Row, iHeadCol), Cells(rCell.Row, iLastCol))
wbReceive.Activate
wbReceive.Sheets(sExcept).Select
wbReceive.Sheets(sExcept).Range(Cells(1, iHeadCol), Cells(1, iLastCol)).Value = rExceptions.Value
As I was debugging, it looked like cells referenced in the wbReceive.Sheets(sExcept)
line were actually referencing a different sheet in the wbReceive
workbook. Not sure why that would be the case though, since the wb and sheet were explicitly referenced?