VBA - “运行时错误 424:需要对象”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16644102/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 21:11:28  来源:igfitidea点击:

VBA - “Run-time error 424: object required”

vbaexcel-vbafor-loopvlookupgetelementsbytagname

提问by user2370064

So although I feel the need to mention that I've already made several posts on here regarding this project, I've since made some progress on it:

因此,虽然我觉得有必要提到我已经在这里发表了几篇关于这个项目的帖子,但我已经取得了一些进展:

For those that haven't read any of my prior posts, I have a database of names for an online membership site, for most of which the genders have been assigned with VLOOKUP from a 6,000 name long list on another sheet. For the rest, they come up as “ERR”, which is the point where the VB code would take over and search up the name on a gender guesser website and be able to return an “F”, “M”, or ‘U” (unknown) for those cells. The function I used in the gender column is as follows:

对于那些没有阅读过我之前任何帖子的人,我有一个在线会员网站的姓名数据库,其中大多数性别已经从另一张纸上的 6,000 个姓名长列表中分配了 VLOOKUP。对于其余的,它们出现为“ERR”,这是 VB 代码将接管并在性别猜测网站上搜索名称并能够返回“F”、“M”或“U”的点”(未知)对于这些细胞。我在gender一栏中使用的函数如下:

=IF(ISERROR(VLOOKUP($A41,NameDatabase!$A$2:$B$8000,2,FALSE)),"ERR",VLOOKUP($A41,NameDatabase!$A$2:$B$8000,2,FALSE))

=IF(ISERROR(VLOOKUP($A41,NameDatabase!$A$2:$B$8000,2,FALSE)),"ERR",VLOOKUP($A41,NameDatabase!$A$2:$B$8000,2,FALSE))

And the VBA code to take care of the rest is as follows:

其余的 VBA 代码如下:

Sub DetermineGender()

Dim dbsheet As Worksheet
Set dbsheet = ThisWorkbook.Sheets("memberdata2")

lr = dbsheet.Cells(Rows.Count, 1).End(xlUp).Row

SelRow = Selection.Row

'Gender (Column H)
GenderText = dbsheet.Cells(SelRow, 8)

'Names (Column A)
NamesText = dbsheet.Cells(SelRow, 1)

'Loop Routine
For Row = 2 To lr
If NamesText.Value = "ERR" Then
Dim IE As New InternetExplorer
IE.Visible = True
IE.navigate "http://www.gpeters.com/names/baby-names.php?name=" & NamesText
Do
       DoEvents
Loop Until IE.readyState = READYSTATE_COMPLETE
Dim Doc As HTMLDocument
Set Doc = IE.Document
        If Doc.getElementsByTagName("b").Item(1).innerText = "It's a boy!" Then
            theGenderText.Value = "F"
       ElseIf Doc.getElementsByTagName("b").Item(1).innerText = "It's a girl!" Then
            GenderText.Value = "M"
       Else
            GenderText.Value = "U"
       End If
End If
Next

End Sub

I'm pretty much an absolute beginner at VBA, and most of my code was cobbled together from 4-5 different sources. Although I mentioned that I was getting the “Run-time error 424: object required” message, I am fairly sure that other errors exist in my code, so please feel free to point those out, as well.

我几乎是 VBA 的绝对初学者,我的大部分代码都是从 4-5 个不同来源拼凑而成的。虽然我提到我收到了“运行时错误 424:需要对象”消息,但我相当确定我的代码中存在其他错误,所以也请随时指出这些错误。

采纳答案by user2370064

I got it! It was mostly a matter of changing GenderText and NamesText to range type variables. However, I may have other questions in the future as I add new features to the code. ;)

我知道了!主要是将 GenderText 和 NamesText 更改为范围类型变量。但是,将来在向代码中添加新功能时,我可能会遇到其他问题。;)

回答by Ahmed

If Doc.getElementsByTagName("b").Item(1).innerText = "It's a boy!" Then theGenderText.Value = "F" ElseIf Doc.getElementsByTagName("b").Item(1).innerText = "It's a girl!" Then GenderText.Value = "M"

如果 Doc.getElementsByTagName("b").Item(1).innerText = "这是个男孩!" 然后 theGenderText.Value = "F" ElseIf Doc.getElementsByTagName("b").Item(1).innerText = "这是个女孩!" 然后 GenderText.Value = "M"

Shouldn't theGenderText.Value = "M"(Male) instead of "F"(Female) when it says It's a boy!??? Same observation for ElseIf part...

难道 theGenderText.Value = "M"(Male) 而不是 "F"(Female) 当它说 It's a boy 时!???对 ElseIf 部分的相同观察...

Also in the first part of If statement you are refering theGenderText, but ElseIf's showing GenderText(without "the")...may be this is the reason for error..Also could you mention which line of code cause the error???

同样在 If 语句的第一部分中,您引用的theGenderText,但 ElseIf 显示的是GenderText(没有“the”)……这可能是错误的原因……您还可以提到哪一行代码导致了错误???