We help IT Professionals succeed at work.

Copy Data

Billa7
Billa7 asked
on
I need Experts help create macro to copy data from Source sheet to Data sheet. If data available in Column I (Source), copy data of the row (Column A,B,C and I) to Data sheet. The copy process should always take the new row at Data sheet, should not overwritten the old data . Hope Experts could help.



Count.xls
Comment
Watch Question

Analyst Programmer
Commented:
You may add the following code to your macro:

Sub CopyData()
    
    Dim i As Integer
    Dim r As Integer
    Dim r2 As Integer
    Dim ss As Worksheet
    Dim sd As Worksheet
    
    Set ss = Sheets("Source")
    Set sd = Sheets("Data")
    r = ss.UsedRange.Rows.Count
    
    For i = 2 To r
        If ss.Cells(i, 9) <> "" Then
            r2 = sd.UsedRange.Rows.Count + 1
            sd.Cells(r2, 1) = ss.Cells(i, 1)
            sd.Cells(r2, 2) = ss.Cells(i, 2)
            sd.Cells(r2, 3) = ss.Cells(i, 3)
            sd.Cells(r2, 4) = ss.Cells(i, 9)
            
            sd.Cells(r2, 1).NumberFormat = "yyyy-m-d h:mm AM/PM"
            sd.Cells(r2, 2).NumberFormat = "hh:mm:ss.00"
        End If
    Next i

End Sub

Open in new window

Author

Commented:
Hi pkwan,

I have tested, the data was not copied to Data sheet.
Count1.xls
Peter KwanAnalyst Programmer

Commented:
I found that the data is copied to much lower (around row 1,900) of the data sheet due to I used "sd.UsedRange.Rows.count" which sets the last used row in the Data sheet.

If you want to put the data to begin at row 2, you need to delete all the data except the header row (row 1).

Author

Commented:
Hi,

Thanks a lot for the help.