在工作表上加载文本文件后自动启动宏

时间:2014-03-20 14:18:43

标签: excel vba excel-vba

我有一个工作表“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

......它运作得很好。

1 个答案:

答案 0 :(得分:0)

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

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"你好"),这样你就可以看到它是否真的触发了,或者只是确实触发了它。没什么'