用户窗体输入后重复宏

Repeat macro after UserForm Input

打开工作簿后,宏将查看单元格 "C27" 是否包含以下任何文本:Location1、Location2、Location3 或 Location4。如果他们这样做,它将继续在这些位置保存模板的 2 个副本文件。如果不是,那么它会打开一个用户窗体 select 从 ComboBox 的正确位置。

如何在用户窗体关闭后重置检查,我尝试在 Unload me 之后调用 Auto_Open 但它没有用。

 Sub Auto_Open()

With Range("B30")
.Value = Time
.NumberFormat = "h-mm-ss AM/PM"
End With

Dim FileName    As String
Dim FilePath    As String
Dim FileDate    As String

Select Case Range("C27").Value

    Case "Location1", "Location2", "Location3", "Location4"

        FilePath = "C:\Users\aholiday\Desktop\FRF_Data_Macro_Insert_Test"
        FileName = Sheets("Data").Range("C27").Text

        Application.DisplayAlerts = False

        ThisWorkbook.SaveAs FileName:=FilePath & "\" & FileName

        Dim FileCopyName    As String
        Dim FileCopyPath    As String
        Dim FileTime        As String

        FilePath = "C:\Users\aholiday\Desktop\Backup"
        FileName = Sheets("Data").Range("C27").Text
        FileTime = Sheets("Data").Range("B30").Text

        ThisWorkbook.SaveAs FileName:=FilePath & "\" & FileName & Space(1) & FileTime & ".xlsx", FileFormat:=xlOpenXMLWorkbook

        MsgBox "File was saved! Ready for Next Test, Please Exit."


    Case Else

        MsgBox "File was not saved, Please Insert The Correct Testing Location"

        UserForm.Show

    Exit Sub

End Select

    Application.DisplayAlerts = True

 End Sub

用户表单

Private Sub UserForm_Initialize()

  'Empty TestLocation Box
   TestLocation.Clear

  'Fill TestLocation Box
  With TestLocation
  .AddItem "Location1"
  .AddItem "Location2"
  .AddItem "Location3"
  .AddItem "Location4"
  End With

End Sub

'--------------------

Private Sub Insert_Click()

Sheets("Data").Activate

Range("C27").Value = TestLocation.Value

End Sub

'--------------------

Private Sub CloseBox_Click()

Unload Me

End Sub

通过对插入按钮使用以下代码:

Private Sub Insert_Click()

   Sheets("Data").Range("C27").Value = TestLocation.Value
   Auto_Open
End Sub

只要您在模块中有 Auto_Open 代码,该代码就可以工作(经过测试)。

如果您将 Auto_Open 子程序放入 ThisWorkbook,然后将其移动到模块中。

然后在ThisWorkbook中使用如下代码:

Private Sub Workbook_Open()
    Auto_Open
End Sub

还有:

Case "Location1", "Location2", "Location1", "Location4"

应该是:

Case "Location1", "Location2", "Location3", "Location4"