How to loop through cells in macro

A

Andrew

I have a basic macro (compliments of macro builder) but
want to repeat the same steps x times.

My macro is cutting, pasting, and deleting rows in a
prospect database. For each contact I have 4 rows of data
and want to consolidate to 1 row, and then delete the
three blank rows. Then move onto the next contact record.

How do I loop the steps or use the Dim counter to cycle
through 1000 rows?

I'd appreciate any good resources for a beginner.

Thanks in advance.
Andrew
 
T

Tom Ogilvy

Assuming your data is in column A, rows 1 to 1000

Sub ConsolidateData()
Dim i As Long
Dim rng As Range
For i = 1 To 1000 Step 4
Cells(i, 2).Value = Cells(i + 1, 1).Value
Cells(i, 3).Value = Cells(i + 2, 1).Value
Cells(i, 4).Value = Cells(i + 3, 1).Value
If rng Is Nothing Then
Set rng = Cells(i, 1).Offset(1, 0).Resize(3, 1)
Else
Set rng = Union(rng, Cells(i, 1).Offset(1, 0).Resize(3, 1))
End If
Next
rng.EntireRow.Delete
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top