I'm using this code to copy and paste values from one excel file to another and it works great:
Sub TransferValues()
Dim wb1 As Workbook
Dim wb2 As Workbook
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Set wb1 = Workbooks("ABC.xls")
Set ws1 = wb1.Sheets("SHEET1")
Set wb2 = Workbooks("CBA")
Set ws2 = wb2.Sheets("SHEET2")
'This line puts the value from wb1.ws1 in wb2.ws2, cells specified:
ws2.Range("O3:O33").Value = ws1.Range("I82:I112").Value
ws2.Range("O35:O65").Value = ws1.Range("J82:J112").Value
End Sub
BUT, as of right now I have to manually tell VBA that it has to grab the "ABC" file.
Is there a way to let Excel read a cell that contains the Workbook name.
So for example workbook CBA says in A1 ABC
I was thinking something like: Set wb1 = Workbooks("(A1).xls")
Thank you!
Related
I am trying to sum values from my original worksheet in specific cells in my newly created worksheet, which has a template to fill out.
When I used macro recorder, it references the worksheet name, which would not be useful as the worksheet name changes depending on which worksheet I am working in when I run the code.
So I tried changing the worksheet name to a variable "XCXX".
The first argument works so I thought everything was okay, however, on the second argument, it keeps trying to open a file, when it should simply go back to XCXX and pull the values.
Is it a problem with my activesheet changing?
Sub AddWorkbooks()
Dim ChangeOrder As Range
Dim XCXX As Worksheet
Dim CoForm As Worksheet
Set XCXX = ActiveSheet
Set CoForm = Worksheets("+CO Form+")
'Set wbNew = Workbooks.Add
CoForm.Copy After:=Sheets(ActiveSheet.Index)
With CoForm
Range("A6:D6").Select
ActiveCell.FormulaR1C1 = XCXX.Range("D2").Value
Range("AD81").Select
ActiveCell.FormulaR1C1 = "='XCXX'!R[-64]C[-24]+'XCXX'!R[-64]C[-23]"
End With
End Sub
This should be close:
Sub AddWorkbooks()
Dim ChangeOrder As Range
Dim XCXX As Worksheet, wb As Workbook
Dim CoForm As Worksheet, CoFormCopy As Worksheet
Set wb = ActiveWorkbook
Set XCXX = ActiveSheet
Set CoForm = wb.Worksheets("+CO Form+")
CoForm.Copy After:=XCXX
Set CoFormCopy = XCXX.Next 'the copy of "+CO Form+"
With CoFormCopy 'assuming you want to work with the copy?
.Range("A6:D6").Value = XCXX.Range("D2").Value
.Range("AD81").FormulaR1C1 = _
Replace("='<nm>'!R[-64]C[-24]+'<nm>'!R[-64]C[-23]", "<nm>", XCXX.Name)
End With
End Sub
Note when using With you need to use a period to link (eg) Range() with the object used in the With statement, otherwise it defaults to the active sheet.
Also generally there's no need to select a range to do something with it.
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
This may be pretty basic but I haven't had success yet.
I have a block of data within a range on workbook 1.
Each cell has a value that corresponds to tab names of workbook 2.
How can I use these cell's value to activate the proper corresponding tab on workbook 2?
The code is on another computer sorry no examples right now.
I tried so far setting a variable equal to the cell's value, but can't seem to translate it into a workbook object for calling the tabs.
thanks!
Assume your tab name is in Sheet1 in Workbook1 then we can use the following method:
Option Explicit
Sub Tab_Caller()
Dim wb1 As Workbook, wb2 As Workbook
Dim ws1 As Worksheet, ws2 As Worksheet
Dim sSheetName As String
'set the workbook objects
Set wb1 = ThisWorkbook
Set wb2 = Workbooks("MySecondWOrkbook.xlsx")
'set the worksheet
Set ws1 = wb1.Sheets("Sheet1")
sSheetName = ws1.Range("A1").Value
'Activate the required tab in wb2 using value in ws1
wb2.Sheets(sSheetName).Selct
wb2.Sheets(sSheetName).Activate
End SUb
Assuming that you have a WB1.xlsm with this data:
and a WB2.xlsx with these three sheets:
both open, positioning the cursor on the AA cell as above and running this code:
Workbooks("WB2.xlsx").Sheets(ActiveCell.Value).Activate
Will activate WB2 and activate the AA sheet.
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 :)