宏仍在运行之后(Msgbox vbyesno)If Then Else语句

时间:2018-11-24 22:23:28

标签: excel vba excel-vba

我在msgbox if / then / else语句中遇到问题,我想有3个选项,首先是如果文件夹中没有子项以提供信息(我做到了),第二个选项是是否有一些选项子选项有“是”(进一步使用宏)和“否”(只是取消操作)选项。但就我而言,是/否都在做同样的事情,Macro正在执行其操作...我尝试了很多方法,但是我没有选择和神经...(我掌握了VBA的基本知识,主要使用google和在这里,我的技能不是那么好-请调整您的编程语言以更好地理解它。

Sub Schaltfläche1_Klicken()
    Dim FileSystem As Object
    Dim HostFolder As String

    ' *** Folder with systems to define (path) ***
    HostFolder = "C:\Users\MirzaV\Desktop\Original"

    ' *** If folder is empty/full message ***
    Dim fs, strFolderPath, oFolder
    Set fs = CreateObject("Scripting.FileSystemObject")
    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual

    strFolderPath = "C:\Users\MirzaV\Desktop\Original" ' *** This is your folder to define ***

    Set oFolder = fs.GetFolder(strFolderPath)
    If (oFolder.SubFolders.Count = 0) Then
        ' * Folder is Empty *
        MsgBox "Folder is empty!", vbOKOnly + vbInformation, "Information!" 
    Else
        ' * Folder isn't empty *
        MsgBox "Folder not empty! Proceed with Macro?", vbYesNo + vbInformation + vbDefaultButton1, "Information!"
    End If

    Set fs = Nothing

    Set FileSystem = CreateObject("Scripting.FileSystemObject")
    DoFolder FileSystem.GetFolder(HostFolder)

    Application.ScreenUpdating = True
    Application.Calculation = xlCalculationAutomatic
End Sub

1 个答案:

答案 0 :(得分:0)

您应该只替换以下行:

MsgBox "Folder not empty! Proceed with Macro?", vbYesNo + vbInformation + vbDefaultButton1, "Information!"

...这两行:

yourAnswer = MsgBox("Folder not empty! Proceed with Macro?", vbYesNo + vbInformation + vbDefaultButton1, "Information!")
If yourAnswer = vbNo Then Exit Sub

第一行存储用户答案-由Integer返回为MsgBox到变量yourAnswer中。第二行If yourAnswer = vbNo将仅Exit Sub