Excel VBA – 对象必需

我有一段时间没有使用Excel VBA,但是我决定挖掘它来构build一些用户友好和可访问的模板来跟踪每月出货量。

这个特定的searchfunction给我头痛,我怀疑这是因为我的语法很生疏。

当我调用一个用户表单的时候,我得到了一个'所需的对象'的错误,但是从我所能看到的,我已经明确地声明和定义了与问题行有关的所有东西。

感谢任何帮助/重温。 我的脸急切地等待着我的手掌。

编辑:当前工作代码张贴在下面的回应

完整的代码

Private Sub UserForm_Initialize() 'Clear result message Me.rResult.caption = "" 'BOL (cached) Dim BOL As Range Set BOL = ThisWorkbook.Sheets("Values").Range("$A$3") 'Addresses (cached) Dim addr As Range Set addr = ThisWorkbook.Sheets("Values").Range("$A$4:$D$21") Dim i As Range 'Search Range Dim srange As Range 'Target Range Dim trange As Range 'First result Dim fr As Range 'Result counter Dim c As Integer c = 0 With ThisWorkbook.ActiveSheet 'Set search range Set srange = .Range(.Cells(7, 6), _ .Cells(.Cells(Rows.Count, 5).End(xlUp).Row, _ .Cells(6, Columns.Count).End(xlToLeft).Column)) 'Find search results in search range Set trange = srange.Find(BOL.Value, LookIn:=xlValues) 'If the cell is not empty and the header in row 6 has a value of "BOL" If Not trange.Value Is Nothing Then '***Problem Line*** If .Cells(6, trange.Value).Value = "BOL" Then 'set first address Set fr = .Range(trange.Address) Do 'result counter c = c + 1 'save each address within cache range For Each i In addr If i.Value = "" Then i.Value = trange.Address Next i Set trange = trange.FindNext(trange) Loop While Not trange Is Nothing And trange.Address <> fr.Address End If End If End With 'Select the first address If c <> 0 Then fr.Select 'Result message Me.rResult.caption = "Search found " & c & _ " result(s) for BOL: " & BOL.Value & "." 'clear cached BOL BOL.Clear End Sub 

我认为这是你在trange结束时的价值。 当你做了一个发现而没有发现什么东西的时候,这个转折就回来了。 然后你要求什么都没有的价值,这是给你的对象所需的错误。

只要改变它If Not trange Is Nothing...但我认为@Abe黄金是正确的,你将需要两个如果陈述。 因为你不能检查'.cells(6,trange.value).value =“BOM”`如果trange是无关紧要的。

对于你的if语句应该是这样的(从上面的@Abe Gold代码)

 If Not trange Is Nothing Then If .Cells(6, trange.Value).Value = "BOL" Then 'set first address Set fr = .Range(trange.Address) Do 'result counter c = c + 1 'save each address within cache range For Each i In addr If i.Value = "" Then i.Value = trange.Address Next i Set trange = trange.FindNext(trange) Loop While trange.Address <> fr.Address End IF End If 

另一个变化。

 If Not trange.Value Is Nothing Then 

应该

 If Not trange Is Nothing Then 

把你的IF重写成两个IF:

  If Not trange.Value Is Nothing Then If .Cells(6, trange.Value).Value = "BOL" Then 'set first address Set fr = .Range(trange.Address) Do 'result counter c = c + 1 'save each address within cache range For Each i In addr If i.Value = "" Then i.Value = trange.Address Next i Set trange = trange.FindNext(trange) Loop While trange.Address <> fr.Address End IF End If 

我知道有一个被接受的答案和两个最有价值的答案,但我都提出了改进,主要是失去了一些东西 (见最后一点)

请考虑这个代码:

 'Find search results in search range Set trange = srange.Find(BOL.Value, LookIn:=xlValues, LookAt:=xlWhole, MatchCase:=False) '<~~always specify those parameters since they are saved from any Find() usage, even that from UI If Not trange Is Nothing Then 'If the cell is not empty and the header in row 6 has a value of "BOL" If .Cells(6, trange.Column).Value = "BOL" Then Set fr = trange '<~~store first trange occurrence found Do c = c + 1 'update result counter addr.SpecialCells(xlCellTypeConstants) = trange.Address '<~~save current address within cache range Set trange = srange.FindNext(trange) '<~~search next occurrence of trange in srange Loop While Not trange.Address <> fr.Address End If End If 

在此总结其与OP的不同之处:

  • Set trange = srange.Find(BOL.Value, LookIn:=xlValues, LookAt:=xlWhole, MatchCase:=False)

    总是指定这些参数,因为它们是从任何Find()用法保存的,甚至是从UI中保存的,所以哟可能会发现自己不自觉地看着stringPart和/或Minding Case Matching

  • If Not trange Is Nothing Then

    好吧,所有的答案和评论已经build立了

  • Set fr = trange

    因为.Range(trange.Address)是多余的

  • addr.SpecialCells(xlCellTypeConstants) = trange.Address

    而不是For Each i In addr循环

  • Set trange = srange.FindNext(trange)

    这里有Set trange = trange FindNext(trange)

    不应该第二次trange是什么?

这个If语句只适用于第一个发现的地址。 我把它移到了循环中以应用于每个迭代结果。

 If .Cells(6, trange.Column).Value = "BOL" Then 

添加了上述回复中列出的其他一些更改/修复。 谢谢!

目前的工作代码片段:

 'Find search results in search range Set trange = srange.Find(BOL.Value, LookIn:=xlValues, LookAt:=xlWhole, MatchCase:=False) 'If the cell is not empy and the header in row 6 has a value of "BOL" If Not trange Is Nothing Then 'set first address Set fr = trange Do If .Cells(6, trange.Column).Value = "BOL" Then 'result counter c = c + 1 'save each address within cache range For Each i In addr If i.Value = "" Then i.Value = trange.Address Exit For End If Next i End If Set trange = srange.FindNext(trange) Loop While Not trange Is Nothing And trange.Address <> fr.Address End If