如何为for语句中的行select列值?
谢谢你过去提出的一些问题,我还有一个问题是我做了大量的研究,没有find答案。
我试图通过表中的行循环,并select某一列的值,在这一点上,我想要做的所有的价值是把它放在一个MsgBox,但我的企图一再失败。
Dim id As Range Dim trtype As Range set id = Activesheet.Range("Table1[Column1]") For Each r In id r.Select Set trtype = .Range(Cells(.ActiveRow, "Column5")) MsgBox trtype.Value Next r
这将返回“types不匹配”行trtype = .Range(Cells(.ActiveRow, "Column5")
突出显示。
任何想法如何使这项工作?
你错过了SET这个词:
trtype = .Range(Cells(.ActiveRow, "Column5"))
trtype是一个对象variables(即指向一个内置Excel对象的variables)。 VBA规则是您必须使用SET来分配它。
多年来,这是我最常见的VBA错误!
我通过执行以下操作来实现它:
For Each r In id r.Select trtype = Selection.Text MsgBox trtype Next r
感谢让我在正确的轨道上回答关于设置,这引导我讨论什么时候使用set和发现它只是为了对象,并且string必须设置.Text
Not .Value