Opening a file with User Input

D

dswiders

I am saving files based on a number and in the macro I need the user
to input the number for the file they wish to open and have the rest
of the macro continue. Right now I am using an InputBox to have the
user input the information but I dont know how to use the variable
that saves the users information and have it apart of the Open
Filename command. This does not work because that command needs to
have text and it wont let me put a variable in there. Any suggestions?
 
J

Jim Rech

Perhaps...

Sub a()
Dim FileNum As Integer
On Error Resume Next
FileNum = InputBox("Enter file number")
If Err.Number = 0 Then
Workbooks.Open "File" & FileNum
If Err.Number = 0 Then
''file opened ok
Else
MsgBox "Could not open file"
End If
Else
MsgBox "Invalid file number"
End If
End Sub


--
Jim
|I am saving files based on a number and in the macro I need the user
| to input the number for the file they wish to open and have the rest
| of the macro continue. Right now I am using an InputBox to have the
| user input the information but I dont know how to use the variable
| that saves the users information and have it apart of the Open
| Filename command. This does not work because that command needs to
| have text and it wont let me put a variable in there. Any suggestions?
|
 
D

dswiders

Perhaps...

Sub a()
Dim FileNum As Integer
On Error Resume Next
FileNum = InputBox("Enter file number")
If Err.Number = 0 Then
Workbooks.Open "File" & FileNum
If Err.Number = 0 Then
''file opened ok
Else
MsgBox "Could not open file"
End If
Else
MsgBox "Invalid file number"
End If
End Sub

--

|I am saving files based on a number and in the macro I need the user
| to input the number for the file they wish to open and have the rest
| of the macro continue. Right now I am using an InputBox to have the
| user input the information but I dont know how to use the variable
| that saves the users information and have it apart of the Open
| Filename command. This does not work because that command needs to
| have text and it wont let me put a variable in there. Any suggestions?
|

This works great, do you know if there is a way to set a directory so
the program knows where to look for the file the user will specify.
Thank you.
 
R

rdwj

take one step in between. Define a string that contains the filename. Based
on the feedback from the inputbox add the number to the string - the result
is again a string. Thus MyFilename = MyFilename & inputbox
thereafter use the open command
 
Top