0

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

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

1 回答 1

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"
于 2015-08-13T19:04:33.787 回答