【问题标题】:Excel VBA .find works on first macro run but not secondExcel VBA .find 适用于第一次宏运行但不是第二次
【发布时间】:2017-07-14 07:33:43
【问题描述】:

我打开我的工作簿并运行我的宏,并且所有代码(我知道的)都可以正常执行。在不更改任何数据的情况下,我尝试再次运行宏,但出现错误。该错误专门发生在.Find 上,并返回Nothing,而实际上在指定范围内可以找到日期。

我已经使用了调试工具,但我不知道为什么 .Find 在第二次宏运行而不是第一次运行时返回 Nothing

有问题的行是:

Set DateFind = .Find(what:=TripFind.Offset(0, 2).Value, LookAt:=xlWhole, _
                     MatchCase:=False, SearchFormat:=False)

第一次运行宏DateFind 返回正确的值。第二次运行宏 DateFind 返回 Nothing 但在所有帐户上应该返回与第一次运行宏时相同的值。

这是有问题的完整代码部分: **代码的第一部分运行良好。第二部分以“添加天数...等”为重要部分。*

ElseIf TripCal.Range("A1") = "SESSION 2" Then

        '-----Copies and pastes cabin numbers into tripcal-----
    For TotalRowsOffered = 5 To 168
        If TotalRowsOffered >= Level1Offered And TotalRowsOffered < Level2Offered And TripsOffered.Cells(TotalRowsOffered, Session2) <> "" Then
            a = TotalRowsOffered - 6
            TripCal.Cells(TotalRowsOffered + a, "B") = TripsOffered.Cells(TotalRowsOffered, Session2).Value
        ElseIf TotalRowsOffered >= Level2Offered And TotalRowsOffered < Level3Offered And TripsOffered.Cells(TotalRowsOffered, Session2) <> "" Then
            a = TotalRowsOffered - 8
            TripCal.Cells(TotalRowsOffered + a, "B") = TripsOffered.Cells(TotalRowsOffered, Session2).Value
        ElseIf TotalRowsOffered >= Level3Offered And TotalRowsOffered < Level4Offered And TripsOffered.Cells(TotalRowsOffered, Session2) <> "" Then
            a = TotalRowsOffered - 10
            TripCal.Cells(TotalRowsOffered + a, "B") = TripsOffered.Cells(TotalRowsOffered, Session2).Value
        ElseIf TotalRowsOffered >= Level4Offered And TotalRowsOffered < Level5Offered And TripsOffered.Cells(TotalRowsOffered, Session2) <> "" Then
            a = TotalRowsOffered - 12
            TripCal.Cells(TotalRowsOffered + a, "B") = TripsOffered.Cells(TotalRowsOffered, Session2).Value
        ElseIf TotalRowsOffered >= Level5Offered And TripsOffered.Cells(TotalRowsOffered, Session2) <> "" Then
            a = TotalRowsOffered - 14
            TripCal.Cells(TotalRowsOffered + a, "B") = TripsOffered.Cells(TotalRowsOffered, Session2).Value
        End If
    Next

        '-----Adds day number of trip for each cabin-----
    For TripCounter = 4 To 323
        If TripCal.Cells(TripCounter, "B") = "" Then
                'Skips if there is no trip name accounted for _
                 beside the level on the Trip Calender
        Else
            With TripsOffered.Range(TripsOffered.Cells(5, Session2), TripsOffered.Cells(168, Session2))
                    Set TripFind = .Find(what:=TripCal.Cells(TripCounter, "B"), LookAt:=xlWhole, MatchCase:=False, SearchFormat:=False)
                            If Not TripFind Is Nothing Then
                                Tripdays = TripFind.Offset(0, 4).Value - TripFind.Offset(0, 2).Value
                                    With TripCal.Range(TripCal.Cells(1, 3), TripCal.Cells(1, LastDate))
                                        Set DateFind = .Find(what:=TripFind.Offset(0, 2).Value, LookAt:=xlWhole, MatchCase:=False, SearchFormat:=False)
                                            If Not DateFind Is Nothing Then
                                                For TripDayCount = 0 To Tripdays
                                                    TripCal.Cells(TripCounter, DateFind.Column + TripDayCount) = TripDayCount + 1
                                                Next
                                            Else
                                                MsgBox ("The Trip Date for " & TripFind.Value & " is outside of the current session dates." & vbNewLine & vbNewLine & "Please check the trip dates in the 'Trips Being Offered' sheet for " & TripFind.Value & " in Session 2.")
                                            End If
                                    End With
                            End If
            End With
        End If
        TripCounter = TripCounter + 1
    Next

ElseIf TripCal.Range("A1") = "SESSION 3" Then
'the above code repeat depending on Range "A1"

【问题讨论】:

    标签: excel vba


    【解决方案1】:

    .Find 默认处理活动单元格 - 如果您将该行更改为 Set DateFind = .Find(what:=TripFind.Offset(0, 2).Value, After:=TripsOffered.Range(TripsOffered.Cells(5, Session2), LookAt:=xlWhole, MatchCase:=False, SearchFormat:=False)

    这将检查所有单元格

    【讨论】:

    • 嗨@Jeremy,非常感谢您的快速回复。据我了解,在使用“.Find”行上方的“With”语句时,我指定了一个范围,因此不采用默认值。但是我确实尝试添加“After:=Cells(1,1)”,然后我想出了一个“类型不匹配(错误 13)”
    • 非常感谢您的帮助。我最终没有尝试您的编辑。我确实按照 Docmarti 的建议添加了“LookIn:=xlFormulas”。然而,我第一次尝试你的建议时确实做了更具体的定义,它也没有工作,但我可能做错了什么。再次感谢!继续加油!
    【解决方案2】:

    Range.Find 方法的参数是可选的。但在查找日期时,强烈建议始终设置 Lookin 参数

    Set TripFind = .Find(what:=CDate(TripCal.Cells(TripCounter, "B")), LookIn:=xlFormulas, LookAt:=xlWhole, MatchCase:=False, SearchFormat:=False)
    

    如果您不设置 Find 参数,它将使用现有设置。

    【讨论】:

    • 成功了!我需要添加的只是 LookIn:=xlFormulas,它现在可以在每个宏上运行。看到添加的“CDate”很有趣,因为我认为我以前从未见过,尽管我不需要添加它。我最终使用的代码的最后一行到目前为止工作正常: Set DateFind = .Find(what:=TripFind.Offset(0, 2).Value, LookIn:=xlFormulas, LookAt:=xlWhole, MatchCase:=False , SearchFormat:=False)
    猜你喜欢
    • 1970-01-01
    • 2022-11-26
    • 2012-04-12
    • 1970-01-01
    • 1970-01-01
    • 2021-01-03
    • 1970-01-01
    • 2017-09-09
    • 1970-01-01
    相关资源
    最近更新 更多