【问题标题】:Using an Excel VBA msgbox使用 Excel VBA 消息框
【发布时间】:2021-05-12 10:08:21
【问题描述】:

我运行了这段代码,它可以工作,但是每次 d 都会弹出 MSGBOX。如果回答是,我希望它只运行一次,而不是每次都询问。我怎样才能完成这项工作?

For d = 2 To 10
    destination_folder = Trim(aw.Worksheets(1).Range("A" & d).Value)
    Dim strFileName As String
    Dim strFileExists As String
    
        strFileName = destination_folder & monthandyear
        strFileExists = Dir(strFileName)
    
       If strFileExists = "" Then
            If MsgBox("The file doesn't exist would you like to create one for " & monthandyear & "?", vbYesNo) = vbNo Then Exit Sub
        Else
            If MsgBox("The selected file exists", vbOKOnly) = vb Then Exit Sub
        End If
    
    Set FSO = CreateObject("Scripting.filesystemobject")
    FSO.Copyfile (source_folder & source_file), destination_folder & monthandyear, True
    
    Next
    
    End Sub

【问题讨论】:

    标签: excel vba msgbox


    【解决方案1】:

    将消息框的值存储在变量中并检查该值。 6 = 是,7 = 否

    For d = 2 To 10
        destination_folder = Trim(aw.Worksheets(1).Range("A" & d).Value)
        Dim strFileName As String
        Dim strFileExists As String
        Dim yesno As Long
        
            strFileName = destination_folder & monthandyear
            strFileExists = Dir(strFileName)
            
            If strFileExists = "" Then
                If Not yesno = 6 Then
                    yesno = MsgBox("The file doesn't exist would you like to create one for " & monthandyear & "?", vbYesNo)
                    If yesno = 7 Then Exit Sub
                End If
            Else
                If MsgBox("The selected file exists", vbOKOnly) = vb Then Exit Sub
            End If
        
        Set FSO = CreateObject("Scripting.filesystemobject")
        FSO.Copyfile (source_folder & source_file), destination_folder & monthandyear, True
        
        Next
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-07-22
      • 1970-01-01
      • 2018-02-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多