Jack
Member
Hi everyone. I have written some code below that works after much trial and error but it worries me that I don't understand why. This code is to allow the user to select the right .xls file to open from a defined folder, first test if the chosen file is already open and then either open it or ignore opening it if already is open so a copy and paste routine (which I haven't bothered showing below) can then proceed.
The problems I had were:
1) I couldn't work out whether vWB which is the result of the GetOpenFilename should be a string or a workbook so I gave up and made it a variant - should it have been declared as a workbook or a string or is it both during the routine and so variant is right?
2) Testing if the workbook was already open I think I got this bit from a Chip Pearson post somewhere - I don't get why it's not wWB.Fullname = vWB.Fullname but through trial and error it worked if I drop the .Fullname from vWB. I guess that means vWB is not actually an object which I am confused about as I thought it was formed by the use of the set command a little earlier on and as its a variant it can be an object if need be.
thanks
John
Sub ImportStaffHrsEntryData()
On Error Resume Next
Dim sWBPath As String
Dim wWB As Workbook
Dim vWB As Variant
Dim IsWBOpen As Boolean
Dim pw As String
Worksheets("CONTROL").Activate
sWBPath = Range("SourceWBPath")
ChDrive "S"
ChDir sWBPath
vWB = Application.GetOpenFilename(FileFilter:="Excel Files (*.xls), *.xls", Title:="Please select a file")
If vWB = False Then
' They pressed Cancel
MsgBox "Stopping because you did not select a file or pressed cancel"
Exit Sub
Else
Set vWB = Workbooks(vWB)
For Each wWB In Workbooks
If wWB.FullName = vWB Then
IsWBOpen = True
Exit For
End If
Next wWB
If IsWBOpen = False Then
Workbooks.Open vWB
End If
End If
'rest of code
end sub
The problems I had were:
1) I couldn't work out whether vWB which is the result of the GetOpenFilename should be a string or a workbook so I gave up and made it a variant - should it have been declared as a workbook or a string or is it both during the routine and so variant is right?
2) Testing if the workbook was already open I think I got this bit from a Chip Pearson post somewhere - I don't get why it's not wWB.Fullname = vWB.Fullname but through trial and error it worked if I drop the .Fullname from vWB. I guess that means vWB is not actually an object which I am confused about as I thought it was formed by the use of the set command a little earlier on and as its a variant it can be an object if need be.
thanks
John
Sub ImportStaffHrsEntryData()
On Error Resume Next
Dim sWBPath As String
Dim wWB As Workbook
Dim vWB As Variant
Dim IsWBOpen As Boolean
Dim pw As String
Worksheets("CONTROL").Activate
sWBPath = Range("SourceWBPath")
ChDrive "S"
ChDir sWBPath
vWB = Application.GetOpenFilename(FileFilter:="Excel Files (*.xls), *.xls", Title:="Please select a file")
If vWB = False Then
' They pressed Cancel
MsgBox "Stopping because you did not select a file or pressed cancel"
Exit Sub
Else
Set vWB = Workbooks(vWB)
For Each wWB In Workbooks
If wWB.FullName = vWB Then
IsWBOpen = True
Exit For
End If
Next wWB
If IsWBOpen = False Then
Workbooks.Open vWB
End If
End If
'rest of code
end sub