循环并选择下一个单元格excel宏

时间:2013-02-22 02:22:35

标签: excel vba

您好希望一切顺利:) 难以弄清楚如何循环和选择下一个单元格 所以当h3:z3范围内的单元格为空时,它会停止:)

它正在做什么是选择h3中的值粘贴b3运行另一个宏,它在e3中给出一个订单号,然后在h4中复制并粘贴然后它将转到b3副本中的I3粘贴中的下一个单元格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

1 个答案:

答案 0 :(得分:2)

我可能会在此代码中更改很多内容。这应该可以帮到你。

对于初学者来说,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
相关问题