0

I'm newbie to VBA. I'm trying to find some data in Sheet1 and copy it to Sheet2. When I select the data from Sheet1, I need to append it to that already in Sheet2.

I can find and paste the data using the following code, but I can't append it. What have I done wrong in my code?

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
4
  • What do you mean that you can't copy and paste the data but can't append it? Commented Mar 8, 2013 at 8:05
  • What exactly is not working? I tested your code and it works for me, the data are pasted correctly to target sheet. Commented Mar 8, 2013 at 8:19
  • append means add new data to the sheet2 next empty line when run the macro again and again. Commented Mar 8, 2013 at 10:36
  • What exactly is not working? I tested your code and it works for me, the data are pasted correctly to target sheet. – Daniel Dusek problem is when run the macro again and again last data row vanishes. simply data relevant to "98" in array replaces with "37". Commented Mar 8, 2013 at 11:28

1 Answer 1

1

I guess that 'append' means to add data when you run the macro again and again. It this situation next time you run will overwrite what you have in sheet2. You need to change Rcount variable into:

Rcount = NewSh.Cells(NewSh.Rows.Count,1).End(xlUp).Row+1

to find first empty cell in Sheet2 in column A. Some additional changes will be required if you run the code for the first time (if you really need to have the results in row 1). Check also where to place Rcount increment line- rather should be moved after .copy line

Sign up to request clarification or add additional context in comments.

4 Comments

Actually I cant understand what you explained. I'm new to this thing. Can you explain more further.
according to your comment below the question you asked my idea is fine. Just try to replace your line: Rcount=0 with the code I presented above. As I said, it is not 100% precise but you will see the result and you could make further changes on your own.
yeah I tried Rcount=0. When run the macro again and again it replaces old data by new data.but i need to add new data end of the old data..
so I'll say it again: replace you Rcount = 0 with proposed Rcount = NewSh.Cells(NewSh.Rows.Count,1).End(xlUp).Row+1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.