2017-10-17 163 views
0

我遇到了一个奇怪的问题,在Excel中从一个工作簿复制数据到另一个。 代码片段:VBA嵌套for循环,只有一个循环前进

For iRow = 7 To iMaxCheckRows 
    strPartNum = thisSheet.Cells(7, 1).Value 
    If strPartNum <> "" Then 
     For iSourceRow = 4 To iMaxRows 
      If sourceBook.Worksheets(1).Cells(iSourceRow, 2).Value = strPartNum Then 
       thisSheet.Cells(iRow, 4).Value = sourceBook.Worksheets(1).Cells(iSourceRow, 4).Value 'here it gets stuck 
       Exit For 
      End If 
     Next iSourceRow 
    End If 
Next iRow 

一切正常,直到第二个if语句条件满足首次。该值被正确复制,但在此之后它卡在这一行上。 iRow递增,但iSourceRow不是,并且直到iRow达到iMaxCheckRows时才会复制相同的值。 我试图用单步调试它,但它与iRow递增保持在同一行。

我从来没有见过这种行为。有人知道这里发生了什么吗?

+5

不应该'strPartNum = thisSheet.Cells(7,1).Value'是'strPartNum = thisSheet.Cells(iRow,1).Value' ??? –

+0

什么是'sourceBook'?它是如何提及的?它打开了吗? – Vityata

+0

是的,strPartNum = thisSheet.Cells(7,1).Value的确应该是strPartNum = thisSheet.Cells(iRow,1).Value - 谢谢! – MarcMan

回答

0

strPartNum = thisSheet.Cells(7,1)。价值的确应strPartNum = thisSheet.Cells(iRow,1)。价值

即确实起作用。现在我感到很蠢。