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".
Related
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.
I've got my macros tucked away in my Personal.xlsb file, and I would like to save a file in the same directory as the workbook I'm applying the macro to.
Currently when I use:
Application.ThisWorkbook.Path
Its getting the location of Personal.xlsb (where the macro stems from) and not the workbook's directory. Can someone please tell me how I can fix this (without adding a module to my workbook)?
The Path property can be used on any Workbook object, not just on the ThisWorkbook one.
So, to obtain the path of the ActiveWorkbook, you can use ActiveWorkbook.Path.
Or if you have, for instance, opened a workbook using something like:
Dim wb As Workbook
Set wb = Workbooks.Open("C:\Temp\Test\File1.xlsx")
then you can use wb.Path which would, of course, return "C:\Temp\Test".
Is it possible to transfer an open workbook to another Excel instance? I.e. 're-parenting' an open workbook without closing and re-opening?
The reason I want to do this is to have a separate instance for a particular workbook. When that workbook opens, it can create a new instance of Excel, and then I'm hoping to somehow add that workbook to the other applications Workbooks collection, while removing it from the original.
The point of this is so that I can run a modal forms application, while still retaining the ability to use Excel. For example, I'd like to programatically copy data to the clipboard while letting the user paste it into another workbook.
Making the original forms application non-modal is NOT a solution as this is a requirement (we've gone to elaborate lengths to keep regular users out of the worksheets and only close the forms with a password.)
For context, I've tried opening the other instance with the same workbook and then closing, but this has problems with the newly opened workbook being read-only. (There are also other reasons why this doesn't work well.)
As the code below demonstrates, you can access a workbook in another instance of Excel and manipulate it.
Dim xlApp As Excel.Application
Dim Wb As Workbook
Dim xlApp2 As Excel.Application
Dim Wb2 As Workbook
Set Wb = ActiveWorkbook
Set xlApp = Wb.Parent
Set xlApp2 = New Excel.Application
Set Wb2 = xlApp2.Workbooks.Open("E:\My Documents\Weekly Report.xlsx")
Wb.Worksheets("Target").Cells(1, 1).Value = Wb2.Worksheets(1).Cells(4, 1).Value
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
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?