Link to home
Start Free TrialLog in
Avatar of Cartillo
CartilloFlag for Malaysia

asked on

Cut , Paste and Delete data

Hi Experts,

I would like to request Experts help create a macro to cut and paste data from Sheet3 (row2 to row 500)  to Sheet 2. After the data being processed using “Sub CopyData()”, the output at “Matched” sheet need to be copied over at “Completed” sheet and delete Sheet2 data automatically. Need to loop this process until the Hope Experts will help me to create this feature. Attached the workbook for Experts perusal.



CopyDataTest.xls
Avatar of SiddharthRout
SiddharthRout
Flag of India image

>>>I would like to request Experts help create a macro to cut and paste data from Sheet3 (row2 to row 500)  to Sheet 2.

Try this. Paste this in a module.

Sub CopyDataS3S2()
    Dim ws1 As Worksheet, ws2 As Worksheet
    Dim ws1LastRow As Long, ws2LastRow As Long
    Dim i As Long
    
    Set ws1 = Sheets("Sheet3")
    Set ws2 = Sheets("Sheet2")
    
    ws1LastRow = ws1.Range("A" & Rows.Count).End(xlUp).Row
    ws2LastRow = ws2.Range("A" & Rows.Count).End(xlUp).Row + 1
    
    ws1.Rows("2:" & ws1LastRow).Cut _
    ws2.Rows(ws2LastRow)
End Sub

Open in new window


Looking at the rest of your requirements.

Sid
Avatar of Cartillo

ASKER

Hi Sid,

Can we fixed the macro only cut and paste 500rows each time I run the sub. This is because I’m having more than 60,000 (millions data) rows. I need to run the ““Sub CopyData()” first before rerun the “Sub CopyDataS3S2()” Hope this is possible.
Sorry had stepped out.

Which macro do you want to fix? Sub CopyData()?

Sid
Hi Sid,

Sorry for the confusion. Need to refine "Sub CopyDataS3S2" to allow Cut and Paste data from row 1 to row 500 only when each time I run the code.
>>> Need to refine "Sub CopyDataS3S2" to allow Cut and Paste data from row 1 to row 500 only when each time I run the code.

Ok in that case paste the code that I gave in a module and then in your Sub CopyData() just add this one line at the end before End Sub

Sub CopyData()
    Dim strTmp As String
    Dim strTmp1 As String
    'Build the data dictionary
    Dim oDicTmp As Object
    Set oDicTmp = CreateObject("Scripting.Dictionary")

    '~~> Rest of the code remains as it is
    
    '~~> Add this line
    CopyDataS3S2
End Sub

Sub CopyDataS3S2()
    Dim ws1 As Worksheet, ws2 As Worksheet
    Dim ws1LastRow As Long, ws2LastRow As Long
    Dim i As Long
    
    Set ws1 = Sheets("Sheet3")
    Set ws2 = Sheets("Sheet2")
    
    ws1LastRow = ws1.Range("A" & Rows.Count).End(xlUp).Row
    ws2LastRow = ws2.Range("A" & Rows.Count).End(xlUp).Row + 1
    
    ws1.Rows("2:" & ws1LastRow).Cut _
    ws2.Rows(ws2LastRow)
End Sub

Open in new window


Hope I have understood you correctly.

Sid
Hi Sid,

I think I just don't know how to put it in a simple words for you to understand. Sorry for that. What I need is to limit the cut and paste function up to 500 lines only from sheet 3 to sheet 2.  
Oh Ok. 1st 500?

Sid
If yes, then try this

Sub CopyDataS3S2()
    Dim ws1 As Worksheet, ws2 As Worksheet
    Dim ws1LastRow As Long, ws2LastRow As Long
    Dim i As Long
    
    Set ws1 = Sheets("Sheet3")
    Set ws2 = Sheets("Sheet2")
    
    ws1LastRow = ws1.Range("A" & Rows.Count).End(xlUp).Row
    ws2LastRow = ws2.Range("A" & Rows.Count).End(xlUp).Row + 1
    
    ws1.Rows("2:501").Cut ws2.Rows(ws2LastRow)
End Sub

Open in new window


Sid
Hi,

Yes, you're right.
Hi Sid,

How to set Sheet 3 data is "Shift:=xlUp" after cut the data (prevent any empty rows). This is to allow me to copy the same data row again.
Hi Sid,

Please let me know if this is not giving you better picture.
ASKER CERTIFIED SOLUTION
Avatar of SiddharthRout
SiddharthRout
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks a lot Sid.