2017-10-17 159 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)。價值

即確實起作用。現在我感到很蠢。