2013-02-22 60 views
0

您好希望一切都很好:) 遇到问题搞清楚如何循环并选择下一个单元格 所以当范围H3细胞:Z3是空的,将停止:)循环,并选择下一个单元格Excel宏

和它正在做的是从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 
+2

你的问题是什么? – Joe 2013-02-22 02:33:28

回答

2

在这段代码中我可能会改变很多。这应该让你开始。

对于初学者来说,一个For循环需要一个Next声明,而不是一个Loop(这是用于Do块。此外,你应该避免复制/粘贴,不惜一切代价,有利于把值直接目的小区(S )

我认为细胞“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 
+0

哇哇多数民众赞成 – 2013-02-22 06:38:16

+0

谢谢sooo多,但你kn ow我的代码有很多复制粘贴这只是它的一小部分生病必须尝试修复它全部up – 2013-02-22 06:40:42

+0

即时通讯混淆如何获得数据选择删除重复,然后转置它我发表了一个问题 http: //stackoverflow.com/questions/15018896/optimizing-vba-code-grouping-postcodes-and-returning-amount – 2013-02-22 07:22:07