+ Reply to Thread
Results 1 to 3 of 3

Thread: VBA to loop through names and dates adding rows

  1. #1
    Registered User
    Join Date
    01-25-2012
    Location
    Brighton, England
    MS-Off Ver
    Excel 2003
    Posts
    2

    VBA to loop through names and dates adding rows

    Hi all,

    This is my first post in this forum and I'm hoping someone may be able to help me.

    I am very new to using VBA to create my macros, previously I have simply used "record".

    I have attached an example .xls file which, I hope, shows what I need to do. problem.xls

    For each name in the list, i need to fill in the dates between the "Date In" and "Date Out" values. I have been able to do this when there is only
    one person in the list but get stuck at looping through all other names.

    Any help would be enormously appreciated.

    Thanks.

  2. #2
    Valued Forum Contributor p24leclerc's Avatar
    Join Date
    07-05-2010
    Location
    Québec
    MS-Off Ver
    Excel 2003-2010
    Posts
    377

    Re: VBA to loop through names and dates adding rows

    Here is a macro to do it:
    It assumes titles row is in row 1 and start working at row 3 as it refers to an offset of -1 row to look at previous data.
    Public Sub test()
    Dim C_ell As Range, D_ateS As Date, D_ateE As Date
    For Each C_ell In Range("A3", Cells(Rows.Count, 1).End(xlUp))
      If C_ell = C_ell.Offset(-1, 0) Then
        D_ateS = C_ell.Offset(-1, 2)
        D_ateE = C_ell.Offset(0, 2)
        While D_ateS + 1 < D_ateE
            C_ell.Offset(-1, 0).Resize(1, 1).EntireRow.Offset(1, 0).Insert
            C_ell.Offset(-1, 0).Resize(1, 5).FillDown
            C_ell.Offset(-1, 2) = D_ateS + 1
            D_ateS = D_ateS + 1
        Wend
      Else
        D_ateS = C_ell.Offset(-1, 2)
        D_ateE = C_ell.Offset(-1, 4)
        While D_ateS + 1 <= D_ateE
          C_ell.Offset(-1, 0).Resize(1, 1).EntireRow.Offset(1, 0).Insert
          C_ell.Offset(-1, 0).Resize(1, 5).FillDown
          C_ell.Offset(-1, 2) = D_ateS + 1
          D_ateS = D_ateS + 1
        Wend
      End If
    Next
    D_ateS = Cells(Rows.Count, 3).End(xlUp)
    D_ateE = Cells(Rows.Count, 5).End(xlUp)
    While D_ateS + 1 <= D_ateE
      Cells(Rows.Count, 1).End(xlUp).Resize(2, 5).FillDown
      Cells(Rows.Count, 3).End(xlUp) = D_ateS + 1
      D_ateS = D_ateS + 1
    Wend
    End Sub

  3. #3
    Registered User
    Join Date
    01-25-2012
    Location
    Brighton, England
    MS-Off Ver
    Excel 2003
    Posts
    2

    Re: VBA to loop through names and dates adding rows

    Thanks a million p24leclerc - that's immensely helpful!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.2.0