vba 循环并选择下一个单元格 excel 宏
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15015815/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
loop and select next cell excel macro
提问by Tariq Khalaf
Hello hope all is well :) having trouble figuring out how to loop and select next cell so when a cell in the range h3:z3 is empty it would stop :)
您好,希望一切顺利:) 无法弄清楚如何循环和选择下一个单元格,因此当 h3:z3 范围内的单元格为空时,它将停止:)
and what it is doing is selecting the value from h3 pasteing in b3 runs a another macro which gives an order number in e3 which is then copied and pasted in h4 then it would go to the next cell in I3 paste in b3 copy result from e3 and paste in I4 and do the same
它正在做的是从 h3 中选择值粘贴 b3 运行另一个宏,它在 e3 中给出一个订单号,然后将其复制并粘贴到 h4 中,然后它将转到 I3 中的下一个单元格粘贴 b3 从 e3 复制结果并粘贴在 I4 中并执行相同操作
thanks
谢谢
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
回答by David Zemens
There's a lot that I would probably change in this code. This should get you started.
在这段代码中,我可能会更改很多内容。这应该让你开始。
For starters, a For
loop requires a Next
statement, not a Loop
(which is used for Do
blocks. Also, you should avoid copying/pasting at all costs, in favor of writing values directly to the destination cell(s).
对于初学者,For
循环需要一个Next
语句,而不是 a Loop
(用于Do
块。此外,您应该不惜一切代价避免复制/粘贴,以支持将值直接写入目标单元格。
I assume that cells "B3" and "E3" are constant, and you are iterating over cells in H3:Z3 and computing some value to put in corresponding cell in H4:Z4.
我假设单元格“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