在UserForm输入后重复宏

时间:2015-08-13 17:48:53

标签: excel vba excel-vba userform select-case

打开工作簿后,宏将查看是否有单元格" C27"包含以下任何文本:Location1,Location2,Location3或Location4。如果他们这样做,那么它将继续按这些位置保存模板的2个复制文件。如果没有,那么它将打开UserForm从ComboBox中选择正确的位置。

如何在UserForm关闭后重置检查,我在卸载后尝试调用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

1 个答案:

答案 0 :(得分:2)

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

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"