how do I tranfser data between two closed workbooks without creating a new one? I have the following scenario:
2 excel workbooks(wb1 and wb2) with different structures
I need certain data from wb1 to be transfered to wb2
I can't modify wb1 & wb2 with macros
My question: Is it possible to activate a macro from a seperate excel workbook - lets name it wb3? So, triggering the macro in wb3 would transfer all the relevant data from wb1 to wb2....how would a macro like this look like?
Thanks!
Sub CommandButton1_Click()
Dim wb1 As Workbook
Dim wb2 As Workbook
Set wb1 = Application.workbooks.open("C:\Users\PlutoX\MyExcelfile1.xlsx")
Set wb2 = Application.workbooks.open("C:\Users\PlutoX\MyExcelfile1.xlsx")
wb2.Sheets("Sheet1").Range("A1") = wb1.Sheets("Sheet1").Range("A1")
wb1.Close False
wb2.Close True
End Sub
Almost there! You need to change three things:
If you want to open a workbook, you need to state the full path
Set wb1 = Application.workbooks.open("C:\Users\PlutoX\MyExcelfile1.xlsx")
If stuff needs to be transferred to wb2, the order is wb2 = wb1 so
wb2.sheets("Sheet1").Range("A1").value = wb1.sheets("Sheet1").Range("A1").value
If you close the workbooks, make sure you save at least the one with changes
wb2.Close True
Related
I'm working on an app that has to use two more excel files for its function. Names of those files change every day so I decided that both of those files are going to be opened from cells that contain functions to change the name based on the date like this.
Workbooks.Open Range("C2")
Windows("App.xlsm").Activate
Workbooks.Open Range("C16")
Now the problem is that I don't know how to switch between the books that I've opened. Normally I'd use something like this:
Windows("A.xlsx").Activate
But I clearly can't do that now. My idea was to activate App.xlsm and then activate a workbook written in the cell that I used like his:
Windows(Workbooks("App.xlsm").Sheets("Pom").Range("C16").Value).Activate
However that doesn't work. Now I'm not sure whether my code is wrong or whether this method just isn't possible. Can someone help me please ?
Use variables to have a handle on the workbooks
Dim wb1 as workbook
dim wb2 as workbook
set wb1 = Workbooks.Open (Range("C2"))
set wb2 = Workbooks.Open (Range("C16"))
' Activate wb1
wb1.activate
' or activate wb2
wb2.activate
Is there a way to make an open (but inactive) workbook active when the name of the workbook is not known? I'd like to build a macro in one workbook that when run brings up another open workbook and then executes the rest of the macro on that workbook.
Also, is there a way to select from among several open, inactive workbooks if the user has more than one open?
Thanks in advance!
You can loop through all workbooks in the application's workbooks collection:
var wb as workbook
For Each wb In Application.Workbooks
If instr(1, wb.name, "hi") Then --activate a workbook if it's name has "hi" in it
wb.activate
End If
'or
If wb.name <> ThisWorkbook.name Then --find the first workbook that isn't this workbook
wb.activate
End If
Next
Basically, inside the loop you can test each workbook found (stored in the wb variable) and activate it depending on whatever property you want to test. This is about as specific as I can answer since you don't describe how you, as a human, would identify which workbook should be activated. You only say that you can't identify it by it's name.
So I currently have three workbooks, I have created a folder in my Desktop, and where I am using workbook x, through a range on a cell to open up workbook called m (old) and workbook called n (new). I am then updating the old with the new.
Problem
Once I have opened up m, I am having to do a save as of the file as a newname. I am keen to know is there way of referring to it without using the workbook newname, as this workbook name would be concatenated with today's date and the name can be quite long and time consuming to type. I have produced several subroutines, one to remove protection etc,. and one for carrying out the coping actions and I am calling the subroutines, I just want to now if there is way of linking the newsaved name to some of the subroutines from referencing. Alternatively the old file could be saved at the very end. I am keen to pursue this avenue as I will be playing in certain other cases with four workbooks.
I have taken this code and amended it, but I am unable to open both spreadsheets, the strange thing when i block out the code and go through a test both work for each path they work, but not at the same time.
this is the code i amended, which i was able to find in
Dim x As Workbook, y As Workbook
Dim ws1 As Worksheet, ws2 As Worksheet
Set x = Workbooks.Open("path to copying book")
Set y = Workbooks.Open("path to pasting book")
Set ws1 = x.Sheets("Sheet you want to copy from")
Set ws2 = y.Sheets("Sheet you want to copy to")
With ws1
.Cells.Copy ws2.cells
y.Close True
x.Close False
End With
End Sub
this is my code where it is not opening both workbooks
Dim wb as Workbook
Dim wb1 As Workbook, wb2 As Workbook
Dim ws as Worksheet, ws1 As Worksheet, ws2 As Worksheet
Dim Lst As Long,
Dim r1, r2 As Range
Set wb = ThisWorkbook
Set wb1 = Workbooks.Open("Sheets("x").Range("A4").Value")
Set wb2 = Workbooks.Open("Sheets("x").Range("A5").Value")
With wb2
Call Wbkunprtect()
I would be grateful for some help, please for those who are trigger happy can you hold back from pressing the down arrow some of us are not excel knowledgeable and also trying to learn and do not want to be banned from asking questions, I am trying to move data between two workbooks wb1 and wb2, through another wb i have completed my code, and this is the bit which stopping me from going forward.
I have learned a great deal from this site the reason I asked certain questions as I am reluctant to use select or activate as I have been told this is a bad habit you have to keep away from.
From what I understood, you'll need to use a WorkBook object! the WorkBook object will have the instance of the newly added workbook. It is as simple as that:
Sub example_new_workbook()
Dim wb As Workbook
Set wb = Workbooks.Add
wb.SaveAs ("path where you want to save")
End Sub
That way, "wb" will have the instance of the new workbook, and you can use freely to reference that new workbook. Remember, the WorkBooks object holds all the instances of all open workbooks, so you can get the reference for any other workbook through this object.
As to make it "Global", you can declare it outside the "Sub" scope, so the variable will be permanent as long as the module (or wherever you put your code) is open like this:
public wb as Workbook
Sub <your_routines>()
...
End Sub
I am currently working on an integrated set of workbooks, where I need to transfer data values between workbooks. In order to do so I need a VBA Macro that can copy a specific range (a row in a worksheet) and insert it at the bottom of an overview list in a different book.
How can I do this?
I am somewhat of a VBA novice, so specific instructions are appreciated.
Note: I'm using MS Excel 2010.
Thank you in advance!
The following example copy will copy the contents of the cells A1:A3 from a Worksheet named Source in a Workbook named workbook1.xlsx into the Worksheet named Target in a different Workbook, workbook1.xlsx. It will throw an error if either of the two Workbooks are not open, or if they do not contain the Worksheets that the Sub calls for.
Sub Example()
Dim wb1 As Workbook
Dim wb2 As Workbook
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Set wb1 = Workbooks("workbook1.xlsx")
Set wb2 = Workbooks("workbook2.xlsx")
Set ws1 = wb1.Sheets("Source")
Set ws2 = wb2.Sheets("Target")
ws2.Range("A1:A3") = ws1.Range("A1:A3").Value
End Sub
How do I activate my Other workbook from the Current workbook? I have a current workbook with dumb.xls and The other workbook name as Tire.xls.I have opened the Tire.xls from the dumb.xls using worksbooks.open filename:= "name of the file".Its getting open but The problem is Im unable to make it work.
If I say cells(2,24).value=24 puts these value in the cell of dumb.xls but I want it to be done one Tire.xls.
activesheet.cells(2,24).value=24 puts these on Tire.xls. But how do i activate the Workbook with the name ? I need to open 3 to 4 excel workbooks And perform the operation? How do I activate the specific workbook
I have found this code on google
activeworkbook.worksheet("sheetname").activate ' but not working
windows("sheetname").activate ' people on google suggested not to use
Its not getting activated. I dont know how to make it work. Can anyone tell me How do i activate a specific workbook and a specific sheet of the other workbook ?
Example: I have niko.xls and niko_2.xls opened as workbooks from the dumb.xls workbook so totally 3 workbooks and I have to activate the 2nd sheet of niko_2.xls workbook.How do I make it? Can anyone explain me the syntax with these example? Thank you in advance
You do not need to activate the sheet (you'll take a huge performance hit for doing so, actually). Since you are declaring an object for the sheet, when you call the method starting with "wb." you are selecting that object. For example, you can jump in between workbooks without activating anything like here:
Sub Test()
Dim wb1 As Excel.Workbook
Set wb1 = Workbooks.Open("C:\Documents and Settings\xxxx\Desktop\test1.xls")
Dim wb2 As Excel.Workbook
Set wb2 = Workbooks.Open("C:\Documents and Settings\xxxx\Desktop\test2.xls")
wb1.Sheets("Sheet1").Cells(1, 1).Value = 24
wb2.Sheets("Sheet1").Cells(1, 1).Value = 24
wb1.Sheets("Sheet1").Cells(2, 1).Value = 54
End Sub
You have to set a reference to the workbook you're opening. Then you can do anything you want with that workbook by using its reference.
Dim wkb As Workbook
Set wkb = Workbooks.Open("Tire.xls") ' open workbook and set reference!
wkb.Sheets("Sheet1").Activate
wkb.Sheets("Sheet1").Cells(2, 1).Value = 123
Could even set a reference to the sheet, which will make life easier later:
Dim wkb As Workbook
Dim sht As Worksheet
Set wkb = Workbooks.Open("Tire.xls")
Set sht = wkb.Sheets("Sheet2")
sht.Activate
sht.Cells(2, 1) = 123
Others have pointed out that .Activate may be superfluous in your case. You don't strictly need to activate a sheet before editing its cells. But, if that's what you want to do, it does no harm to activate -- except for a small hit to performance which should not be noticeable as long as you do it only once or a few times. However, if you activate many times e.g. in a loop, it will slow things down significantly, so activate should be avoided.
You can try this.
Workbooks("Tire.xls").Activate
ThisWorkbook.Sheets("Sheet1").Select
Cells(2,24).value=24
The code that worked for me is:
ThisWorkbook.Sheets("sheetName").Activate
try this
Windows("name.xls").Activate
Dim Wb As Excel.Workbook
Set Wb = Workbooks.Open(file_path)
Wb.Sheets("Sheet1").Cells(2,24).Value = 24
Wb.Close
To know the sheets name to refer in Wb.Sheets("sheetname") you can use the following :
Dim sht as Worksheet
For Each sht In tempWB.Sheets
Debug.Print sht.Name
Next sht