对象所需的vba

对象所需的vba,vba,Vba,我有下面的代码,其中集合x不起作用。这和集合f一样奇怪。我不知道为什么,我在我的代码的中间,这根本不起作用。在我看来,它和集合f是一样的。你知道为什么吗 Sub Macro2() Dim WsOuput As Worksheet Dim WsScenarios As Worksheet Dim ScenarioIDrow As Long Dim ScenarioIDColumn As Long Dim ScenarioIDinScenarios As Long Dim ScenarioIDin

我有下面的代码,其中集合x不起作用。这和集合f一样奇怪。我不知道为什么,我在我的代码的中间,这根本不起作用。在我看来,它和集合f是一样的。你知道为什么吗

Sub Macro2()

Dim WsOuput As Worksheet
Dim WsScenarios As Worksheet
Dim ScenarioIDrow As Long
Dim ScenarioIDColumn As Long
Dim ScenarioIDinScenarios As Long
Dim ScenarioIDinScenariosC As Long
Dim p As String
Dim q As String
Dim x As Range
Dim z As String
Dim r As String
Dim RgnScenarioOutput As Range
Dim RgnScenarioScenario As Range
Dim Findsomething As Range
Dim FindAgain As Range
Dim lLastRow As Long
Dim f As Range
Dim fAgainAddress As Range

Set WsOutput = Worksheets("Output")
Set WsScenarios = Worksheets("Scenarios.New")
lLastRow = WsOutput.Cells(Rows.Count, "B").End(xlUp).Row
r = WsOutput.Cells(lLastRow, 2).Address(RowAbsolute:=False, ColumnAbsolute:=False)
Range("B22").Select
    Selection.Copy
    Sheets("Scenarios.New").Select
    Columns("A:A").Select

    Set f = Selection.Find(What:=Worksheets("Output").Range("B22").Value, After:=ActiveCell, LookIn:=xlFormulas, LookAt _
        :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
        False, SearchFormat:=False)

    f.Select
    q = f.Address
    Set x = Cells.FindNext(After:=ActiveCell).Activate
    x.Select
    z = x.Address

    Range("F21:M21").Select
    Application.CutCopyMode = False
    Selection.Copy
    Sheets("Output").Select
    Range("AFI35").Select
    Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
        False, Transpose:=True
End Sub

正如Andrew Cheong和Chrismas007所评论的,您的问题在于激活方法

Range.Activate命令不会返回对象,它只是激活某个对象,因此无法将某个对象设置为Range.Activate

您可能想要更改:

Set x = Cells.FindNext(After:=ActiveCell).Activate
致:

你可以用这个

Dim x As Range
Dim f As Range
Set f = Selection.Find(What:=Worksheets("Output").Range("B22").Value, After:=ActiveCell, LookIn:=xlFormulas, LookAt _
        :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
        False, SearchFormat:=False)

    f.Select
    q = f.Address
    Set x = Range("A:A").FindNext(f)
    x.Select

Is Cells.FindNextAfter:=ActiveCell.Activate返回一个对象?Is FindNext.Activate是否激活正确的语法?您可能应该重写所有这些,而不必使用.Select和.Activatethank!各位!你是对的!不喜欢我的语法评论-P@Chrismas007-为我辩护,在你发表评论之前,我开始键入答案-我将修改我的答案,以给你评分@Mat'sMug-我对您所做的编辑很满意,但严格来说,不需要维护原始功能-原始功能无论如何都可以通过Set语句后面的x.Select语句实现。啊,很好的一点-我只是查看了答案中的代码!=。。。请注意,如果x为空,Select将爆炸,所以在那里有空检查仍然很好,只是提醒OP那里可能有运行时错误。@Mat'sMug-这就是为什么我很高兴编辑被保留的原因。实际上,我自己也在考虑添加类似的东西,但后来我决定需要重写所有代码来处理这些问题,我觉得太累了,没有那么多麻烦。@Leandromorera注意到,如果FindNext找不到任何东西,x.Select将引发运行时错误91。另一个答案解决了这个问题。请确保在x返回null/Nothing时正确处理它。如前所述,您可能会遇到错误91.Hi。它没有任何错误。我的想法是设置第一个和第二个的地址。在这种情况下,它们是相同的。如果地址不同,我想复制该行
Dim x As Range
Dim f As Range
Set f = Selection.Find(What:=Worksheets("Output").Range("B22").Value, After:=ActiveCell, LookIn:=xlFormulas, LookAt _
        :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
        False, SearchFormat:=False)

    f.Select
    q = f.Address
    Set x = Range("A:A").FindNext(f)
    x.Select