【问题标题】:loop and select next cell excel macro循环并选择下一个单元格excel宏
【发布时间】:2013-02-22 02:22:34
【问题描述】:

你好希望一切都好:) 无法弄清楚如何循环和选择下一个单元格 因此,当 h3:z3 范围内的单元格为空时,它将停止:)

它正在做的是从 h3 中选择值粘贴到 b3 运行另一个宏,该宏在 e3 中给出一个订单号,然后将其复制并粘贴到 h4 中,然后它将转到 I3 中的下一个单元格粘贴到 b3 副本中从 e3 得到结果并粘贴到 I4 中并执行相同的操作

谢谢

For Each cell In Range("H3:Z3")

If IsEmpty(cell.Value) Then Exit For
'select ammount and place in lookup
Range("H3").Select
Selection.Copy
Range("B3").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False

' fill in order numbers
bulkON_Click

'select order and past under postcode
Range("E3").Select
Application.CutCopyMode = False
Application.CutCopyMode = False
Selection.Copy
Range("H4").Select
ActiveSheet.Paste

Loop

【问题讨论】:

  • 你有什么问题?

标签: excel vba


【解决方案1】:

我可能会在此代码中更改很多内容。这应该可以帮助您入门。

对于初学者,For 循环需要 Next 语句,而不是 Loop(用于 Do 块。此外,您应该不惜一切代价避免复制/粘贴,以支持写入值直接到目标单元格。

我假设单元格“B3”和“E3”是恒定的,并且您正在迭代 H3:Z3 中的单元格并计算一些值以放入 H4:Z4 中的相应单元格。

For Each Cell In Range("H3:Z3")

    If Cell.Value = vbNullString Then Exit For
    'select ammount and place in lookup
    Range("B3").Value = Cell.Value  '<< no need to "copy & paste", just write the value directly
    ' fill in order numbers
    bulkON_Click

    'insert the value under postcode
    ' this OFFSET refers to the cell 1 row below the "Cell"
    Cell.Offset(1, 0).Value = Range("E3").Value

Next

【讨论】:

  • 非常感谢,但你知道我的代码有很多复制粘贴,这只是其中的一小部分,我必须尝试修复它
  • 我对如何获取数据感到困惑选择删除重复项然后转置我发布了一个问题stackoverflow.com/questions/15018896/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-16
  • 2010-09-26
  • 1970-01-01
  • 2014-12-25
  • 2021-10-31
  • 2014-08-01
相关资源
最近更新 更多