1

我有一个 Lmonth 循环代码,它搜索日期列表以查找例如在一月 (1) 中订购的工作,并将这些工作复制并粘贴到新工作表中。

代码运行良好,但当它结束时,它会标记@debug 错误 13'

如果我禁用该行,代码将不起作用,但我无法弄清楚是什么损坏了。

Sub Search_Month()

    Dim datasheet As Worksheet
    Set datasheet = Sheet2
    Dim Mreport As Worksheet
    Set Mreport = Sheet9

    Dim Lmonth As Integer
    Search = Range("m4").Value

    Dim i As Integer

    Mreport.Unprotect Password:=rapid1

    Mreport.Range("a2:a300").ClearContents

    datasheet.Activate

    For i = 7 To 5000

        Lmonth = Month(Cells(i, 6))

        If Lmonth = Search Then

            Range(Cells(i, 2), Cells(i + 3, 2)).Copy
            Mreport.Activate
            Range("A1000").End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues
            datasheet.Activate

        End If

    Next i

    Mreport.Activate

    Mreport.Protect Password:=rapid1

    MsgBox "End of Month Report Updated"

End Sub

标记的行是 Lmonth = Month(Cells(i, 6)) 但我不知道为什么。

宏找到的所有结果都是正确的,只是最后的错误很烦人。我认为它说'下一个搜索行没有显示 lmonth=1 所以我不能再运行代码所以它必须被破坏"

4

3 回答 3

0

每月复制

  • 我已将变量更改rapid1为字符串。您可能想要更改它以使代码正常工作。
  • 尽管实现常量(仅更改一次并在“一个地方”(开始时)快速更改)并适当地命名它们可能会增加其他人的可读性(一段时间后对您来说),但在开发时可能并非如此。因此,我在Main Version下方包含了No Constants Version 。

主要版本

Sub Search_Month()

    ' Data
    Const cSearch As String = "M4"  ' Search Value Cell Range
    Const cFRD As Long = 7          ' First Row Number
    Const cOffset As String = 3     ' Copy Row Offset
    Const cCol As Variant = "F"     ' Search Column Letter/Number
    Const cCopy As Variant = "B"    ' Copy Column Letter/Number
    ' Report
    Const cFRR As Long = 2          ' First Row Number
    Const cWrite As Variant = "A"   ' Write Column Letter/Number
    ' Data
    Dim datasheet As Worksheet  ' Worksheet
    Dim rng As Range            ' Last Cell Range
    Dim Search As Long          ' Search Month
    Dim vntMonth As Variant     ' Current Month
    Dim i As Long               ' Row Counter
    ' Report
    Dim Mreport As Worksheet    ' Worksheet
    Dim FER As Long             ' First Empty Row

    ' Create References to Worksheets
    Set datasheet = Sheet2
    Set Mreport = Sheet9

    ' Speed up
    With Application
        .ScreenUpdating = False
        .Calculation = xlCalculationManual
    End With
    On Error GoTo ProcedureExit

    ' In Data Worksheet
    With datasheet

        ' Assign value from Search Value Cell Range to Search Month.
        Search = .Range(cSearch).Value

        ' In Search Column
        With .Columns(cCol)
            ' Calculate Last Cell Range in Search Column.
            Set rng = .Find("*", , xlFormulas, xlWhole, xlByColumns, xlPrevious)
        End With

        If rng Is Nothing Then ' No data in column (Highly unlikely).
            MsgBox "No Data in column '" _
                    & Split(.Cells(1, cCol).Address, "$")(1) & "'."
            GoTo ProcedureExit
        End If

        ' In Report Worksheet
        With Mreport
            .Unprotect Password:="rapid1"
            ' Clear contents from First Row to bottom cell of Write Column.
            .Cells(cFRR, cWrite).Resize(.Rows.Count - cFRR + 1).ClearContents
            ' Write First Row Number to First Empty Row.
            FER = cFRR
        End With

        ' Loop through cells of Data Worksheet.
        For i = cFRD To rng.Row
            ' Write value of current cell to Current Month.
            vntMonth = .Cells(i, cCol)
            ' Check if Current Month is a date or can be converted to a date.
            If IsDate(vntMonth) Then
                ' Check if month of current cell value is equal to Current Month.
                If Month(vntMonth) = Search Then
                    ' Write data from Data Worksheet to Report Worksheet.
                    Mreport.Cells(FER, cWrite).Resize(cOffset) = _
                            .Cells(i, cCopy).Resize(cOffset).Value
                    FER = FER + cOffset
                End If
            End If
        Next
    End With

    ' In Report Worksheet
    With Mreport
        .Protect Password:="rapid1"
        MsgBox "End of Month Report Updated"
    End With

ProcedureExit:

    ' Speed down
    With Application
        .Calculation = xlCalculationAutomatic
        .ScreenUpdating = True
    End With

End Sub

无常量版本

Sub Search_Month_No_Constants()

    ' Data
    Dim datasheet As Worksheet  ' Worksheet
    Dim rng As Range            ' Last Cell Range
    Dim Search As Long          ' Search Month
    Dim vntMonth As Variant     ' Current Month
    Dim i As Long               ' Row Counter
    ' Report
    Dim Mreport As Worksheet    ' Worksheet
    Dim FER As Long             ' First Empty Row

    ' Create References to Worksheets
    Set datasheet = Sheet2
    Set Mreport = Sheet9

    ' Speed up
    With Application
        .ScreenUpdating = False
        .Calculation = xlCalculationManual
    End With
    On Error GoTo ProcedureExit

    ' In Data Worksheet
    With datasheet

        ' Assign value from Search Value Cell Range to Search Month.
        Search = .Range("M4").Value

        ' In Search Column
        With .Columns("F")
            ' Calculate Last Cell Range in Search Column.
            Set rng = .Find("*", , xlFormulas, xlWhole, xlByColumns, xlPrevious)
        End With

        If rng Is Nothing Then ' No data in column (Highly unlikely).
            MsgBox "No Data in column 'F'." _
            GoTo ProcedureExit
        End If

        ' In Report Worksheet
        With Mreport
            .Unprotect Password:="rapid1"
            ' Clear contents from First Row to bottom cell of Write Column.
            .Cells(2, "A").Resize(.Rows.Count - 2 + 1).ClearContents
            ' Write First Row Number to First Empty Row.
            FER = 2
        End With

        ' Loop through cells of Data Worksheet.
        For i = 7 To rng.Row
            ' Write value of current cell to Current Month.
            vntMonth = .Cells(i, "F")
            ' Check if Current Month is a date or can be converted to a date.
            If IsDate(vntMonth) Then
                ' Check if month of current cell value is equal to Current Month.
                If Month(vntMonth) = Search Then
                    ' Write data from Data Worksheet to Report Worksheet.
                    Mreport.Cells(FER, "A").Resize(3) = _
                            .Cells(i, "B").Resize(3).Value
                    FER = FER + 3
                End If
            End If
        Next
    End With

    ' In Report Worksheet
    With Mreport
        .Protect Password:="rapid1"
        MsgBox "End of Month Report Updated"
    End With

ProcedureExit:

    ' Speed down
    With Application
        .Calculation = xlCalculationAutomatic
        .ScreenUpdating = True
    End With

End Sub
于 2019-02-15T15:00:47.243 回答
0

调试错误 13 是类型不匹配。因此,函数“Month”被赋予了一个无法处理的值。

请参阅文档以供参考。它需要是一个日期。

您的主要问题似乎是,您只是从第 7 行转到第 5000 行,甚至没有检查是否有任何内容。我认为您不能相信表格中始终有 4993 个条目的事实。

所以我建议将循环更改为类似For i = 7 To ActiveSheet.UsedRange.Rows.Count. 如果您不确定,您也可以在使用带有“ IsDate ”功能的“Month()”之前检查数据类型。

于 2019-02-15T12:15:02.677 回答
0

好吧,这很尴尬……

我尝试了上面的许多选项都无济于事,然后我将一些东西粘贴到我的代码中,它粘贴了一个我认为很奇怪的工作编号,但实际上是代码出现故障的工作编号。所以我去看了这个字段,发现我输入了那个字段的日期为 07/02/19/,最后的前向断路器抛出了一个代码错误。

删除 / 并重新运行代码,它运行良好,没有调试错误。

感谢大家的帮助和建议,我将使用您的编码和反馈来改进此代码以及将来更多

再次感谢!

菜鸟错误!

于 2019-02-18T08:11:27.370 回答