我想知道是否可以(并且如何)创建具有特定宏的Excel模板,以便在我们从测量硬件导出的电子表格中自动使用此宏。
我们将测量值从硬件导出到Excel电子表格中。现在我们必须为每个电子表格编写相同的宏来过滤我们只需要的特定条件。
因此我们希望在其中保存一个带有此宏的Excel模板,从我们的硬件导入Excel电子表格,以便每次都自动过滤条件,以便我们立即使用它。
我们如何安排这个?
答案 0 :(得分:0)
如果假设您的测量硬件始终生成相同的输出文件名...
Sub LoadDataSheet()
Dim sWbkPath As String
sWbkPath = "PATH_TO_FILE\" & "FILE_NAME"
Dim wbkData As Workbook
Set wbkData = Workbooks.Open(sWbkPath)
DataFilteringMacro wbkData 'or sheet
End Sub
在“ThisWorkbook”模块中......
Dim WithEvents App As Application
Private Sub Workbook_Open()
Set App = Application
End Sub
Private Sub App_WorkbookActivate(ByVal Wb As Workbook)
'...
'if using a ribbon, you could put the code in the "GetVisible" callback
'and invalidate the ribbon in the App_WorkbookActivate()
'bVisible is the value set in the ribbon callback
Dim wbk As Workbook
Set wbk = ActiveWorkbook
If wbk.Name = "FILE_NAME" Then
'bVisible = True 'the data file was loaded
Else
'bVisible = False 'another file was loaded
End If
End Sub