【问题标题】:Auto Launch Macro After Text File Load on Worksheet在工作表上加载文本文件后自动启动宏
【发布时间】:2014-03-20 14:18:42
【问题描述】:

我有一个工作表“CMReport”,它在工作簿打开时从外部文本文件加载。 我希望能够在工作簿打开时在文件上启动宏,但无法实现。我不确定我做错了什么......确定它很简单(我希望)

我在 VBA ThisWorkbook 中有这个

Private Sub Workbook_Open()
    Run "CMReportCleanUp"
End Sub

此代码位于名为 OnOpen 的模块中

Sub CMReportCleanUp()
    Dim Sheet As Worksheet, Sheet2 As Worksheet, Sheet3 As Worksheet, Sheet4 As Worksheet
    Dim i As Long, j As Long, LRow As Long, R As Long
    Dim CM As String, School As String
    Dim delRng As Range
    Dim LastRow As Integer, LastRow2 As Integer

    Application.ScreenUpdating = False

    Set Sheet = Excel.Worksheets("CMReport")
    Set Sheet2 = Excel.Worksheets("IEPReport")
    Set Sheet3 = Excel.Worksheets("CaseManager")

    'Add CaseManager and School to each row
    With Sheet
        LRow = .Range("A" & .Rows.Count).End(xlUp).Row  'Get last row of Column A.

        'Cells contain "Case Manager"
        For i = 1 To LRow  'Loop through cells in Column A.
            If InStr(1, .Cells(i, 1).Value, "Case Manager", vbTextCompare) Then  'Check to see if the cell contains "Case Manager".
                CM = .Cells(i, 1).Value  'Store the Case Manager's name in a variable.

                'Store the row numbers which have "Case Manager", it will be deleted later.
                If delRng Is Nothing Then
                    Set delRng = .Rows(i)
                Else
                    Set delRng = Union(delRng, .Rows(i))
                End If
            Else
                .Cells(i, 6).Value = CM  'Store the Case Manager in Column F.
            End If

            If InStr(1, .Cells(i, 1).Value, "Elementary", vbTextCompare) Then  'Check to see if the cell contains "Elementary".
                School = .Cells(i, 1).Value  'Store the School in a variable.

                'Store the row numbers which have "Elementary", it will be deleted later.
                If delRng Is Nothing Then
                    Set delRng = .Rows(i)
                Else
                    Set delRng = Union(delRng, .Rows(i))
                End If
            Else
                .Cells(i, 7).Value = School  'Store the School in Column G.
            End If

            If InStr(1, .Cells(i, 1).Value, "Middle", vbTextCompare) Then  'Check to see if the cell contains "Middle".
                School = .Cells(i, 1).Value  'Store the School in a variable.

                'Store the row numbers which have "Middle", it will be deleted later.
                If delRng Is Nothing Then
                    Set delRng = .Rows(i)
                Else
                    Set delRng = Union(delRng, .Rows(i))
                End If
            Else
                .Cells(i, 7).Value = School  'Store the School in Column G.
            End If

            If InStr(1, .Cells(i, 1).Value, "High", vbTextCompare) Then  'Check to see if the cell contains "High".
                School = .Cells(i, 1).Value  'Store the School in a variable.

                'Store the row numbers which have "High", it will be deleted later.
                If delRng Is Nothing Then
                    Set delRng = .Rows(i)
                Else
                    Set delRng = Union(delRng, .Rows(i))
                End If
            Else
                .Cells(i, 7).Value = School  'Store the School in Column G.
            End If

            If InStr(1, .Cells(i, 1).Value, "Academy", vbTextCompare) Then  'Check to see if the cell contains "Academy".
                School = .Cells(i, 1).Value  'Store the School in a variable.

                'Store the row numbers which have "Academy", it will be deleted later.
                If delRng Is Nothing Then
                    Set delRng = .Rows(i)
                Else
                    Set delRng = Union(delRng, .Rows(i))
                End If
            Else
                .Cells(i, 7).Value = School  'Store the School in Column G.
            End If

            If InStr(1, .Cells(i, 1).Value, "Preschool", vbTextCompare) Then  'Check to see if the cell contains "Preschool".
                School = .Cells(i, 1).Value  'Store the School in a variable.

                'Store the row numbers which have "Preschool", it will be deleted later.
                If delRng Is Nothing Then
                    Set delRng = .Rows(i)
                Else
                    Set delRng = Union(delRng, .Rows(i))
                End If
            Else
                .Cells(i, 7).Value = School  'Store the School in Column G.
            End If
        Next i
    End With

    If Not delRng Is Nothing Then delRng.Delete  'Delete the rows which contain "Case Manager"

    Sheet.Cells(1, 6).Value = "Case Manager"  'Assign Case Manager Label to Column Header
    Sheet.Cells(1, 7).Value = "School"  'Assign School Label to Column Header

    Application.ScreenUpdating = True

    'Delete "Case Manager:" from cells
    Cells.Replace What:="Case Manager: ", _
    Replacement:="", _
    LookAt:=xlPart, _
    SearchOrder:=xlByRows, _
    MatchCase:=False, _
    SearchFormat:=False, _
    ReplaceFormat:=False
End Sub

编辑 #1 我在 ThisWorkbook 下设置了这个

Private Sub Workbook_Open()
     Run "Test"
End Sub

这在一个模块下

Sub Test()
    MsgBox Date
    Worksheets("Sheet1").Range("A1").Value = Date
End Sub

...它工作得很好。

【问题讨论】:

  • 尝试用OnOpen.CMReportCleanUp替换Run "CMReportCleanUp"
  • 宏根本不触发?
  • 对...宏根本不触发。如果我将其更改为 OnOpen,我会收到编译错误...
  • Sub CMReportCleanUp() 代码是否与Private Sub Workbook_Open() 位于同一工作簿中?另外,你有没有尝试我的建议?
  • 是...Private Sub Workbook_Open() 位于 ThisWorkbook 下,Sub CMReportCleanUp 位于同一 Workbook 的模块下。我可以运行另一个 MsgBox 宏,就像我有这个设置一样,只是不是这个宏。

标签: vba excel


【解决方案1】:

尽量不要使用明显的变量名,这可能会像 sheet2 一样混淆 excel excel:

Set Sheet = Excel.Worksheets("CMReport")
Set Sheet2 = Excel.Worksheets("IEPReport")
Set Sheet3 = Excel.Worksheets("CaseManager")

试试

Set Sh1 = thisworkbook.sheets("CMReport")
Set Sh2 = thisworkbook.sheets("IEPReport")
Set Sh3 = thisworkbook.sheets("CaseManager")

另外,无需使用 RUN 来调用同一工作簿中的宏

Private Sub Workbook_Open()
CMReportCleanUp
End Sub

除此之外,在 sub 的开头添加一个断点(或一个简单的 msgbox "hello" ),这样您就可以查看它是否真的触发了,或者只是“什么都不做”

【讨论】:

    猜你喜欢
    • 2019-02-06
    • 2013-06-06
    • 2018-03-30
    • 2012-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多