Is there any way to merge/combine data from multiple unsaved excel documents so its neatly in one document? Essentially I have lots of excel tabs open (all unsaved) and want to take columns A1-A20 and B1-B20 of all excels and place into one main document going downwards. SO rather than have heaps of different tabs with the information, there will be one ‘master’ document with all the info. So the master document could be book 1 or any other named excel.
Anyone have any idea how to go about this?
A lot of methods I’ve found involve saved files and it’d take hours to manually save them. I know you can add macros to templates upon opening/creating files, however I have no idea how you’d auto save each document opened and give it a different name.
If description inadequate -
[https://www.dropbox.com/s/0opi8pyy095hsrb/Book1%20as%20per%20image.xlsx?dl=0][1]
[https://www.dropbox.com/s/q4d3g526zgbwrl4/Book%201%20image.png?dl=0][2]
Place this code on a Standard Module of the workbook where you want to copy data from all the opened workbooks.
Run the code and the code will copy Range("A1:B20") from all the opened workbooks into the sheet1 of the macro workbook.
If required tweak it as per your requirement.
Sub CopyDataFromEachOpenedWorkbook()
Dim swb As Workbook, wb As Workbook
Dim sws As Worksheet, ws As Worksheet
Set swb = ThisWorkbook
Set sws = swb.Sheets(1)
For Each wb In Application.Workbooks
If Not wb Is swb Then
For Each ws In wb.Worksheets
ws.Range("A1:B20").Copy sws.Range("A" & Rows.Count).End(3)(2)
Next ws
End If
Next wb
End Sub
Related
I created an excel macro "add-in" for the first time. So now I can use the same macro across multiple workbooks using the quick link at the top of the workbook.
My issue is that the first command of my Macro is to add in a sheet "Sheet1". My workbook has 2 sheets in it currently. "Attrition 2017" and "Attrition 2018".
When I added in "Sheet1" the first time nothing happened, and the rest of my workbook errored out because of it. The second time I went through it said "Sheet1" already exists. This is the only workbook I have open. I tried it with numerous sheet names. it keeps adding sheets to an unknown spot then stating that they already exist. the rest of my code works with the add-in.
My code for adding in the worksheet works when not using the add-in function. here it is.
Dim ws As Worksheet
With ThisWorkbook
Set ws = .Sheets.Add(After:=.Sheets(.Sheets.Count))
ws.Name = "Sheet1"
End With
ThisWorkbook is the workbook where the code is running - in this case your add-in.
You probably need ActiveWorkbook here
is there a way where I can copy and paste data from any open workbook without using Workbooks.Open("FileName") ? I want to be able to copy the same set of data from a workbook, close it, and when I open another workbook, the macro would know that I am copying from the second workbook.
For example:
Code 1: Copying from first data source
Set wb1 = ThisWorkbook
Set wb2 = Workbooks.Open("C:\Users\HONL120\Desktop\Sept HC Reports\HR Headcount Report 2018 Australia SEPTEMBER.XLSX")
Code 2: Copying from second data source
Set wb1 = ThisWorkbook
Set wb2 = Workbooks.Open("C:\Users\HONL120\Desktop\Sept HC Reports\18 09 Malaysia HR Headcount Report 2018 (Sept).xlsx")
From the example above, I would like to avoid this and let the macro know that when I open the second data source, I am pulling data from it, without having to type out the filename over again.
Is there a way to achieve this? I hope my explanation is clear and thanks in advance!
I suppose you could loop through the open workbooks, and use an If Statement to ignore the ones you don't want to include:
Dim wb As Workbook
For Each wb In Application.Workbooks
If wb.Name <> "PERSONAL.XLSB" And wb.Name <> ThisWorkbook.Name Then
Rem: Do what you need to do with the other workbook
End If
Next wb
Sorry i didn't quite get what your goal is, but I suppose you can set a macro in your excel template file (please google on where it is locate, as it depends on your version), with the code activeworkbook.range(xxx).copy.
In this case whenever you open a new workbook you want to copy, run this macro in that excel (you can even set the macro to the quick access bar for easier access)
Hope this help.
Situation:
I have two workbooks:
Workbook #1: A Downloaded data set from online data repository
Workbook #2: A Master collection of Macros
I have built a collection of Macros to format a data set after it's been downloaded from an online repository. This data set can differ greatly based on features that users have chosen before downloading the data. The macros I created cover all possible scenarios. Thus, when opening the View Macros dialog box there are an overwhelming amount to choose from. Even with efficient naming conventions it's too much to sift through for my audience, who has an average to low experience level with Excel.
In order to simplify their experience, I wanted to level the playing field by simply providing a "Go" button after they choose from an ActiveX dropdown list.
I successfully created the dropdown, populated the list, and upon activation of the "Go" button, the selection triggers a specific Macro to run.
MY PROBLEM:
I need the Subcode on the "Go" button in Workbook #2 to force the macros to run in Workbook #1.
Thank you ahead of time for all your help!
You can take cues from the macro recorder.
Here is some basic code that is stored in one workbook and manipulates cells in another workbook.
Sub Macro2()
Dim mlib As Workbook
Dim wb As Workbook
Dim ms As Worksheet
Dim ws As Worksheet
Set mlib = ActiveWorkbook ' the file with the macros
Set ms = mlib.Worksheets("Sheet1")
' write into the macro workbook
ms.Range("B3").FormulaR1C1 = "asdf"
'activate another workbook that is already open and is called somefile.xlsx
Windows("somefile.xlsx").Activate
' set variables to reference that workbook
Set wb = ActiveWorkbook
Set ws = wb.Worksheets("Sheet1")
' write something into somefile.xlsx
ws.Range("B1").FormulaR1C1 = "copy me"
'copy something within somefile.xlsx
ws.Range("B1").Copy ws.Range("B2")
' copy something from the macro workbook to the somefile workbook
ms.Range("A1").Copy ws.Range("A5")
' activate the macro workbook
mlib.Activate
End Sub
I asked for some sample code so I could explain in your context.
There are about a dozen different ways how you can identify the "other" file instead of hard-coding it into the macro. If you could be bothered to provide a bit more information, that little detail could also be taken care of.
I want to reference different workbooks in my code and I have used this code:
Dim Basicwb As Excel.Workbook
Dim Basic As Excel.Application
Set Basic = New Excel.Application
Set Basicwb = Basic.Workbooks.Open("X:\Job\Masroori\3-042-PMS.xlsx")
but the problem is how can I refrence it if I dont want to open it each time. I used this code (without .Open) but I get this Error! : "Subscript out of range"
Set Basicwb = Basic.Workbooks("X:\Job\Masroori\3-042-PMS.xlsx")
Also, I dont want to activate the workbook each time, Is there any way?
Taken from the msdn site for the Workbooks property:
"Returns a Workbooks collection that represents all the open workbooks. Read-only."
hence the last line of your code gives you an error since the file is not open. AFAIK, you cannot reference objects within a workbook if that workbook is not open. You can access whatever a workbook has without activating it (so without using .Activate), but it has to be open. Maybe this SO question is of help to you:
Open Excel file for reading with VBA without display
If your workbook is open, you can do the following:
Dim wBook as Excel.Workbook, rngTemp as range
Set wBook = workbooks("wbName.xls")
With wBook
' Do stuff, no need to activate. Example:
set rngTemp=.sheets(1).usedRange
End With
I hope this helps?
Okay, so I've written a Macro that works fine when launched from the Workbook which I coded it in. However, I need the Macro to be able to work across various Workbooks.
I've moved the Macro to the hidden "personal" workbook, but, now when the code references "ThisWorkBook", well, yeah, it points to the personal workbook.
I had thought of just creating a workbook variable and pointing to the address/name of the workbook... but this varies (the workbook is generated by a system, and the name of the workbook is dependant on the time/date generated).
For reference, I want to put a button on the toolbar in Excel, and when the user clicks it, it runs this Macro. Is there, perhaps, a way to get the name of the Workbook that calls the Macro?
Thanks,
Sam.
There are Active commands which will allow you to reference the workbook and worksheet that the code is been called from.
ActiveCell
ActiveChart
ActiveEncryptionSession
ActivePrinter
ActiveProtectedViewWindow
ActiveSheet
ActiveWindow
ActiveWorkbook
All properties of the Application object
If there's a naming convention in workbook A (the data workbook) , you can do the following changes
to identify workbook A
Dim wbA As Workbook 'workbook A ( the data workbook)
Dim wb As Workbook
For Each wb In Workbooks 'loop through all opened workbooks, matching the name
If InStr(wb.Name, "ABC") > 0 Then
Set wbA = wb
Exit For
End If
Next wb
then replace the "ThisWorkBook" by wbA
You could also use VBS instead of a macro. The languages are pretty much identical, and your script would be independent of your workbooks.
As far as identifying which file to use to run the macro (without examples of your data and code, it's hard to be too specific), you can browse for the file you want.
Dim objShell
Set objShell = CreateObject("Shell.Application")
Dim strFileName
Dim strFilePath
Dim dPicker
Set dPicker = objShell.BrowseForFolder(0, "Choose a file:", &H4000)
strFilePath = dPicker.self.Path 'will give you the file path of the file you choose,
'which you can then use to open/access the file with
'the data you need.
'Code
Set dPicker = Nothing
If you want to stick with a macro, you can check out the FileDialog Object for using the file picker in VBA.
To get the filename and/or path of an open workbook in VBA, use
ActiveWorkbook.Name
ActiveWorkbook.Path
Hope this helps!
Just replace "ThisWorkbook" by "ActiveWorkbook".