tljenkin
Board Regular
- Joined
- Jun 14, 2007
- Messages
- 147
Hi All,
I am trying to compile a simple code to check ...
1) if a file is open,
2) if it is, run the rest of the code,
3) if it is not, open the file and run the code
4) if nothing is selected at 3), show an error message to that effect and close sub
Please see code below and help me figure out whats wrong
Thank you
Function AlreadyOpen(TargetFile As String) As Boolean
Dim wkb As Workbook
On Error Resume Next
Set wkb = Workbooks(TargetFile)
AlreadyOpen = Not wkb Is Nothing
Set wkb = Nothing
End Function
Sub CopyBudToMRP()
Dim TargetFile As String
TargetFile = Application.GetOpenFilename _
(Title:="Please choose Binary Budget file to open!", _
FileFilter:="Excel Files *.xlsb(*.xlsb),")
If AlreadyOpen(TargetFile) Then
Else
If TargetFile = False Then
MsgBox "No file selected.", vbExclamation, "Sorry!"
Exit Sub
Else
Workbooks.Open Filename:=TargetFile
End If
End If
End Sub
I am trying to compile a simple code to check ...
1) if a file is open,
2) if it is, run the rest of the code,
3) if it is not, open the file and run the code
4) if nothing is selected at 3), show an error message to that effect and close sub
Please see code below and help me figure out whats wrong
Thank you
Function AlreadyOpen(TargetFile As String) As Boolean
Dim wkb As Workbook
On Error Resume Next
Set wkb = Workbooks(TargetFile)
AlreadyOpen = Not wkb Is Nothing
Set wkb = Nothing
End Function
Sub CopyBudToMRP()
Dim TargetFile As String
TargetFile = Application.GetOpenFilename _
(Title:="Please choose Binary Budget file to open!", _
FileFilter:="Excel Files *.xlsb(*.xlsb),")
If AlreadyOpen(TargetFile) Then
Else
If TargetFile = False Then
MsgBox "No file selected.", vbExclamation, "Sorry!"
Exit Sub
Else
Workbooks.Open Filename:=TargetFile
End If
End If
End Sub