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
Solved

Excel macro cut/paste/delete do loop

Posted on 2013-05-20
2
835 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

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.

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;…
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
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 how to use longer labels with horizontal bar charts instead of the vertical column chart.

861 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