Solved

Cut , Paste and Delete data

Posted on 2011-03-13
13
240 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.

691 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