Reputation: 41
Sub CWSSCheck()
If ActiveSheet.Name = "Position_by_Fixture" Then
Call FileCheck
ElseIf ActiveSheet.Name = "Group_PositionList" Then
MsgBox "This is the Group Position List. Convert the Shelfstock to the old
format using the 'Convert Shelfstock' function and try again.", vbExclamation, "Invalid Format"
Else
MsgBox "This workbook doesn't have a Shelfstock sheet. Please open a valid Shelfstock file and try again.", vbExclamation, "Shelfstock Not Found"
End If
End Sub
Sub FileCheck()
'Check the REQUIRED FILES folder
UserForm1.Show
Dim RFPath As String
Dim UOLPath As String
Dim SOLPath As String
RFPath = ""
On Error Resume Next
RFPath = Environ("USERPROFILE") & "\Desktop\REQUIRED FILES\"
On Error GoTo 0
UOLPath = ""
On Error Resume Next
UOLPath = Environ("USERPROFILE") & "\Desktop\REQUIRED FILES\UPDATED_OUTLET_LIST.xlsx"
On Error GoTo 0
SOLPath = ""
On Error Resume Next
SOLPath = Environ("USERPROFILE") & "\Desktop\REQUIRED FILES\SAP_OUTLET_LIST.xlsx"
On Error GoTo 0
If RFPath = "" Then
UserForm1.CheckBox3.Value = False
Else
UserForm1.CheckBox3.Value = True
End If
If SOLPath = "" Then
UserForm1.CheckBox2.Value = False
Else
UserForm1.CheckBox2.Value = True
End If
If UOLPath = "" Then
UserForm1.CheckBox1.Value = False
Else
UserForm1.CheckBox1.Value = True
End
End Sub
I wrote the following code to check for a folder in the user's desktop and two files inside that folder and then update three checkboxes in a userform.
But every time i run this i get different results regardless the availability of the files in the said folder. The code seems to be checking the checkboxes at random.
I'm having a hard time seeing what's wrong with the code. Any help would be appreciated!
Upvotes: 0
Views: 181
Reputation: 1337
I'd advise using the file scripting object
to avoid the volatility where you can come up against issues if looping the function:
Sub FileCheck()
Dim FSO As Object
Dim CheckForFolder As Boolean, CheckForFile1 As Boolean, CheckForFile2 As Boolean
Set FSO = CreateObject("Scripting.FileSystemObject")
CheckForFolder = False
CheckForFile1 = False
CheckForFile2 = False
'Check the REQUIRED FILES folder
UserForm1.Show
If FSO.FolderExists(Environ("USERPROFILE") & "\Desktop\REQUIRED FILES\") Then CheckForFolder = True ' Checks for the folder. If it exsists, set boolean to "True"
If FSO.FileExists(Environ("USERPROFILE") & "\Desktop\REQUIRED FILES\UPDATED_OUTLET_LIST.xlsx") Then CheckForFile1 = True ' Checks for the 1st file. If it exsists, set boolean to "True"
If FSO.FileExists(Environ("USERPROFILE") & "\Desktop\REQUIRED FILES\SAP_OUTLET_LIST.xlsx") Then CheckForFile2 = True ' Checks for the 2nd file. If it exsists, set boolean to "True"
If CheckForFolder = False Then ' Checks the boolean, asings the checkbox accordingly
UserForm1.CheckBox3.Value = False
Else
UserForm1.CheckBox3.Value = True
End If
If CheckForFile2 = False Then ' Checks the boolean, asings the checkbox accordingly
UserForm1.CheckBox2.Value = False
Else
UserForm1.CheckBox2.Value = True
End If
If CheckForFile1 = False Then ' Checks the boolean, asings the checkbox accordingly
UserForm1.CheckBox1.Value = False
Else
UserForm1.CheckBox1.Value = True
End
Set FSO = Nothing 'Tidy up the memory
End Sub
Upvotes: 1
Reputation: 149295
RFPath = ""
On Error Resume Next
RFPath = Environ("USERPROFILE") & "\Desktop\REQUIRED FILES\"
On Error GoTo 0
You are creating the path in a String Variable but not checking if it exists or not. Use this function
Public Function FileFolderExists(strFullPath As String) As Boolean
On Error GoTo EarlyExit
If Not Dir(strFullPath, vbDirectory) = vbNullString Then FileFolderExists = True
EarlyExit:
On Error GoTo 0
End Function
Ex:
Debug.Print FileFolderExists(RFPath)
Also to store in a String variable you do not need the On Error Resume Next
. You can directly do
RFPath = Environ("USERPROFILE") & "\Desktop\REQUIRED FILES\"
Upvotes: 2