copy from whole external worksheet to worksheet in mainbook - excel

This code works up through the paste into sheet 2 - I can switch over to the new open workbook and see that it is in copy mode but cannot get it to paste into Sheet2(Test). I have tried both "Sheet2" and "Test" but get
run time error 9: subscript out of range
see snip below
Sub ImportWorksheet999()
Dim Wb1 As Workbook
Dim MainBook As Workbook
'Open All workbooks first:
Set Wb1 = Workbooks.Open("G:\T\TWeir\Prod Ctrl\XML Reports\BDA\BDAREPORTtest.xlsx")
Set MainBook = ActiveWorkbook
'Now, copy what you want from wb1:
Wb1.Sheets("BDA Report").Cells.Copy
'Now, paste to Main worksheet:
MainBook.Sheets("Sheet2").Range("A1").PasteSpecial
'Close Wb's:
Wb1.Close
End Sub

Set Wb1 = Workbooks.Open("G:\T\TWeir\Prod Ctrl\XML Reports\BDA\BDAREPORTtest.xlsx")
Set MainBook = ActiveWorkbook
Opening a file will make it the ActiveWorkbook, so your Wb1 and MainBook both refer to the opened workbook.
Should be
Set MainBook = ActiveWorkbook 'or ThisWorkbook if that's where the code is running
Set Wb1 = Workbooks.Open("G:\T\TWeir\Prod Ctrl\XML Reports\BDA\BDAREPORTtest.xlsx")

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

Macro in Personal.xlsb - Active workbook reference [duplicate]

I have a number of scripts that are in a module in my Personal.xlsb file. It's kept hidden, but in this script, the idea is that you run it from within a different workbook each time. It opens a separate workbook (source.xlsx), copies a range from it, pastes into the original workbook, and then closes source.xlsx.
When it comes to the "ThisWorkbook.ActiveSheet.Paste" part, it's pasting it into the Personal.xlsb workbook instead of the target workbook that is actually open and visible. How can I make sure it's being pasted in the right workbook? The workbook's filename will always be different, so I can't specify a path or anything like that.
Sub CopyData()
Application.DisplayAlerts = False
Dim wbSource As Workbook
Set wbSource = Workbooks.Open(Filename:="source.xlsx", UpdateLinks:=3)
wbSource.Sheets(1).Range("A1:X105").Copy
ThisWorkbook.ActiveSheet.Paste
wbSource.Close
Application.DisplayAlerts = True
Call CopyCFormat
End Sub
Don't use ThisWorkbook in most cases, as it references the workbook that the macro is stored in (in this case, personal.xlsb).
Instead, you can use ActiveWorkbook to refer to whichever workbook has focus at the time the macro is run. You can also assign ActiveWorkbook to a variable for easier reference.
Sub CopyData()
Application.DisplayAlerts = False
Dim wbSource As Workbook
Dim wbTarget as Workbook
Set wbTarget = ActiveWorkbook
Set wbSource = Workbooks.Open(Filename:="source.xlsx", UpdateLinks:=3)
wbSource.Sheets(1).Range("A1:X105").Copy
wbTarget.ActiveSheet.Paste
wbSource.Close
Application.DisplayAlerts = True
Call CopyCFormat
End Sub
You could also reference the active sheet without specifying which workbook it's in, as:
Dim wbSource As Workbook
Dim shtTarget as Worksheet
Set shtTarget = ActiveSheet
Set wbSource = Workbooks.Open(Filename:="source.xlsx", UpdateLinks:=3)
wbSource.Sheets(1).Range("A1:X105").Copy
shtTarget.ActiveSheet.Paste
Luck!
If I understand it, you should just add another workbook variable.
Sub CopyData()
Dim mainWB As Workbook
Dim mainWS As Worksheet
Set mainWB = ActiveWorkbook
Set mainWS = mainWB.Sheets(1) ' Change this to whatever you need it to be
Application.DisplayAlerts = False
Dim wbSource As Workbook
Set wbSource = Workbooks.Open(Filename:="source.xlsx", UpdateLinks:=3)
wbSource.Sheets(1).Range("A1:X105").Copy
mainWS.Paste
wbSource.Close
Application.DisplayAlerts = True
Call CopyCFormat
End Sub

VBA, activate workbook without explicitly naming it

my code has workbook A and workbook B. I copy sheets from B to A. The issue is that both will be open, so I explicitly name each workbook.
When I paste into B I activate A by its name. Is there anyway I can activate A without using the workbook name?
'Set Current Workbook as Master
Set masterWB = Application.ThisWorkbook
'Set some Workbook as the one you are copying from
Set dailyWB = Workbooks.Open(Sheets("Control Manager").Range("O2"))
'Copy the Range from dailyWB and Paste it into the MasterWB
dailyWB.Sheets("Summary").Range("A1:BJ200").Copy masterWB.Sheets("AFS Summary").Range("A1").Rows("1:1")
'formatting and paste as values
Workbooks("Workbook B").Activate
Worksheets("Summary").Select
If you are not wanting to explicitly set your workbook, then you can always expect some room for error.
But, the following should allow you to set the alternate workbook if you only have two workbooks open - otherwise if you have more open, then this will probably not work as expected.
Dim masterWB As Workbook, dailyWB As Workbook, wb As Workbook
Set masterWB = ThisWorkbook
For Each wb In Application.Workbooks
If wb.Name <> masterWB.Name Then
Set dailyWB = wb
exit for
End if
Next
Debug.Print dailyWB.Name

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.

Excel vba how to copy sheet with all formatting & page setup

I've seen quite a few examples for making a full copy of a worksheet but none of them are working for me. In my case the sheet has to go into a new workbook. In my actual code wb is defined global and the workbook is created in another sub that called this one.
Dim wb As Workbook
Set wb = Workbooks.Add()
Dim newtab as Worksheet
With ActiveWorkbook
.Sheets("Sample Attendance").Copy After:=wb.Sheets(.Sheets.Count)
Set newtab = wb.Sheets(wb.Sheets.Count - 1)
End With
didn't work.
Likewise
ActiveWorkbook.Sheets("Sample Attendance").Copy After:=wb.Sheets(1)
Set newtab = wb.Sheets("Sample Attendance")
newtab.Name = tabname
both methods return after the Copy statement.
I've been moderately successful with this:
Set newtab = wb.Worksheets.Add
newtab.Name = tabname
Set Attendance = ThisWorkbook.Sheets("Sample Attendance")
Attendance.Range("A:BB").Copy Destination:=newtab.Cells(1, 1)
which works. But then I have to copy all of the PageSetup across which is giving me fits and takes forever.
I see two problems with your 1st piece of code...
You are using Activeworkbook. When you add a new workbook, the new workbook becomes your active workbook :)
The second problem is the DOT before .Sheets.Count in wb.Sheets(.Sheets.Count). why pick the count from the workbook you are copying?
Try this
Sub Sample()
Dim thiswb As Workbook, wb As Workbook
Dim newtab As Worksheet
Set thiswb = ThisWorkbook
Set wb = Workbooks.Add()
With thiswb
.Sheets("Sample Attendance").Copy After:=wb.Sheets(wb.Sheets.Count)
Set newtab = wb.Sheets(wb.Sheets.Count - 1)
End With
End Sub
Give this a shot:
Sub SheetCopier()
Dim OriginalWB As Workbook
Dim NewWB As Workbook
Set OriginalWB = ActiveWorkbook
Workbooks.Add
Set NewWB = ActiveWorkbook
OriginalWB.Sheets("qwerty").Copy Before:=NewWB.Sheets(1)
End Sub

Resources