![]() |
Excel VBA
Hello:
I have a macro that goes to the My Documents folder and opens a specific file. Is there a way that I can enter a parameter for the file name, so that I can have the macro open any filename that I specify? Thanks for your help. Theresa |
Excel VBA
This will allow you to select a file to open. You'll need to add
error handling. See VBE help for options with GetOpenFilename. Dim strF as String strF = Application.GetOpenFilename Workbooks.Open FileName:=strF HTH Paul -------------------------------------------------------------------------------------------------------------- Be advised to back up your WorkBook before attempting to make changes. -------------------------------------------------------------------------------------------------------------- Hello: I have a macro that goes to the My Documents folder and opens a specific file. Is there a way that I can enter a parameter for the file name, so that I can have the macro open any filename that I specify? Thanks for your help. Theresa |
Excel VBA
Theresa,
Here's some code ('stolen' from this group) that allows opening multiple files from a folder. (check the notes...) The code can be shortened to use input boxes or forms for the path and file name variables. Sub OpenMyFile() Dim GetFiles As Variant Dim iFiles As Long Dim nFiles As Long Dim pth As String Dim wbnm As String 'Open *.xls Application.DisplayAlerts = False Application.ScreenUpdating = False On Error Resume Next pth = ActiveWorkbook.path ' sets directory to current book path ' change pth as needed... ChDir pth On Error GoTo 0 GetFiles = Application.GetOpenFilename _ (FileFilter:="Excel Files (*.xls),*.xls", _ Title:="Select Files To Open", MultiSelect:=True) ' allows selection of multiple files If TypeName(GetFiles) = "Boolean" Then ''' GetFiles is False if GetOpenFileName is Canceled MsgBox "No Files Selected", vbOKOnly, "Nothing Selected" End Else '' GetFiles is Array of Strings (File Names) '' File names include Path nFiles = UBound(GetFiles) For iFiles = 1 To nFiles ' List Files in Immediate Window Workbooks.Open FileName:=GetFiles(iFiles) Next End If Application.DisplayAlerts = True Application.ScreenUpdating = True End Sub -- sb "Theresa" wrote in message ... Hello: I have a macro that goes to the My Documents folder and opens a specific file. Is there a way that I can enter a parameter for the file name, so that I can have the macro open any filename that I specify? Thanks for your help. Theresa |
All times are GMT +1. The time now is 05:17 PM. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com