简体   繁体   中英

VBA Next Loop Not Working



This is the first time i have used VBA. I have successfully created a loop that completes what i want it too.

However when it loops to the second worksheet the "for" doesn't appear to work. I need this to be "reset".

Please see the code below, i hope you can see why its not working:

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

I look forward to hearing from someone soon :)

Thanks for all your help! I managed to complete it now with the suggestion from cybernetic.nomad. I needed to reinitialise the ii and iii in the foreach loop :)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM