我是VBA的新手。我試圖在Sheet1中找到一些數據並將其複製到Sheet2。當我從Sheet1中選擇數據時,我需要將它附加到已經在Sheet2中的數據。VBA將數據添加到Excel 2010中的最後一行
我可以使用下面的代碼找到並粘貼數據,但我無法追加它。我在代碼中做了什麼錯誤?
Sub Copy_To_Another_Sheet_1()
Dim FirstAddress As String
Dim MyArr As Variant
Dim Rng As Range
Dim Rcount As Long
Dim i As Long
Dim NewSh As Worksheet
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
MyArr = Array("37", "283", "300", "112", "1100", "336", "98")
Set NewSh = Sheets("Sheet2")
With Sheets("Sheet1").Range("B5:B500")
Rcount = 0
For i = LBound(MyArr) To UBound(MyArr)
Set Rng = .Find(What:=MyArr(i), _
After:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
If Not Rng Is Nothing Then
FirstAddress = Rng.Address
Do
Rcount = Rcount + 1
Rng.EntireRow.Copy NewSh.Range("A" & Rcount)
Set Rng = .FindNext(Rng)
Loop While Not Rng Is Nothing And Rng.Address <> FirstAddress
End If
Next i
End With
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
End Sub
之後移動,這是什麼意思,您不能複製和粘貼數據,但不能附加它? – 2013-03-08 08:05:29
什麼是不工作?我測試了你的代碼,它適用於我,數據被正確地粘貼到目標表單上。 – dee 2013-03-08 08:19:11
追加意味着當一次又一次運行宏時,將新數據添加到sheet2下一個空行。 – 2013-03-08 10:36:38