Loop for each date in the worksheet

Rainmanne

Board Regular
Joined
Mar 10, 2016
Messages
134
Office Version
  1. 2019
Platform
  1. Windows
I've got a worksheet with all the data in 1 column in the following sequence: date, name, data, comment, number, blank repeating for a month. I need to move the data cell and the number cell to the right to the date. So I am trying to write a simple loop:

For each date in the worksheet,
offset 2 cells down, copy
offset 1 column to the right, paste
offset 4 cells down, copy
offset 2 columns to the right, paste
Next date
Until the last date on the worksheet

So I am thinking to write something like this:

Code:
Sub data_move ()
Dim i as Integer
Dim d as Range
For each i
   Find date and make it d
   d.Offset(2,0).Copy
   d.Offset(0,1).Paste
   d.Offset(4,0).Copy
   d.Offset(0,2).Paste
Next i

Not sure if the copy/paste part will work but before that I have stuck with finding each date. Could anyone please help?
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
After you move the data and number to the same row as the date, do you want to:


  1. Delete those rows
  2. Blank out the data and number but keep the rows
  3. Keep those rows as-is


The code you showed takes the third option but I want to make sure before I provide code.
 
Upvote 0
I would try this but I have not created a file from scratch to test it. Generally direct assignments will be faster and cleaner than copy & paste:
Code:
Sub data_move ()

Dim i as Integer
Dim d as Range

i = 1 ' first row containing a date. Change if you have header rows.

Do Until Cells(i, "A") = ""    '  no more dates

   Cells(i, "B") = Cells(i + 2, "A")
   Cells(i, "C") = Cells(i + 4, "A")

   i = i + 6

Loop
However, if you need to also preserve formats then here is a copy/paste solution. You can do a copy & paste all on one line of code if you don't need any Paste Special options.
Code:
Sub data_move ()

Dim i as Integer
Dim d as Range

i = 1 ' first row containing a date. Change if you have header rows.

Do Until Cells(i, "A") = ""    '  no more dates

   Cells(i + 2, "A").Copy Cells(i, "B")
   Cells(i + 4, "A").Copy Cells(i, "C")

   i = i + 6

Loop
 
Upvote 0
Solution
Thanks a lot for that!

I would try this but I have not created a file from scratch to test it. Generally direct assignments will be faster and cleaner than copy & paste:
Code:
Sub data_move ()

Dim i as Integer
Dim d as Range

i = 1 ' first row containing a date. Change if you have header rows.

Do Until Cells(i, "A") = ""    '  no more dates

   Cells(i, "B") = Cells(i + 2, "A")
   Cells(i, "C") = Cells(i + 4, "A")

   i = i + 6

Loop
However, if you need to also preserve formats then here is a copy/paste solution. You can do a copy & paste all on one line of code if you don't need any Paste Special options.
Code:
Sub data_move ()

Dim i as Integer
Dim d as Range

i = 1 ' first row containing a date. Change if you have header rows.

Do Until Cells(i, "A") = ""    '  no more dates

   Cells(i + 2, "A").Copy Cells(i, "B")
   Cells(i + 4, "A").Copy Cells(i, "C")

   i = i + 6

Loop
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,182
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top