Justincut
asked on
How to use a wildcard * in VBA for a File Path?
Hi Guys, I have a Excel file that I am trying to copy and paste to from another Excel file via a Macro in which the code I have given a wildcard as the File name ending changes at random,but I get a "Runtime Error:Subscript out of Range" message every time I run the Macro. Can anyone help? See code below:
Sheets("Daily Commentary").Select
Windows("BluesFinal-EMEAMo rtgages" & "*.xlsx").Activate
Sheets("B7_CONS-MORTMP_EME A_MP, CB, SP").Select
ActiveSheet.Outline.ShowLe vels RowLevels:=3
Range("M8").Select
Range(Selection, Selection.End(xlDown)).Sel ect
Range(Selection, Selection.End(xlToLeft)).S elect
Selection.SpecialCells(xlC ellTypeVis ible).Sele ct
Selection.Copy
Windows("Mortgages Commentary*.xlsx").Activat e
Range("A11").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows("BluesFinal-EMEAMo rtgages*.x lsx").Acti vate
ActiveWindow.SmallScroll ToRight:=7
Range("U8").Select
Range(Selection, Selection.End(xlDown)).Sel ect
Selection.SpecialCells(xlC ellTypeVis ible).Sele ct
Application.CutCopyMode = False
Selection.Copy
Windows("Mortgages Commentary*.xlsx").Activat e
Sheets("Daily Commentary").Select
Windows("BluesFinal-EMEAMo
Sheets("B7_CONS-MORTMP_EME
ActiveSheet.Outline.ShowLe
Range("M8").Select
Range(Selection, Selection.End(xlDown)).Sel
Range(Selection, Selection.End(xlToLeft)).S
Selection.SpecialCells(xlC
Selection.Copy
Windows("Mortgages Commentary*.xlsx").Activat
Range("A11").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows("BluesFinal-EMEAMo
ActiveWindow.SmallScroll ToRight:=7
Range("U8").Select
Range(Selection, Selection.End(xlDown)).Sel
Selection.SpecialCells(xlC
Application.CutCopyMode = False
Selection.Copy
Windows("Mortgages Commentary*.xlsx").Activat
ASKER
Yep. That is correct. What alternatives are there?
You can iterate through the windows and check the name of each one, if one matches what you are after then select that one.
Here is a subroutine that you call by passing in the filename that you want to look for, it works by finding the last window that it finds with a caption that matches the entered text from the start of the caption.
Here is a subroutine that you call by passing in the filename that you want to look for, it works by finding the last window that it finds with a caption that matches the entered text from the start of the caption.
SelectMatchingWindow ("BluesFinal-EMEAMortgages")
Sub SelectMatchingWindow(MatchingFilename As String)
Dim NumberOfWindows As Integer
Dim Count1 As Integer
Dim MatchedWindowNumber As Integer
Dim WindowName As String
NumberOfWindows = Windows.Count
For Count1 = 1 To NumberOfWindows
WindowName = Windows(Count1).Caption
If InStr(WindowName, MatchingFilename) = 1 Then
MatchedWindowNumber = Count1
End If
Next Count1
If MatchedWindowNumber > 0 Then
Windows(MatchedWindowNumber).Activate
End If
End Sub
ASKER
Hi, this is too complicated. There is only 2 files with Wildcard in my Code:
Windows("Mortgages Commentary*.xlsx), which is the Excel file with the Macro in and
Windows("BluesFinal-EMEAMo rtgages" & "*.xlsx"), which is the Excel file we are copying from.
Can you simplify the code?
Windows("Mortgages Commentary*.xlsx), which is the Excel file with the Macro in and
Windows("BluesFinal-EMEAMo
Can you simplify the code?
ChloesDad is correct: you cannot have a wildcard character in a filename, nor reference a file in that manner.
It sounds like you have version numbers (or dates) appended to the base names of the workbooks in-question. You need to capture the exact name of the workbooks immediately upon opening and save them to a variable. Then you can call them up as needed.
At the start of the macro, you'd add:
Then,immediately after the "BluesFinal..." workbook is opened (presumably from the macro), you'd assign a variable to that name:
When you want to activate either workbook, you'd use these commands instead:
Regards,
-Glenn
It sounds like you have version numbers (or dates) appended to the base names of the workbooks in-question. You need to capture the exact name of the workbooks immediately upon opening and save them to a variable. Then you can call them up as needed.
At the start of the macro, you'd add:
Dim strMortComm as String
Dim strBluesFinal as String
.
.
strMortComm = ActiveWorkbook.Name
Then,immediately after the "BluesFinal..." workbook is opened (presumably from the macro), you'd assign a variable to that name:
strBluesFinal = ActiveWorkbook.Name
When you want to activate either workbook, you'd use these commands instead:
Windows(strMortComm).Activate
.
.
.
Windows(strBluesFinal).Activate
Regards,
-Glenn
Here's a revised sample of your code with the above variables added in context:
Sub sample()
Dim strMortComm, strBluesFinal As String
'insert this statement at the very top of the macro
strMortComm = ActiveWorkbook.Name
Sheets("Daily Commentary").Select
'immediately after opening the BluesFinal-EMEAMortgages... workbook add this statement:
strBluesFinal = ActiveWorkbook.Name
Windows(strBluesFinal).Activate
Sheets("B7_CONS-MORTMP_EMEA_MP, CB, SP").Select
ActiveSheet.Outline.ShowLevels RowLevels:=3
Range("M8", Range("M8").End(xlDown)).Select
Range(Selection, Selection.End(xlToLeft)).Select
Selection.SpecialCells(xlCellTypeVisible).Select
Selection.Copy
Windows(strMortComm).Activate
Range("A11").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Windows(strBluesFinal).Activate
Range("U8", Range("U8").End(xlDown)).SpecialCells(xlCellTypeVisible).Select
Selection.Copy
Windows(strMortComm).Activate
End Sub
Using a function is the correct way to go, you call the function twice in your code replacing the lines where you use the wildcards
and
Windows("BluesFinal-EMEAMortgages" & "*.xlsx").Activate
becomes
SelectMatchingWindow ("BluesFinal-EMEAMortgages")
and
Windows("Mortgages Commentary*.xlsx").Activate
becomes
SelectMatchingWindow ("Mortgages Commentary")
ASKER
Hi, the file called "BluesFinal-EMEAMortgages" has a randon number at the end which changes every day (thus the wildcard) and only recieved via e-mail so I never know the full name of the file so when I click the macro it will be already open everyday so how can I assign a variable to its name without a wildcard? Any ideas? Justin
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Ok, the file name today is called BluesFinal-EmeaMortgages_3 1_Oct_2014 _865841515 .xlsx" and the last 9 numerals change on a daily basis and the other Excel file I have to copy from is called "BluesWIPandBvGVariance-EM EAMortgage s_30_Oct_2 014_182639 827" where the last 9 numerals also change on a dilay basis. They are both opened via Outlook so they have no path. The File that I paste them to and where the Function will reside is called "Mortages Commentary YYYY MM NN. So how will the code be adapted for this?
ASKER
I've requested that this question be closed as follows:
Accepted answer: 0 points for Justincut's comment #a40419288
for the following reason:
Excellent
Accepted answer: 0 points for Justincut's comment #a40419288
for the following reason:
Excellent
If both files are open in excel then you don't need to know the filename, the function that I supplied will set the active window to the required window.
Is there more to your original question than just activating a window using a wildcard.
Is there more to your original question than just activating a window using a wildcard.
ASKER
Hi, they work fine, but I have 1 more problem. One more file I need to copy and paste from, the prefix is variable and thus the Function does not work. Can you adapt the function so this path works:
'Windows("10.29.14 - Daily GMSP Balance Sheet.xlsm (sent).xlsx").Activate
eg. "10.29.14" is Variable and the "Daily GMSP Balance Sheet.xlsm (sent).xlsx" is the ever present name in the path?
'Windows("10.29.14 - Daily GMSP Balance Sheet.xlsm (sent).xlsx").Activate
eg. "10.29.14" is Variable and the "Daily GMSP Balance Sheet.xlsm (sent).xlsx" is the ever present name in the path?
In the function it checks for the string position being 1, if you change this to > 0 then it will find those where the variable part is at the start of the string rather than at the end. Just make the call as before with the parameter "Daily GMSP Balance Sheet"
Windows("Mortgages Commentary*.xlsx").Activat
You can't select a window with a wildcard as if there was more than one your code would not know which to select.
Will there only be one window that matches this file name?