尝试在范围内查找值并获取其行。变量未设置错误?

huangapple go评论71阅读模式
英文:

Trying to find value in range and get its row. Variable not set error?

问题

以下是代码中需要翻译的部分:

The code below is supposed to take the value for net in each month, copies it, search for net name in range1(another worksheet) and pastes value in the cell corresponding to that row and column "AA".

This part of code is having issue:
Set Netrng = Range("AA" & Range1.Find(What:=Net, After:=ActiveCell, LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
        False, SearchFormat:=False).Rows.row)

The error is -

object variable or with block variable not set.

what am I doing wrong?


Sub test()

    Dim Range2 As Range
    Dim lRow As Long
    Dim Count As Long
    Dim Net As String
    Dim Line As Range
    Dim Netrng As Range
    Dim First As Range
    Dim Range1 As Range
    Dim wb As Worksheet

    Set First = ActiveCell
    Set wb = ActiveSheet
    Set Range1 = wb.Range(First, First.End(xlDown))
    ActiveWindow.ActivatePrevious
    
    ActiveSheet.PivotTables("PivotTable1").PivotFields("Client Code").CurrentPage _
        = "BUN"
    
    ActiveSheet.Range("B5").Activate
     lRow = Cells(Rows.Count, 1).End(xlUp).Row - 6
     Set Range2 = Range(ActiveCell.Offset(2, -1), ActiveCell.Offset(lRow, -1))
     Set Months = Range(ActiveCell.Offset(0, 0), ActiveCell.Offset(0, 2))
     
    

       Count = 1
       While Count <= Range2.Count
       Set Line = Range2.Rows(Count)
       Net = Line.Value
       Line.Offset(0, 1).Copy
       ActiveWindow.ActivatePrevious
       Set Netrng = Range("AA" & Range1.Find(What:=Net, After:=ActiveCell, LookIn:=xlValues, LookAt _
       :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
        False, SearchFormat:=False).Row)
       Netrng.Offset(0, 4).PasteSpecial Paste:=xlPasteValues
       Netrng.Value = 0
       ActiveWindow.ActivatePrevious
       Line.Offset(0, 2).Copy
       ActiveWindow.ActivatePrevious
       Netrng.Offset(0, 8).PasteSpecial Paste:=xlPasteValues
       ActiveWindow.ActivatePrevious
       Count = Count + 1
       Wend

End Sub
英文:

The code below is supposed to take the value for net in each month, copies it, search for net name in range1(another worksheet) and pastes value in the cell corresponding to that row and column "AA".

This part of code is having issue:

Set Netrng = Range(&quot;AA&quot; &amp; Range1.Find(What:=Net, After:=ActiveCell, LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
        False, SearchFormat:=False).Rows.row)

The error is -

> object variable or with block variable not set.

what am I doing wrong?

Sub test()

    Dim Range2 As Range
    Dim lRow As Long
    Dim Count As Long
    Dim Net As String
    Dim Line As Range
    Dim Netrng As Range
    Dim First As Range
    Dim Range1 As Range
    Dim wb As Worksheet

    Set First = ActiveCell
    Set wb = ActiveSheet
    Set Range1 = wb.Range(First, First.End(xlDown))
    ActiveWindow.ActivatePrevious
    
    ActiveSheet.PivotTables(&quot;PivotTable1&quot;).PivotFields(&quot;Client Code&quot;).CurrentPage _
        = &quot;BUN&quot;
    
    ActiveSheet.Range(&quot;B5&quot;).Activate
     lRow = Cells(Rows.Count, 1).End(xlUp).row - 6
     Set Range2 = Range(ActiveCell.Offset(2, -1), ActiveCell.Offset(lRow, -1))
     Set Months = Range(ActiveCell.Offset(0, 0), ActiveCell.Offset(0, 2))
     
    

       Count = 1
       While Count &lt;= Range2.Count
       Set Line = Range2.Rows(Count)
       Net = Line.Value
       Line.Offset(0, 1).Copy
       ActiveWindow.ActivatePrevious
       Set Netrng = Range(&quot;AA&quot; &amp; Range1.Find(What:=Net, After:=ActiveCell, LookIn:=xlValues, LookAt _
       :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
        False, SearchFormat:=False).row)
       Netrng.Offset(0, 4).PasteSpecial Paste:=xlPasteValues
       Netrng.Value = 0
       ActiveWindow.ActivatePrevious
       Line.Offset(0, 2).Copy
       ActiveWindow.ActivatePrevious
       Netrng.Offset(0, 8).PasteSpecial Paste:=xlPasteValues
       ActiveWindow.ActivatePrevious
       Count = Count + 1
       Wend

End Sub

答案1

得分: 2

现在的代码假设Find操作是成功的,但这并不总是情况。

测试代码如下:

Dim foundRng as Range
Set foundRng = Range1.Find(What:=Net, After:=ActiveCell, LookIn:=xlValues, _ 
    LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
    MatchCase:=False, SearchFormat:=False)

If Not FoundRng is Nothing Then
    Set Netrng = Range("AA" & foundRng.Row)
    ...
End If

其他建议:

  1. 避免使用SelectActivate(以及ActiveCellActiveWindow以及任何Active相关的操作)。
  2. 完全指定每个Range所在的WorkbookWorksheet(关于避免使用Select的回答中有相关信息)。
  3. While...Wend是过时的。使用For Each循环。
英文:

As is, the code is assuming that the Find is successful, which may not always be the case.

To test:

Dim foundRng as Range
Set foundRng = Range1.Find(What:=Net, After:=ActiveCell, LookIn:=xlValues, _ 
    LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
    MatchCase:=False, SearchFormat:=False)

If Not FoundRng is Nothing Then
    Set Netrng = Range(&quot;AA&quot; &amp; foundRng.Row)
    ...
End If

Other recommendations:

  1. Avoid using Select and Activate. (and ActiveCell, ActiveWindow, anything Active).
  2. Fully qualify which Workbook and Worksheet each Range is on (helpful reading in the answer on avoiding Select).
  3. While...Wend is old-fashioned. Use a For Each loop.

huangapple
  • 本文由 发表于 2020年1月3日 23:26:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/59581144.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定