rjwebgraphix
Well-known Member
- Joined
- May 25, 2010
- Messages
- 590
This is just for learning sake, so no need to rush an answer.
I was working on some code to help someone else out and ran into a snag when working between two workbooks.
This is a shortened version to ensure I covered relevant variables and declarations....
I'd like to focus on 2 lines of this.
I thought that since the workbook and worksheet were already set as objects, I should just be able to write it like this...
but that didn't work, it produces an error and drove me batty for a bit.
I ended up just settling for writing them with....
Workbooks(wbCodeBook.Name) and Sheets(wsCodeBook.Name)
... and that works, but I thought the purpose of setting the object in the first place was to be able to reference it by its variable name.
Any ideas why that didn't work and what would be the proper way to reference these objects?
Thanks.
I was working on some code to help someone else out and ran into a snag when working between two workbooks.
This is a shortened version to ensure I covered relevant variables and declarations....
Code:
Sub whynotwork()
'This workbook
Dim wbCodeBook As Workbook
Set wbCodeBook = ThisWorkbook
'Set worksheet
Dim wsCodeBook As Worksheet
Set wsCodeBook = Sheets("Sheet1")
'set last row of worksheet
Dim lrCodeBook As Long
lrCodeBook = Workbooks(wbCodeBook.Name).Sheets(wsCodeBook.Name).Cells.Find("*", SearchOrder:=xlByRows, LookIn:=xlValues, SearchDirection:=xlPrevious).Row
'lrCodeBook = wbCodeBook.wsCodeBook.Cells.Find("*", SearchOrder:=xlByRows, LookIn:=xlValues, SearchDirection:=xlPrevious).Row
'open file
Dim wbDataBook As Workbook
Set wbDataBook = Workbooks.Open(Filename:="C:\temp\file1.xls", UpdateLinks:=0)
'last row and column of file
Dim lrDataBook As Long
Dim lcDatabook As Long
lrDataBook = Cells.Find("*", SearchOrder:=xlByRows, LookIn:=xlValues, SearchDirection:=xlPrevious).Row
lcDatabook = Cells.Find("*", SearchOrder:=xlByColumns, LookIn:=xlValues, SearchDirection:=xlPrevious).Column
'set range to copy
Dim c1 As Range, c2 As Range, rng As Range
Set c1 = wbDataBook.ActiveSheet.Cells(2, "A")
Set c2 = wbDataBook.ActiveSheet.Cells(lrDataBook, lcDatabook)
Set rng = wbDataBook.ActiveSheet.Range(c1, c2)
rng.Copy Destination:=Workbooks(wbCodeBook.Name).Sheets(wsCodeBook.Name).Range("A" & lrCodeBook + 1)
'rng.Copy Destination:=wbCodeBook.wsCodeBook.Range("A" & lrCodeBook + 1)
End Sub
Code:
'This one
lrCodeBook = Workbooks(wbCodeBook.Name).Sheets(wsCodeBook.Name).Cells.Find("*", SearchOrder:=xlByRows, LookIn:=xlValues, SearchDirection:=xlPrevious).Row
'and this one
rng.Copy Destination:=Workbooks(wbCodeBook.Name).Sheets(wsCodeBook.Name).Range("A" & lrCodeBook + 1)
I thought that since the workbook and worksheet were already set as objects, I should just be able to write it like this...
Code:
'first line
lrCodeBook = wbCodeBook.wsCodeBook.Cells.Find("*", SearchOrder:=xlByRows, LookIn:=xlValues, SearchDirection:=xlPrevious).Row
'second line
rng.Copy Destination:=wbCodeBook.wsCodeBook.Range("A" & lrCodeBook + 1)
but that didn't work, it produces an error and drove me batty for a bit.
I ended up just settling for writing them with....
Workbooks(wbCodeBook.Name) and Sheets(wsCodeBook.Name)
... and that works, but I thought the purpose of setting the object in the first place was to be able to reference it by its variable name.
Any ideas why that didn't work and what would be the proper way to reference these objects?
Thanks.