Solved

Cut , Paste and Delete data

Posted on 2011-03-13
13
235 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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,…
How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

867 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now