I currently run a sub in another (open) workbook using this line:
Application.Run ("'NDS Analytics.xlsm'!Process")
The names of the referenced workbooks are changing so I would like to change the reference so that instead of looking for file NDS Analytics.xlsm it looks for an open file that contains the words "NDS Analytics".
There will only ever be one file with this name open at a time and it could be called either NDS Analytics.xlsm, wNDS Analytics.xlsm or 40ghNDS Analytics.xlsm.
Application Run with Variable Workbook
Option Explicit
Sub runProcess()
Dim wb As Workbook
For Each wb In Workbooks
If UCase(wb.Name) Like UCase("*NDS Analytics*.xlsm") Then
Application.Run "'" & wb.Name & "'!Process"
Exit For
End If
Next wb
End Sub
Related
I have files I download on a daily basis but the name of the file changes every day.
Ex: Day 1 file name: miss123_1 Day 2: miss349_1
Is there any way to have the macro look for open files where the active workbooks title contains certain characters rather than having the macro look for that exact file? My current workaround is to constantly change the workbook title to fit that days name but there must be a way to make it dynamic.
My idea that does not work:
Windows(Contains("miss","_1",".xlsx")).Activate
Reference an Open Workbook by Partial Name
You need to loop through the Workbooks collection and compare each workbook name to a string pattern.
You can use the Like operator for the comparison.
The RefWorkbookTEST procedure illustrates how to utilize the RefWorkbook function.
Option Explicit
Sub RefWorkbookTEST()
Dim wb As Workbook: Set wb = RefWorkbook("miss*_1*.xlsx")
If wb Is Nothing Then
MsgBox "Workbook not found.", vbExclamation
Else
MsgBox "Referenced workbook '" & wb.Name & "'.", vbInformation
End If
End Sub
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Purpose: References an open workbook whose lower-case name matches
' a given lower-case string pattern.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Function RefWorkbook(ByVal LowerCaseNamePattern As String) As Workbook
Dim wb As Workbook
For Each wb In Workbooks
If LCase(wb.Name) Like LowerCaseNamePattern Then
Set RefWorkbook = wb
Exit Function
End If
Next wb
End Function
This is my current method of selecting a workbook with a dynamic name (generated each time I generate the report):
Sub SelectWorkbooks()
Dim wb As Workbook
For Each wb In Application.Workbooks
If wb.Name <> "PERSONAL.XLSB" And wb.Name <> "Tracker.xlsb" Then
wb.Worksheets(1).Cells.Copy Destination:=Workbooks("Tracker.xlsb").Sheets("OfficeAppointments").Cells
wb.Close
GoTo EndAll
End If
Next wb
EndAll:
End Sub
This... works. But it's probably slow and inefficient as it loops through as many as three workbooks to determine if it should take the information from it and paste it to the Tracker.xlsb file.
The only constant in the generated file name is OfficeAppointments_JT_, if that helps. Is my code the best it can be, or can we improve upon it any?
I have two Excel files open. I would like to create a formula that refers to the current workbook AND another open workbook. The referenced file name is not always the same and may have spaces, and will be used in a formula.
I can reference the current file name and use it in a formula with CELL("filename") but am not sure how reference another (not currently active) open file. How can this be done?
You will need something like this function in your workbook VBA.
Function OtherName()
Dim wb As Workbook, x As String
For Each wb In Workbooks
If wb.Name <> ThisWorkbook.Name Then
x = wb.Name
End If
Next wb
OtherName = x
End Function
Then you just have to call it with your code.
I want to reference from code in an active workbook to another workbook,
I don't want to type path like that workbooks("path") , this reference should be flexible, is there something like array of already opened workbooks ?
You can assign an open workbook to a variable without providing the full path. You can then use the set object variable to perform any actions you wish.
Sub set_wb()
Dim wb As Workbook
Set wb = Workbooks("test_wb.xlsb")
wb.Activate
End Sub
You can also iterate through each open workbook using for each
Sub wb_names()
Dim wb As Workbook
For Each wb In Workbooks
Debug.Print wb.Name
Next wb
End Sub
Similarly, you can use for to iterate through each workbook using their index (the index is dependant on which order workbooks were opened).
Sub wb_index()
Dim i As Byte
For i = 1 To Workbooks.Count
Debug.Print Workbooks(i).Name
Next i
End Sub
Hope this helps.
See this answer.
To reference an already open workbook, you can use
Workbooks("book_name.xlsx")
You can also iterate through the collection
Dim i As Integer
For i = 1 To Workbooks.Count
MsgBox Workbooks(i).Name
Next i
I created a macro in my workbook using an active worksheet from another workbook. I would now like to run my macro, but use yet another different active workbook to get my data.
I have this line 20 times in my macro...
Windows("IFS_round_1").Activate
...so I do not want to change it (ex. IFS_round_2) each time I open a new workbook to run the macro. Is there something I can add so the macro just uses whichever active workbook I have open?
Thanks!
Create a variable to refer to the workbook. For example:
Sub Macro1()
Dim wb as Workbook
Set wb = ActiveWorkbook
wb.Activate
'DO CODE HERE
End Sub
Does this help?
I don't know if you are opening the other workbook programatically, but this solution works. basically you just save the handle to the other workbook as soon as you open it.
sother_filename = "IFS_round_1"
'saves the name of the current workbook
curr_workbook = ActiveWorkbook.Name
'opens the new workbook, this automatically returns the handle to the other
'workbook (if it opened it successfully)
other_workbook = OpenWorkbook(sother_filename)
But what fun is that? one more solution to automatically get the workbook name when there are only two workbooks open and then simply use that to call the other workbook
Function GetOtherWBName()
GetOtherWBName = ""
'if we dont have exactly two books open
'we don't know what to do, so just quit
If (Workbooks.Count) <> 2 Then
Exit Function
End If
curr_wb = ActiveWorkbook.Name
'if the active workbook has the same name as workbook 1
If (StrComp(curr_wb, Workbooks(1).Name) = 0) Then
'then the other workbook is workbook 2
GetOtherWBName = Workbooks(2).Name
Else
'then this is the other workbook
GetOtherWBName = Workbooks(1).Name
End If
End Function
So now in the workbook that has the macro make a button and assign a macro to it similar to this
Sub ButtonClick()
'first we save the current book, so we can call it easily later
curr_wb = ActiveWorkbook.Name
other_wb = GetOtherWBName()
If Len(other_wb) = 0 Then
MsgBox ("unable to get other wb")
Exit Sub
End If
'now to call the other workbook just use
Workbooks(other_wb).Activate
'all the rest of your code
End Sub