- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这是 Excel 2013 中的 VBA 宏。
我正在循环访问工作表 1 的 B 列中的单元格。对于每个单元格,我想获取其值并在工作表 2 的 A 列中搜索该值。如果找到,我想获取工作表中的相应值2、B 栏并将其放置在工作表 1 的 E 栏相应的行中。
这些表:
Tagging Automatic Categories
B E A B
hat cake Delicious cake.
cake
arm
应该变成:
Tagging Automatic Categories
B E A B
hat cake Delicious cake.
cake Delicious cake.
arm
代码:
Sub AutoCategorize()
Dim c As Range
Dim searchRng As Range
For Each c In Sheets("Tagging").Range("B6:B500").Cells ' loop through cells to do the lookup based on
If Not c.Value Is Nothing Then ' if there is something in the cell
If c.Offset(0, 3).Value Is Nothing Then ' make sure the cell to fill is empty
With Sheets("Automatic Categories").Range("A2:A500") ' using the cells we're looking up in...
Set searchRng = .Find(What:=c.Value) ' find it
If Not searchRng Is Nothing Then ' make sure we've found a thing
If Not searchRng.Offset(0, 1).Value Is Nothing Then ' make sure it has a corresponding entry
Set c.Offset(0, 3).Value = searchRng.Offset(0, 1).Value ' fill it in
End If
End If
End With
End If
End If
Next
End Sub
我认为,我的问题在于我对 Excel-VBA 如何构造数据的理解。不幸的是,MSDN 在这方面确实没有帮助,我只是通过实验才拼凑出很多事情的工作原理。
当我运行代码时,我得到
Run-time error '424': Object required
和调试器突出显示
If Not c.Value Is Nothing Then
谁能解释一下导致错误的原因吗?我很确定我的逻辑是正确的,但正如我所说,我并不是 100% 了解如何引用单元格/数据结构如何工作。
我是 VB 和 Excel 宏的新手,所以如果有更好的方法来构建事物,请大声喊叫。这也是我的第一篇 StackOverflow 帖子,因此如果我做错了什么,请告诉我。
最佳答案
这里的错误是If Not c.Value Is Nothing
正在检查单元格 c 中包含的值是否是一个对象,并且该对象尚未实例化。
由于单元格值是原始类型(实际上是变体),因此要使用的正确检查是
If c.Value <> vbNullString
或
If IsEmpty(c)
您稍后使用Is Nothing
,在 If Not searchRng Is Nothing
是正确的,因为这是检查 Range 对象是否包含 Nothing
.
关于vba - Excel VBA 宏 : "Object required" in "For Each ... In ..." loop,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26628345/
我是一名优秀的程序员,十分优秀!