I'm trying to use a snippet of code i found here .
Sub foo()
Dim x As Workbook
Dim y As Workbook
Dim wbname As String
Dim lastcol As Long
wbname = Replace(ActiveWorkbook.name, ".xlsx", "")
'## Open both workbooks first:
Set x = Workbooks.Open("N:\blah\deposit\" & wbname & "*.xlsx")
lastcol = Cells(1, Columns.Count).End(xlToLeft).Column
mycol = GetColumnLetter(lastcol)
'Now, copy what you want from x:
x.Sheets("sheet1").Range("mycol:mycol").Copy
x.Close
'Now, paste to y worksheet:
y.Sheets("sheet1").Range("mycol:mycol").PasteSpecial
End Sub
Function GetColumnLetter(colNum As Long) As String
Dim vArr
vArr = Split(Cells(1, colNum).Address(True, False), "$")
GetColumnLetter = vArr(0)
End Function
I can't seem to find how to copy the last column. It says error 9 subscript is out of range.
I tried x.Sheets("sheet1").Columns(lastcol).Copy
x.Sheets("sheet1").Range("mycol").copy
, x.Sheets("sheet1").Range(mycol)
and x.Sheets("sheet1").Range(Columns(mycol)).Copy
Is there something else I was supposed to do or declare? I serously do not understand why this is not working. :(