+ Reply to Thread
Results 1 to 5 of 5

Looping thought Dates

Hybrid View

  1. #1
    Registered User
    Join Date
    06-21-2012
    Location
    Netherlands
    MS-Off Ver
    365
    Posts
    48

    Looping thought Dates

    I'm quite new to VB and I'm trying to figure out how I can loop between two dates. The result should be an xml file which I can import in a different application. Sadly the XML DTD doesn't allow me to use an enddate, so i will need to print each date in a range seperately. This is what I have so far.

    vacDay is not specified, it's where the new date should come everytime in the loop.

    Love to hear some suggestions.


    intRowCount = 8
    
    With Sheets("Agent")
        Do Until .Cells(intRowCount, 1) = ""
        
        'Read data from sheet
        Action = .Cells(intRowCount, 1)                     'New of Change
        ttvID = .Cells(intRowCount, 84)
        
        vacActivity = "Instroom"
        vacPeriod1_from = .Cells(intRowCount, 50)
        vacPeriod1_to = .Cells(intRowCount, 51)
        vacPeriod2_from = .Cells(intRowCount, 52)
        vacPeriod2_to = .Cells(intRowCount, 53)
        vacPeriod3_from = .Cells(intRowCount, 54)
        vacPeriod3_to = .Cells(intRowCount, 55)
        
        vacDay1 = .Cells(intRowCount, 56)
        vacDay2 = .Cells(intRowCount, 57)
        vacDay3 = .Cells(intRowCount, 58)
        vacDay4 = .Cells(intRowCount, 59)
        vacDay5 = .Cells(intRowCount, 60)
        vacDay6 = .Cells(intRowCount, 61)
        
        'exc_date = .Cells(intRowCount, 2)
        'code = .Cells(intRowCount, 4)
        vac_year = .Cells(intRowCount, 71)
        vac_group = .Cells(intRowCount, 136)
    
        'Write data to text file
        If Action = "New" Then
            Print #intFile, "<agent>"
            Print #intFile, "   <id>" & ttvID & "</id>"
    
        'If vacation period 1 is filled, loop between dates and print each date where vacDay.
            If vacPeriod1_from <> "" And vacPeriod1_to <> "" Then
            Do Until vacPeriod1_from = vacPeriod1_to
                Print #intFile, "   <schedule>"
                Print #intFile, "       <day>" & Format(vacDay, "d") & "</day><month>" & Format(vacDay, "m") & "</month><year>" & Format(vacDay, "yyyy") & "</year>"
                Print #intFile, "       <startdate>"
                Print #intFile, "          <day>" & Format(vacDay, "d") & "</day><month>" & Format(vacDay, "m") & "</month><year>" & Format(vacDay, "yyyy") & "</year>"
                Print #intFile, "       </startdate>"
                Print #intFile, "       <code>" & vacActivity & "</code>"
                Print #intFile, "       <vacationGroup>" & vac_group & "</vacationGroup>"
                Print #intFile, "       <vacationYear>" & vac_year & "</vacationYear>"
                Print #intFile, "   <schedule>"
                Print #intFile, "</agent>"
            Loop
        
            
            intRowCount = intRowCount + 1
        Loop
    End With

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: Looping thought Dates

    Which dates do you want to loop through?
    If posting code please use code tags, see here.

  3. #3
    Registered User
    Join Date
    06-21-2012
    Location
    Netherlands
    MS-Off Ver
    365
    Posts
    48

    Re: Looping thought Dates

    In this case the date would be in vacPeriod1_from and vacPeriod2_to. For example 2015-05-01 and 2015-05-15

    vacPeriod1_from = .Cells(intRowCount, 50)
        vacPeriod1_to = .Cells(intRowCount, 51)

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: Looping thought Dates

    All you need is this then.
    For dt = vacPeriod1_from To vacPeriod1_to
    
    Next dt
    With your example dates this would go through 2015-05-01, 2015-05-02, 2015-05-03, ..., 2015-05-15.

  5. #5
    Registered User
    Join Date
    06-21-2012
    Location
    Netherlands
    MS-Off Ver
    365
    Posts
    48

    Re: Looping thought Dates

    Thanks! It works great

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Looping through & cutting and pasting values between two dates
    By ShaliniGomes in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 06-19-2014, 10:33 AM
  2. Looping dates continuously with user option of stopping
    By dem tacos in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-24-2013, 07:54 PM
  3. Looping though a list of dates
    By pablowilks in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-13-2013, 01:49 PM
  4. looping with dates
    By johncon in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 01-07-2009, 07:41 AM
  5. help with looping dates
    By srroduin in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 04-18-2006, 02:10 PM

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.6.0 RC 1