vba - 为什么单元格(变量 ,"B")不起作用

标签 vba excel

我创建了一个小程序,我真的不明白为什么我的代码不能工作:O

For Each numberItem In Worksheets("Sheet1").Range("A01:A240").Cells
  Dim c As String
  Dim cpt As Long
  cpt = numberItem
  c = Cells(cpt, "B")    !!!!!ERROR !!!!!

如果我输入 1 而不是 cpt,它就可以工作。
我在网上没有找到好的东西。
是否存在简单的东西?

多谢你们

最佳答案

相信你只需要指定你想要的行:

For Each numberItem In Worksheets("Feuil1").Range("A01:A240").Cells
  Dim c As String
  Dim cpt As Long
  cpt = numberItem.Row
  c = Cells(cpt, "B")
Next

根据@omegastripes 评论更新:

假设循环位于单元格 A10 并且 A10 的值为 37

选项 1:您想使用 numberItem 中的行号:
For Each numberItem In Worksheets("Feuil1").Range("A01:A240").Cells
  Dim c As String
  Dim cpt As Long
  cpt = numberItem.Row
  c = Cells(cpt, "B")
Next

结果:c = Cells(10, "B")
选项 2:您想使用单元格 A10 中的值,然后稍作调整
For Each numberItem In Worksheets("Feuil1").Range("A01:A240").Cells
  Dim c As String
  Dim cpt As Long
  cpt = numberItem.Value
  c = Cells(cpt, "B")
Next

结果:c = Cells(37, "B")

关于vba - 为什么单元格(变量 ,"B")不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36516668/

相关文章:

sql-server - Microsoft Access 错误 : "Because of your security settings and current security policy, this control is disabled"

vba - 过滤所有值并导出与每个值相关的所有行

vba - Excel vba 对象变量或未设置 block 变量错误

excel - 在特定时间运行 VBA 代码

Excel - 根据另一列中的值替换单元格值

python - 将电子表格的列存储在 Python 字典中

Excel 公式 LEN 和替换

excel - 以数字作为文本的高级过滤器

vba - 如何在 VBA 的函数子例程中添加错误消息

vba - 在 Outlook 中使用 VBA 收集收到的电子邮件的统计信息