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.
Related
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
How do I set a workbook to a variable name to be used later on in the sub?
I'm trying to open a workbook (PriceFile) and set values in this workbook to values in the original workbook (TestFile). I can open PriceFile but can't name the workbook.
Public Sub Get_Sum_Assured()
Dim TestFile As Workbook
Dim PriceFile As Workbook
Dim PriceFileName As String
Dim Test_Cases As Integer
Dim FirstTest As Integer
Dim CommDate As Date
Dim DOB As Date
Dim MonthPrem As Long
Dim SumAssured As Long
Dim TestCount As Integer
Set TestFile = ThisWorkbook
Call Open_Pricing_File
TestFile.Activate
PriceFileName = Range("Pricing_File").Value
Set PriceFile = Workbooks(PriceFileName)
Open_Pricing_File opens the file named in "Pricing_File" and when I've stepped through this works. When I try and set PriceFile to this workbook, the code falls over on this last line.
The following macro opens two workbooks and gives them the name "wb1" and "wb2".
At the end the value from cell A1 is copied from "wb1" to "wb2".
Sub copyValue_wb1wb2()
Dim wb1 As Workbook
Dim wb2 As Workbook
'wb1 (workbook1)
Workbooks.Open Filename:="C:\Data\ExcelFile1.xlsm", Local:=True, ReadOnly:=False
Set wb1 = ActiveWorkbook
'wb2 (workbook2)
Workbooks.Open Filename:="C:\Data\ExcelFile2.xlsm", Local:=True, ReadOnly:=False
Set wb2 = ActiveWorkbook
'Now you can jump between workbooks like:
wb1.Activate
wb2.Activate
'You can insert a value from wb1 to wb2 like:
wb2.Sheets(1).Range("A1").Value = wb1.Sheets(1).Range("A1").Value
End Sub
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")
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
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