copy entire data from one workbook to another using vba - vba

I'm getting a "object variable or with block variable not set" error message when running the following code. What is wrong with the code?
Dim directory As String, fileName As String, sheet As Worksheet, i As Integer, j As Integer
Dim mainworkBook As Workbook
Application.ScreenUpdating = False
directory = "C:\Users\425410\Desktop\MYExcel\"
fileName = Dir(directory & "*.xl??")
Dim x As Workbook, y As Workbook
Dim ws1 As Worksheet, ws2 As Worksheet
Set x = Workbooks.Open(directory & fileName)
Windows("Book3.xlsm").Activate
Set ws1 = x.Sheets(1)
Set ws2 = y.Sheets(1)
With ws1
.Cells.Copy ws2.Cells
y.Close True
x.Close False
End With

As #user3514930 commented you need to set y to a workbook object.
Added some extra code edits.
'<<I think grouping similar types together when declaring variables is clearer than OP
'<<Also I prefer all declarations at the start of subroutines
Dim directory As String, fileName As String
Dim sheet As Worksheet
Dim i As Integer, j As Integer
Dim mainworkBook As Workbook
Dim x As Workbook, y As Workbook
Dim ws1 As Worksheet, ws2 As Worksheet
Application.ScreenUpdating = False
directory = "C:\Users\425410\Desktop\MYExcel\"
fileName = Dir(directory & "*.xl??")
Set x = Workbooks.Open(directory & fileName)
Windows("Book3.xlsm").Activate
Set y = Workbooks("someopenBook") '<<<<<<<<<<<<<
Set ws1 = x.Sheets(1)
Set ws2 = y.Sheets(1)
'<<not sure why you have the workbook close lines within the With block
With ws1
.Cells.Copy ws2.Cells
End With
y.Close True
x.Close False

Related

How to switch between workbooks

I have problem with switching between workbooks. Could you take a look at code and say what is wrong with attitude to problem which i am trying? Or propose some other way to do this.
Sub copy_spreadsheets()
Dim wb As Workbook, wb_main As Workbook, path As String, sheet_name As String, x As Integer, i As Integer, source_sheet As Worksheet, ws As Worksheet
path = "C:\Users\me\Desktop\folder\"
Set wb_main = ThisWorkbook
For x = 2 To 10
i = 2
sheet_name = ("sheet" & i & ".xlsx")
Set wb = Workbooks.Open(path & sheet_name)
Set sourceSheet = Worksheets("sheet1")
sourceSheet.Activate
sourceSheet.Cells.Select
Selection.Copy
Workbooks("C:\Users\me\Desktop\folder\sheet1.xls").Worksheets("Sheet1").Activate
Set ws = Sheets.Add
i = i + 1
Next x
End Sub
There is a Workbook.Activate method as well as Worksheet. Try activating the workbook before activating sheets within that workbook.

copying range end xl down pasting different Wb

I've got a macro recorder code (with select and activate) that I'm trying to simplify. It currently looks like this:
Windows("Stambestand.xlsm").Activate
Range("AA2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Windows("Ijking document.xlsm").Activate
Range("B3").Select
ActiveSheet.Paste
As of recently I've started using variables and want to trim down the code. I'm thinking along these lines:
WbStambestand.WsStam.Range("AA2", Selection, Selection.End(xlDown)).Copy WbIjk.WsIjk.range("A1").paste
Workbooks("WbStambestand").Worksheets("WsStam").Range("AA2", Selection, Selection.End(xlDown)).Copy
However, these don't function. I'm hoping you guys can help me along. Much appreciated.
FYI, these are my variables (they are declared). The Ijk ones are the paste destination.
Dim WbStambestand, WbIjk As Workbook
Dim WsIjk, WsStam As Worksheet
Set WsIjk = ActiveSheet
Set WbIjk = ActiveWorkbook
Set WsIjk = ActiveSheet
Set WbIjk = ActiveWorkbook
Set WbStambestand = Workbooks.Open(stam)
Set WsStam = WbStambestand.Worksheets("stambestand")
Try this
Sub abc()
Dim WbStambestand As Workbook
Dim WbIjk As Workbook
Dim WsIjk As Worksheet
Dim WsStam As Worksheet
Dim LastRow As Long
Dim stam As String
stam = "C:\Users\Admin\Desktop\Stambestand.xlsm" ' path with complete file name with extension.
Set WsIjk = ActiveSheet
Set WbIjk = ThisWorkbook ' workbook which has current code
Set WbStambestand = Workbooks.Open(stam)
Set WsStam = WbStambestand.Worksheets("stambestand")
LastRow = WsStam.Range("AA2").End(xlDown).Row
WsStam.Range("AA2:AA" & LastRow).Copy WsIjk.Range("B3")
End Sub

Looping through all files in a folder (encountering run time error '-2147221080 (800401a8)')

I am running a code that aims to pull in data from all workbooks within a specific folder. I have written the code for the loop separate from the code for the pulling in of data.
Sub FixedIncomeLoop()
Dim file As Variant
Dim wb1 As Workbook
Dim wb2 As Workbook
Dim folderpath As String
Dim ws As Worksheet
Dim ws2 As Worksheet
Set wb1 = ActiveWorkbook
Set ws = ActiveSheet
folderpath = "C:\Users\Wei Hong\Documents\Trade Tickets\"
file = Dir(folderpath)
While (file <> "")
Set wb2 = Workbooks.Open(file)
Set ws2 = wb2.Worksheets("Trade Ticket")
Call FixedIncome(wb1, ws, ws2)
wb2.Close SaveChanges:=False
Set wb2 = Nothing
Wend
End Sub
Private Sub FixedIncome(wb1 As Workbook, ws As Worksheet, ws2 As Worksheet)
Dim ws3 As Worksheet
Set ws3 = wb1.Worksheets("Constants") <-- facing error
ws.Activate
...
End Sub
I am facing the runtime error above. Not sure why!

Copy specific entire column from file 1 to 2

Hello I'm trying to copy columns C, R, W,X from file 1 to file 2 with below code but keep getting an error. My VBA knowledge isn't that good yet but probably has to do with the range setting? I've tried multiple ways but can't get it to work.
Am I using the right setting or should I use another action to get the specific columns?
Sub PFS()
Dim wbCopy As Workbook
Dim wsCopy As Worksheet
Dim rngCopy As Range
Dim wbPaste As Workbook
Dim wsPaste As Worksheet
Dim rngPaste As Range
Set wbPaste = ActiveWorkbook
Set wbCopy = Workbooks.Open("path to copy")
Set wsCopy = wbCopy.Worksheets("Blad1")
Set rngCopy = wsCopy.Range("d, e").EntireColumn
Set wsPaste = wbPaste.Worksheets("PFS")
Set rngPaste = wsPaste.Range("a1")
rngCopy.Copy
rngPaste.PasteSpecial
Workbooks.Application.CutCopyMode = False
Application.DisplayAlerts = False
wbCopy.Save
wbCopy.Close
End Sub
Solutions to copy entire column.
Sub copy()
Dim wb As Workbook
Dim wbNew As Workbook
Dim ws As Worksheet
Dim wsNew As Worksheet
Set wb = ActiveWorkbook
Set ws = wb.Sheets("old")
Set wbNew = Workbooks("Book.xlsx")
Set wsNew = wbNew.Sheets("new")
ws.Columns(3).copy
wsNew.Columns(3).Insert Shift:=xlToRight
ws.Columns(18).copy
wsNew.Columns(18).Insert Shift:=xlToRight
ws.Columns(23).copy
wsNew.Columns(23).Insert Shift:=xlToRight
ws.Columns(24).copy
wsNew.Columns(24).Insert Shift:=xlToRight
Set wsNew = Nothing
Set wbNew = Nothing
Set ws = Nothing
Set wb = Nothing
End Sub

This Piece of code still does not update excel [duplicate]

This question already has answers here:
Subscript out of range error in this Excel VBA script
(3 answers)
Closed 9 years ago.
This code still gives me an out of subscript error
Sub importData2()
ChDir "C:\Users\Desktop\Java"
Dim filenum(0 To 10) As Long
filenum(0) = 052
filenum(1) = 060
filenum(2) = 064
filenum(3) = 068
filenum(4) = 070
filenum(5) = 072
filenum(6) = 074
filenum(7) = 076
filenum(8) = 178
filenum(9) = 180
filenum(10) = 182
Dim sh1 As Worksheet
Dim rng As Range
Set rng = Range(Selection, ActiveCell.SpecialCells(xlLastCell))
Dim wb As Workbook
Set wb = Application.Workbooks("30_graphs_w_Macro.xlsm")
Dim sh2 As Worksheet
Dim rng2 As Range
Set rng2 = Range("A69")
Dim wb2 As Workbook
For lngposition = LBound(filenum) To UBound(filenum)
Set wb2 = Application.Workbooks.Open(filenum(lngposition) & ".csv")
wb2.Worksheets(filenum(lngposition)).rng.Copy wb.Worksheets(filenum(lngposition)).rng2.Paste
Next lngposition
my_handler:
MsgBox "All done."
End Sub
This still gives me an out of subscript error on the line:
Set wb2 = Application.Workbooks(filenum(lngposition) & ".csv")
I avoided using .active and .select. .select.
Subscript out of Range would raise on that line if the required file is not already open.
Since it seems unlikely that you would already have 11 files open, you probably need to use the Open method to open the necessary workbook inside your loop.
Set wb2 = Application.Workbooks.Open(filenum(lngposition) & ".csv").
Updated your code
Sub importData2()
ChDir "C:\Users\Desktop\Java"
Dim filenum(0 To 10) As String
Dim wb As Workbook
Dim sh1 As Worksheet
Dim rng As Range
Dim wb2 As Workbook
Dim sh2 As Worksheet
Dim rng2 As Range
filenum(0) = "052"
filenum(1) = "060"
filenum(2) = "064"
filenum(3) = "068"
filenum(4) = "070"
filenum(5) = "072"
filenum(6) = "074"
filenum(7) = "076"
filenum(8) = "178"
filenum(9) = "180"
filenum(10) = "182"
'## What workbook is this referring to?? This might cause problems later...
Set rng = Range(Selection, ActiveCell.SpecialCells(xlLastCell))
Set rng2 = Range("A69")
Set wb = Application.Workbooks("30_graphs_w_Macro.xlsm")
For lngposition = LBound(filenum) To UBound(filenum)
Set wb2 = Application.Workbooks.Open(filenum(lngposition) & ".csv")
Set sh1 = wb.Worksheets(filenum(lngposition))
Set sh2 = wb2.Worksheets(1) 'A CSV file only has 1 worksheet.
sh2.rng.Copy Destination:=sh1.Range(rng2.Address)
Next lngposition
my_handler:
MsgBox "All done."
End Sub
You should definitely have Set on the line when you assign worksheets:
Set sh1 = Worksheets(filenum(lngPosition))