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
Related
Trying to copy data from one workbook to the next.
When I try to select a worksheet I get a message
Select Method of Range Class Failed
I want to use select to copy paste special something to keep the formatting.
Public Sub Worksheet_Export()
'Setting Dimensions for Current Workbook and New workbook
Dim current_workbook As Workbook
Dim New_workbook As Workbook
Dim current_worksheet As Worksheet
Dim New_worksheet As Worksheet
Set current_workbook = ThisWorkbook
Set New_workbook = Workbooks.Add
Set current_worksheet = current_workbook.Sheets(2)
Set New_worksheet = New_workbook.Sheets(1)
'Copying Data From Current Workbook to CSV File Workbook
current_worksheet.Range("A:C").Select
End Sub
you need to activate the sheet before selecting the range
better solution:
current_worksheet.Range("A:C").Copy Destination:=New_worksheet.Range("A1")
Public Sub Worksheet_Export()
'Setting Dimensions for Current Workbook and New workbook
Dim current_workbook As Workbook
Dim New_workbook As Workbook
Dim current_worksheet As Worksheet
Dim New_worksheet As Worksheet
Set current_workbook = ThisWorkbook
Set New_workbook = Workbooks.Add
Set current_worksheet = current_workbook.Sheets(2)
Set New_worksheet = New_workbook.Sheets(1)
'Copying Data From Current Workbook to CSV File Workbook
current_worksheet.Activate 'you need to activate before selecting
current_worksheet.Range("A:C").Select
'better solution:
current_worksheet.Range("A:C").Copy Destination:=New_worksheet.Range("A1")
End Sub
I have a series of Workbooks that I continually need to copy Sheet1 of a workbook to Sheet2 of the new workbook. The names of the Workbooks will advance in number (name_May2011_2, name_May2011_3, name_May2011_5). The digit on the end will change, not necessarily in sequence. I have code that allows me to list the active workbooks in a new sheet. I need to reference a cell in that sheet as the name of the Workbook as the destination top copy. My code is as follows so far:
[code]
Sub Copy_Merge()
'Declare variables and data types
Dim Wb As Workbook
Dim Ws As Worksheet
Dim i As Single, j As Single
'Create a new worksheet and save to object ws
Set Ws = Sheets.Add
'Go through open workbooks
For j = 1 To Workbooks.Count
'Save workbook name to cell A1 and downwards
Range("A1").Cells(j, 1) = Workbooks(j).Name
'Iterate through worksheets in given workbook
For i = 1 To Workbooks(j).Sheets.Count
'Save worksheet names to cell B1 and cells further right
Range("A1").Cells(j, i + 1) = Workbooks(j).Sheets(i).Name
'Continue with next worksheet
Next i
'Continue with next workbook
Next j
'This is the part I'm having issues with
'I need to set variable in cell A2 of Sheet 2 of Active Workbook as a string
'and use that string as the Workbook destination name
Dim SB As Workbook
Dim Ss As Worksheet
Set SB = ThisWorkbook
Set Ss = ThisWorkbook.Sheet("Sheet2")
Set MyToday = SB.Ss.Range("A2").Value 'name of destination workbook
Sheets("Sheet1").Select
Sheets("Sheet1").Copy Before:=Workbooks(MyToday).Sheets(3)
On Error Resume Next
ActiveSheet.Name = "Sheet2"
On Error GoTo 0
End Sub
[/code]
I've been working on this for a few days, I'm relatively new to writing macro's, and I'm at my whit's end. Can someone help with this code or suggest a better code to use?
In order to retrieve the target workbook's name from cell "A2" of "Sheet2" of your current workbook and use it to assign the respective workbook to an object, you could use:
Dim SourceWorkbook As Workbook ' Your SB
Dim SourceWorksheet As Worksheet ' Your Ss
Dim TargetWorkbook As Workbook
Dim TargetWorksheet As Worksheet
Dim TargetWorkbookName As String
Dim TargetSheetName As String
Set SourceWorkbook = ThisWorkbook
Set SourceWorksheet = SourceWorkbook.Sheets("Sheet2") ' the "s" in "Sheets" is important to access the Workbook's Sheet-Collection
TargetWorkbookName = SourceWorksheet.Range("A2").Value ' TargetWorkbookName is of type "String", thus "Set" is not allowed
Set TargetWorkbook = Workbooks(TargetWorkbookName)
TargetSheetName = SourceWorksheet.Range("B2").Value
Set TargetWorksheet = TargetWorkbook.Sheets(TargetSheetName)
I want to be able to open up a second data source (workbook) and select the sheet within the second workbook which is equal to the name in a cell in my first workbook.
So far I have:
Dim src As Workbook
Dim src2 As Workbook
Dim shtno As String
Dim ws As Worksheet
Set src = ActiveWorkbook
shtno = Sheets("CONTROL").Range("G3").Value
Set ws = Sheets(shtno)
Workbooks.Open Filename:= 'file link
Set src2 = ActiveWorkbook
ws.Select
Where 'ws.Select' should select my sheet required. Where am I going wrong?
Thanks.
BigBen is correct.
You need to move the Set ws statement down below the Workbooks.Open statement then:
Set ws = src2.Sheets(shtno)
ws.Select
NOTE:
once you Open the new workbook, it becomes the Active workbook
you can only Select a worksheet on the Active workbook
I am trying to create a macro that will copy a range from one open sheet (Original.xlsx) to another open sheet (Destination.xlsx). The tricky part is I want the user to be able to name the origin excel filename (without the .xlsx at the end) via inputbox and I am having trouble with combining the dim with the copy function.
Dim wbdest As Workbook
Dim X As Variant
X = InputBox("Workbook from name?")
Set wbdest = Workbooks(X & ".xlsx")
Workbooks("wbdest").Worksheets("Sheet1").Range("A2:K25").copy
Workbooks("destination.xlsx").Worksheets("Sheet1").Range("A2").PasteSpecial Paste:=xlPasteValues
The input box in this example will be input with "Original"
I am getting a Runtime error 9, subscript out of range on
Workbooks("wbdest").Worksheets("Sheet1").Range("A2:K25").copy
Replace:
Workbooks("wbdest").Worksheets("Sheet1").Range("A2:K25").copy
with:
wbdest.Worksheets("Sheet1").Range("A2:K25").copy
(there may be other errors in your posted code)
Your be best of doing it like this;
Sub test()
Dim wb1 As Workbook 'declare the workbook
Dim wb2 As Workbook 'declare the workbook
Dim strFile As String
strFile = InputBox("Workbook from name?") 'open up input box for user to type in what workbook they want to copy
Set wb1 = Workbooks(strFile & ".xlsx") 'set the user defined workbook by name
Set wb2 = Workbooks("Destination.xlsx") 'set the destination workbook by name
wb2.Worksheets("Sheet1").Range("A2:K25").Value = wb1.Worksheets("Sheet1").Range("A2:K25").Value 'put the value from userdefined workbook into destination
End Sub
Try and avoid copy and pasting if you can :)
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.