我如何修复Lmonth循环

问题描述 投票:1回答:3

我有一个月循环代码,它搜索日期列表以查找在1月(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

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

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

excel loops
3个回答
0
投票

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

请参阅Documentation以供参考。它需要是一个约会。

你的主要问题似乎是这样的事实,即你只是从第7行到第5000行,甚至没有检查是否有任何内容。我不认为你可以相信这一事实,表中总有4993个条目。

所以我建议将循环改为像For i = 7 To ActiveSheet.UsedRange.Rows.Count。如果您不确定,也可以在使用“month()”和“IsDate”函数之前检查数据类型。


0
投票

Copy Monthly

  • 我已将变量rapid1更改为字符串。您可能希望更改此设置以使代码正常工作。
  • 虽然实现常量(只改变一次并在一个地方快速改变'(在开头))并适当地命名它们可能会增加其他人的可读性(并且对你来说,一段时间后),但在开发时可能不是这样。因此我在主版本下面加入了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

0
投票

这是令人尴尬的......

我尝试了上面的许多选项都无济于事,然后我将代码粘贴到我的代码中并粘贴了一个我觉得奇怪的工作号,但实际上是代码中断的工作号码。所以我去看了这个领域,发现我输入了该字段的日期为07/02/19 /,最后的正向断路器抛出了代码错误。

删除/并重新运行代码,它完美地工作,没有调试错误。

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

非常感谢!

rookieerror!

© www.soinside.com 2019 - 2024. All rights reserved.