[英]Check availability of folder/files
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
我編寫了以下代碼來檢查用戶桌面中的文件夾和該文件夾中的兩個文件,然后更新用戶窗體中的三個復選框。
但是每次我運行它時,無論所述文件夾中文件的可用性如何,我都會得到不同的結果。 該代碼似乎是隨機檢查復選框。
我很難看出代碼有什么問題。 任何幫助,將不勝感激!
RFPath = ""
On Error Resume Next
RFPath = Environ("USERPROFILE") & "\Desktop\REQUIRED FILES\"
On Error GoTo 0
您在字符串變量中創建路徑但不檢查它是否存在。 使用此功能
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
前任:
Debug.Print FileFolderExists(RFPath)
同樣要存儲在 String 變量中,您不需要On Error Resume Next
。 你可以直接做
RFPath = Environ("USERPROFILE") & "\Desktop\REQUIRED FILES\"
我建議使用file scripting object
來避免在循環函數時可能遇到問題的波動:
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.