Excel VBA - needs to loop from Start Date to X days and print each date

mk000

Board Regular
Joined
Dec 10, 2004
Messages
234
Hi Excel Pros and forum members... Thank you all in advance!

I am looking for a solution to the following. I have two Sheets in my Book, Sheet1 and Sheet2. Sheet1 contains a list of people with a Begin date and number of days like so...
ABC
NameBegin DateDays
Mike1/1/201920
Todd5/15/201911
Steve10/20/201990

What i want a vba macro to do is to Loop through each person in column A, and using the start date, make a row for that person in sheet2 starting with the begin date, and however many days in Column C...

Result on Sheet2 would look like...


NameDate
Mike1/1/2019
Mike1/2/2019
Mike1/3/2019

Mike
... to 20 days
Todd5/15/2019
Todd
5/16/2019

Todd

5/17/2019
Todd... to 11 days
Steve10/20/2019 ... to 90 days....
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Hi mk000,

Try this:

VBA Code:
Option Explicit
Sub Macro1()

    Dim lngMyRow As Long
    Dim lngLastRow As Long
    Dim lngPasteRow As Long
    Dim wsSource As Worksheet
    Dim wsDestin As Worksheet
    
    Application.ScreenUpdating = False
    
    Set wsSource = ThisWorkbook.Sheets("Sheet1")
    Set wsDestin = ThisWorkbook.Sheets("Sheet2")
    
    lngLastRow = wsSource.Range("A:C").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    
    For lngMyRow = 2 To lngLastRow
        lngPasteRow = wsDestin.Range("A:B").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row + 1
        wsDestin.Range("A" & lngPasteRow & ":A" & lngPasteRow + wsSource.Range("C" & lngMyRow) - 1).Value = wsSource.Range("A" & lngMyRow).Value
        With wsDestin.Range("B" & lngPasteRow & ":B" & lngPasteRow + wsSource.Range("C" & lngMyRow) - 1)
            .Formula = "=IF(ROW()=" & lngPasteRow & ",'" & wsSource.Name & "'!B" & lngMyRow & ",B" & lngPasteRow - 1 & "+1)"
            .Value = .Value
        End With
    Next lngMyRow
    
    Application.ScreenUpdating = True

End Sub

Regards,

Robert
 
Upvote 0
Hi Thanks Robert,

I'm getting a "Run-time error '91' Object variable or With block variable not set on... i think its from this row...

wsDestin.Range("A" & lngPasteRow & ":A" & lngPasteRow + wsSource.Range("C" & lngMyRow) - 1).Value = wsSource.Range("A" & lngMyRow).Value

Here's a better look at the data format and below I also have your code pasted into Developer window...

Capture.PNG
 

Attachments

  • Capture2.PNG
    Capture2.PNG
    45.4 KB · Views: 3
Upvote 0
I'd say there's nothing in columns A or B on Sheet2. Make sure there's at least headers in columns A and B and try again.

Code works for me.
 
Upvote 0
Robert, my apologies. Didnt realize that header was being referenced on Sheet2. You are correct and this work fabulous. Thank you!
 
Upvote 0
It's not that the headings are being referenced but this line of code...

VBA Code:
lngPasteRow = wsDestin.Range("A:B").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row + 1

...fails if there's nothing on the tab as it's searching for any piece of data to set the variable and doesn't what to do if there isn't anything to find. Though you've solved this by entering the headers, the following will get around the issue via code:

VBA Code:
Option Explicit
Sub Macro1()

    Dim lngMyRow As Long
    Dim lngLastRow As Long
    Dim lngPasteRow As Long
    Dim wsSource As Worksheet
    Dim wsDestin As Worksheet
   
    Application.ScreenUpdating = False
   
    Set wsSource = ThisWorkbook.Sheets("Sheet1")
    Set wsDestin = ThisWorkbook.Sheets("Sheet2")
   
    lngLastRow = wsSource.Range("A:C").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
   
    For lngMyRow = 2 To lngLastRow
        On Error Resume Next
            lngPasteRow = wsDestin.Range("A:B").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row + 1
        On Error GoTo 0
        If lngPasteRow = 0 Then lngPasteRow = 2 'Defualt row output if there's no data in column A or B of the 'wsDestin' tab. Change to suit.
        wsDestin.Range("A" & lngPasteRow & ":A" & lngPasteRow + wsSource.Range("C" & lngMyRow) - 1).Value = wsSource.Range("A" & lngMyRow).Value
        With wsDestin.Range("B" & lngPasteRow & ":B" & lngPasteRow + wsSource.Range("C" & lngMyRow) - 1)
            .Formula = "=IF(ROW()=" & lngPasteRow & ",'" & wsSource.Name & "'!B" & lngMyRow & ",B" & lngPasteRow - 1 & "+1)"
            .Value = .Value
        End With
    Next lngMyRow
   
    Application.ScreenUpdating = True

End Sub

this work fabulous. Thank you!

You're welcome :cool:

Robert
 
Upvote 0

Forum statistics

Threads
1,214,985
Messages
6,122,605
Members
449,089
Latest member
Motoracer88

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