I would like to somehow declare dynamic workbook variables so that I can change workbook file names without causing errors. My original thought was to use Set Variable = ActiveWorkbook
, but that appears to give some peculiar errors:
Set WkBk1 = ActiveWorkbook
MsgBox (WkBk1.Name)
WkBk1.Worksheets(1).Select
The above code works fine by itself. Afterwards, in the same procedure, I open a new workbook and run the following:
Set WkBk2 = ActiveWorkbook
MsgBox (WkBk2.Name)
WkBk2.Worksheets(1).Select
WkBk1.Worksheets(1).Select
I get an error at the last line, "Run-time error 1004; select method of worksheet class failed". Can any one explain the source of this issue? I'm guessing that it has something to do with using ActiveWorkbook to declare variables, or a faulty method of going between workbooks.
I should mention that the opened file is a .dat file opened in excel.
Ultimately, I want to have code that will assign two worksheets, of whatever file name, to their own respective variables. I then want to copy and paste data between the two workbooks.
The actual code is below. The problem is with the copy statement in the for loop.
"Run-time error '438' object doesnt support this property or method". Ive used similar syntax before and can't figure out why it isn't working here.
Sub Import3()
' Imports TSS samples from backlog
Dim TSS As Workbook, Backlog As Workbook
Dim Sample As Range, SearchRange As Range
Dim Count As Long
Set TSS = ActiveWorkbook
ChDir ("C:\lwuser6")
Workbooks.OpenText Filename:="C:\lwuser6\BACKLOG.DAT", Origin:=437, _
StartRow:=1, DataType:=xlFixedWidth, FieldInfo:=Array(Array(0, 1), Array(7, _
1), Array(68, 1), Array(78, 1), Array(86, 1), Array(126, 1), Array(150, 1)), _
TrailingMinusNumbers:=True
' for some reason _ has to be in between "Array(7, _1)"
' Can't be between list elements of encompassing list "Array"
Set Backlog = ActiveWorkbook
Set SearchRange = Backlog.Worksheets("BACKLOG").Range(Cells(1, 1), _
Cells(Rows.End(xlDown).Row, 1))
Count = 14
For Each Sample In SearchRange
Backlog.Range(Sample(1, 1), Sample(1, 3)).Copy
TSS.Range("G" & Count).PasteSpecial (xlValues)
Count = Count + 2
Next
End Sub