VBA下一循环不起作用

时间:2018-02-19 21:30:12

标签: excel vba loops



这是我第一次使用VBA。我已经成功创建了一个完成我想要的循环。

然而,当它循环到第二个工作表时," for"似乎不起作用。我需要这个"重置"。

请参阅下面的代码,我希望你能看出它不起作用的原因:

For Each ws In wStock.Sheets
 ws.Activate
 i = 0
   For i = 1 To 4


    FullName = ActiveSheet.Cells(37, ii).Value
    Name = Split(FullName, " ")

    For intCount = LBound(Name) To UBound(Name)
      sData.Range("C" & iii).Value = Name(0)
      sData.Range("D" & iii).Value = Name(1)
    Next

    ActiveSheet.Cells(38, ii).Copy
    sData.Range("F" & iii).PasteSpecial xlPasteValues

  Address = ActiveSheet.Cells(39, ii).Value
  Address1 = Split(Address, ",")

     For intCount = LBound(Address1) To UBound(Address1)
        sData.Range("H" & iii).Value = Address1(0)
        sData.Range("J" & iii).Value = Address1(1)
     Next

    ActiveSheet.Cells(24, ii).Copy
    sData.Range("Y" & iii).PasteSpecial xlPasteValues

    ActiveSheet.Cells(18, ii).Copy
    sData.Range("Z" & iii).PasteSpecial xlPasteValues

    ActiveSheet.Cells(20, ii).Copy
    sData.Range("AA" & iii).PasteSpecial xlPasteValues

    ActiveSheet.Cells(22, ii).Copy
    sData.Range("AB" & iii).PasteSpecial xlPasteValues

    ActiveSheet.Cells(33, ii).Copy
    sData.Range("AC" & iii).PasteSpecial xlPasteValues

    ii = ii + 1
    iii = iii + 1

    Application.CutCopyMode = False
    Application.Wait (Now + TimeValue("0:00:02"))

   Next i
Next ws

我期待很快收到某人的回复:)

1 个答案:

答案 0 :(得分:0)

感谢您的帮助!我设法根据cybernetic.nomad的建议完成了它。我需要在foreach循环中重新初始化ii和iii :)

For Each ws In wStock.Sheets
  ws.Activate
  i = 0
  ii = 2
  iii = iii
  For i = 1 To 4
    FullName = ActiveSheet.Cells(37, ii).Value
    Name = Split(FullName, " ")

    For intCount = LBound(Name) To UBound(Name)
      sData.Range("C" & iii).Value = Name(0)
      sData.Range("D" & iii).Value = Name(1)
    Next

    ActiveSheet.Cells(38, ii).Copy
    sData.Range("F" & iii).PasteSpecial xlPasteValues

    Address = ActiveSheet.Cells(39, ii).Value
    Address1 = Split(Address, ",")

    For intCount = LBound(Address1) To UBound(Address1)
      sData.Range("H" & iii).Value = Address1(0)
      sData.Range("J" & iii).Value = Address1(1)
    Next

    ActiveSheet.Cells(24, ii).Copy
    sData.Range("Y" & iii).PasteSpecial xlPasteValues

    ActiveSheet.Cells(18, ii).Copy
    sData.Range("Z" & iii).PasteSpecial xlPasteValues

    ActiveSheet.Cells(20, ii).Copy
    sData.Range("AA" & iii).PasteSpecial xlPasteValues

    ActiveSheet.Cells(22, ii).Copy
    sData.Range("AB" & iii).PasteSpecial xlPasteValues

    ActiveSheet.Cells(33, ii).Copy
    sData.Range("AC" & iii).PasteSpecial xlPasteValues

    ii = ii + 1
    iii = iii + 1

    Application.CutCopyMode = False
    Application.Wait (Now + TimeValue("0:00:02"))

  Next i
Next ws