2014-11-03 31 views
0

我試圖將單元格值從一個Excel表格複製到另一個表格中。爲此,我寫了下面的代碼:如何使用vba複製excell單元格值

Dim p As Variant 

For p = 2 To 12000 

If ThisWorkbook.Sheets("Sheet1").Cells(p, 1).Value = ThisWorkbook.Sheets("Sheet2").Cells(p, 7).Value Then 


Sheet1.Cells(p, 9).Value = Sheet2.Cells(p, 7).Value 

End If 


Next p 

比較完成後,只有複製沒有完成......請你幫我解決這個問題。

在此先感謝

回答

0

這是爲我工作:

Dim p As Long 
For p = 2 To 12000 
    If ThisWorkbook.Sheets("Sheet1").Cells(p,1).Value = ThisWorkbook.Sheets("Sheet2").Cells(p,7).Value Then 
     ThisWorkbook.Sheets("Sheet1").Cells(p,9).Value = ThisWorkbook.Sheets("Sheet2").Cells(p,7).Value 
    End If 
Next p 
0

爲什麼不使用這樣的:

ThisWorkbook.Sheets("Sheet1").Cells(p, 9).Value = ThisWorkbook.Sheets("Sheet2").Cells(p, 7).Value 
+0

已經嘗試過這個......沒有運氣雖然 – user3305327 2014-11-03 08:20:09

+0

嗯,我想我的建議與你選擇的@Matteo的答案是一樣的(無論是「For」還是「If」)。所以,它應該工作。我應該提到對這些明顯的代碼行的省略。 – sudipto 2014-11-03 18:34:13