Solved

Cut , Paste and Delete data

Posted on 2011-03-13
13
237 Views
Last Modified: 2012-05-11
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
0
Comment
Question by:Cartillo
  • 7
  • 6
13 Comments
 
LVL 30

Expert Comment

by:SiddharthRout
ID: 35127273
>>>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
0
 

Author Comment

by:Cartillo
ID: 35127363
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.
0
 
LVL 30

Expert Comment

by:SiddharthRout
ID: 35128368
Sorry had stepped out.

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

Sid
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:Cartillo
ID: 35129219
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.
0
 
LVL 30

Expert Comment

by:SiddharthRout
ID: 35130197
>>> 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
0
 

Author Comment

by:Cartillo
ID: 35130693
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.  
0
 
LVL 30

Expert Comment

by:SiddharthRout
ID: 35130705
Oh Ok. 1st 500?

Sid
0
 
LVL 30

Expert Comment

by:SiddharthRout
ID: 35130717
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
0
 

Author Comment

by:Cartillo
ID: 35130721
Hi,

Yes, you're right.
0
 

Author Comment

by:Cartillo
ID: 35130827
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.
0
 

Author Comment

by:Cartillo
ID: 35134066
Hi Sid,

Please let me know if this is not giving you better picture.
0
 
LVL 30

Accepted Solution

by:
SiddharthRout earned 500 total points
ID: 35134473
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)
    ws1.Cells.SpecialCells(xlCellTypeBlanks).Delete shift:=xlUp
End Sub

Open in new window


Sid
0
 

Author Closing Comment

by:Cartillo
ID: 35146616
Thanks a lot Sid.
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
Introduction While answering a recent question (http:/Q_27311462.html), I created an alternative function to the Excel Concatenate() function that you might find useful.  I tested several solutions and share the results in this article as well as t…
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question