VBA copying worksheet to another workbook - excel

I know this question is discussed many times and I have checked almost all sources but I don't know why this simple code does not work. I want to copy a worksheet from one workbook to another.
Sub SimpleCode()
Dim wb1 As Workbook
Dim wb2 As Workbook
Set wb1 = Workbooks("C:\Users\File1.xlsx")
Set wb2 = Workbooks("C:\Users\File2.xlsx")
wb1.Sheets("File1Sheet1").Copy After:=wb2.Sheets("File2Sheet1")
End Sub

Assuming that both files are open you only need to call the name of the workbooks, not the full name:
Sub SimpleCode()
Dim wb1 As Workbook
Dim wb2 As Workbook
Set wb1 = Workbooks("File1.xlsx")
Set wb2 = Workbooks("File2.xlsx")
wb1.Sheets("File1Sheet1").Copy After:=wb2.Sheets("File2Sheet1")
End Sub

Related

VBA Vlookup from a saved file

I have a dynamic report extracted from SAP, that I need to run a Vlookup Formula in it from a closed file on my PC.
so what I basically do is VBA to open the source file that I will vlookup from then run the Vlookup formula in my sap extracted workbook.
My problem is how to let the formula run on the Sheet extracted from SAP, The VBA code is in personal file, not the extracted SAP sheet so I can't use thisworkbook function.
Sub Vllokp()
Dim wb As Workbook
Dim wb1 As Workbook
Workbooks.Open "C:\User\1.Work\18.SAP GUI and Automation\Payment propsal Test\Bank.xlsx"
Set wb = Workbooks("Bank.xlsx")
Set wb1 = ActiveWorkbook.Sheets("sheet1")
With wb1.Sheets("sheet1")
.Range("AA2").Formula = "=VLOOKUP(D1,'[Bank.xlsx]Sheet1'!A:C,3,FALSE)"
End With
End Sub
This worked for me,
Sub Vllokp()
Dim wb1 As Workbook
Dim wb2 As Workbook
Set wb1 = ActiveWorkbook
Workbooks.Open "C:\User\1.Work\18.SAP GUI and Automation\Payment propsal Test\Bank.xlsx"
Set wb = Workbooks("Bank.xlsx")
With wb1.Sheets("sheet1")
.Range("AA2").Formula = "=VLOOKUP(D1,'[Bank.xlsx]Sheet1'!A:C,3,FALSE)"
.Range("AA2").copy Range("AA3:AA" & Cells(Rows.Count, 4).End(xlUp).Row - 1)
End With
End Sub

Can a workbook, opened via VBA from another workbook, in turn open a third workbook via VBA?

Just describing the issue hurt my Monday AM brain. Below is an outline of what the basis of my problem is. For ease of this example, all files are in the same directory.
First workbook ('WB1') has a Sub ('Sub1') that opens another workbook ('WB2')
WB1 does stuff to WB2
WB1 Sub1 calls Save of WB2
Public Sub Sub1()
Dim WB1 As Workbook
Dim WB2 As Workbook
Set WB1 = ThisWorkbook
Set WB2 = Workbooks.Open(WB1.Path & "\WB2.xlsm") '1.
WB2.Sheets(1).Cells(1, 1) = "Hello World" '2.
WB2.Close True '3.
End Sub
WB2's "BeforeSave" Sub is called, which in turn calls a Sub ('Sub2') from a Module within WB2
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
If ThisWorkbook.Saved = False Then
Call Sub2
End If
End Sub
Sub2 calls to open yet another workbook ('WB3') **
Public Sub Sub2()
Dim WB2 As Workbook
Dim WB3 As Workbook
Set WB2 = ThisWorkbook
Set WB3 = Workbooks.Open(WB2.Path & "\WB3.xlsm")
WB3.Sheets(1).Cells(1, 1) = "Hello World" '=== Error, WB3 wasn't opened
WB3.Close True
End Sub
at step 5, I get a failure in that WB3 does not get opened.
In the 'real life' code,
I have confirmed that I can open WB3 from WB2 if Sub2 is called directly from WB2 (i.e. if WB1 is out of the picture), so it's not an error in the path for WB3 or anything like that.
I can also call for the opening of WB3 from WB1, which is my current work around, but in our current system, this workaround would take a bit of doing to correct all the sheets that would be affected by this new add of "workbook.open-ception" -- the opening of WB3 is a new layer added to update an "overview" workbook from individual "log" workbooks.
If this behavior is by design, so be it. But if there's any way to make it work as is, that'd be great.

Copy a range from a closed workbook to a specific sheet

I am currently working on a VBA script to automate a excel sheet. The goal is to have the code open a file from using a file path in cell A2 on a sheet called Reports (the file path is dynamic and is formed using information from the sheet) , copy the data from the file for range A1:E200 and to paste the data into the original workbook on a sheet called HOURS starting at A1. At the moment i have gotten to the point where the file is opened but there is a "Mismatch" error when trying to copy the information across. Below I've attached the code used. I was hoping that someone would be able to help to make sense of the error! I am having the same problem with the close section as well. Note: I am a rookie on VBA so if you could be as clear as possible
Sub Button1_Click()
Call Test
Call Copy_Method
Call CloseWorkbook
End Sub
Sub Test()
Dim strFName As String
strFName = Sheet4.Range("A2").Value
Workbooks.Open Filename:=strFName
End Sub
Sub Copy_Method()
'Copy range to another workbook using Range.Copy Method
Dim wb1 As Workbook
Dim wb2 As Workbook
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Set wb2 = ThisWorkbook
Set ws2 = wb2.Sheets("HOURS")
Set wb1 = ThisWorkbook.Worksheets("Reports").Range("A2")
Set ws1 = wb1.Sheets("Sheet")
ws2.Range("A1:E200") = ws1.Range("A1:E200").Value
End Sub
Sub CloseWorkbook()
Workbooks("venues_theeway_hours_August2020.XLS").Close SaveChanges:=True
End Sub
Have you tried this ?
ws2.Range("A1:E200").Value = ws1.Range("A1:E200").Value
You're making life quite difficult for yourself there, splitting the code out across 3 subs. Better to
rename the references to make them easier to differentiate source/destination.
keep it all together so the workbooks/worksheets can still be referenced as they're created:
Apologies if I've misread your requirements, my code does the following:
Reads the original workbook, sheet "Reports", range A2 for a filename.
Opens that filename as a 'source' workbook
Copies data from..
that 'source' workbook, sheet "Sheet", range A1:E200
..to original workbook, sheet "HOURS", range A1:E200
and then closes the 'source' workbook, unsaved as you've not made any changes.
Dim wbSource As Workbook
Dim wbDest As Workbook
Dim wsSource As Worksheet
Dim wsDest As Worksheet
Dim strFName As String
Set wbDest = ThisWorkbook
Set wsDest = wbDest.Sheets("HOURS")
strFName = wbDest.Worksheets("Reports").Range("A2").Value
Set wbSource = Workbooks.Open(strFName)
Set wsSource = wbSource.Worksheets("Sheet")
wsDest.Range("A1:E200").Value = wsSource.Range("A1:E200").Value
wbSource.Close SaveChanges:=False
I'm a little puzzled about your workbook close with save? Perhaps you actually want to close the source sheet unsaved and maybe save the destination sheet you're adding data to? In that case you'll need to add this line to the end of the above code.
wbDest.Close SaveChanges:=True

excel VBA error when copying a worksheet from workbook opened using activeworkbook.path

I need to copy a summary worksheet from 12 different workbooks into a regional workbook. I get a debugging error on the line that should copy the worksheet into the regional workbook. I tried using both the name and worksheet number on both the source and current workbooks. The code is included below.
Sub copyfc()
Dim sourceworkbook As Workbook
Dim currentworkbook As Workbook
Set currentworkbook = ThisWorkbook
Set sourceworkbook = Workbooks.Open("C:\Country Files\BE FC12 BU19.xlsx")
sourceworkbook.Sheets("BE").Copy after:=currentworkbook.Sheets("End")
sourceworkbook.Close
End Sub
The line below will place the worksheet at the end:
sourceworkbook.Sheets("BE").Copy After:=currentworkbook.Sheets(currentworkbook.Sheets.Count)

Unable to set workbook with open workbook

I have an open workbook from it I want to fetch data in another workbook.
My code is:
Dim wbsource as workbook
Dim wssource as worksheet
Dim wbtarget as workbook
Dim wstarget as worksheet
set wbsource = workbooks("D:/test.xlsx")
Even though my source workbook name and address is correct it's giving subscript out of range error.
If I close my source workbook and use
Set wbsource = workbooks.open ("D:/test.xlsx")
it works fine.
Try matching the name in the Set command to the caption name:
Sub SetupWorkbookObject()
Dim wb As Workbook
Set wb = Workbooks("sample.xlsm")
MsgBox wb.Name
End Sub
Note:
Neither the Set command nor the window caption have the full filespec, only the filename.

Resources