Solved

Excel macro cut/paste/delete do loop

Posted on 2013-05-20
2
790 Views
Last Modified: 2013-05-23
Wanting to create a Excel 2007 macro that loops, or auto repeats until it runs out of data to copy/paste.  I manually recorded the code below. It works, but depending on the data size (small sample file attached), I might have 1 iteration or 5000 to process.  

Sub Macro1()
'
' Macro1 Macro
'

'
    Range("B4").Select                   'The pattern is Select (the first step will always be B4)
    Selection.Cut                            'Cut
    Range("H3").Select                   'Select  (a cell up 1 and over 6 in this case H3)
    ActiveSheet.Paste                     'Paste  
    Rows("4:5").Select                    'Select  
    Selection.Delete Shift:=xlUp     'Delete
    Range("B5").Select                   'Repeat until there is no data in the next B cell
    Selection.Cut
    Range("H4").Select
    ActiveSheet.Paste
    Rows("5:6").Select
    Selection.Delete Shift:=xlUp
    Range("B6").Select                 'If empty then stop
End Sub
Sample-File.xlsx
0
Comment
Question by:InfoChase
2 Comments
 
LVL 43

Accepted Solution

by:
Saqib Husain, Syed earned 500 total points
ID: 39181749
I have redone it in another way

Sub macro1()
Range("G3:G" & Range("G" & Rows.Count).End(xlUp).Row).Offset(, 1).FormulaR1C1 = "=r[1]c[-6]"
ActiveSheet.UsedRange.Range("H:H").Value = Range("H:H").Value
ActiveSheet.UsedRange.Range("A:A").SpecialCells(xlCellTypeBlanks).EntireRow.Delete
End Sub

Open in new window

0
 

Author Closing Comment

by:InfoChase
ID: 39191590
Perfect.
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

This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…
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…

932 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

12 Experts available now in Live!

Get 1:1 Help Now