+ Reply to Thread
Results 1 to 5 of 5

VBA to insert this first monday of the week date function/value

Hybrid View

line1260skr VBA to insert this first... 11-27-2012, 08:50 PM
xLJer Re: VBA to insert this first... 11-27-2012, 11:22 PM
line1260skr Re: VBA to insert this first... 11-28-2012, 05:42 PM
line1260skr Re: VBA to insert this first... 11-28-2012, 06:10 PM
xLJer Re: VBA to insert this first... 11-28-2012, 07:15 PM
  1. #1
    Registered User
    Join Date
    08-01-2012
    Location
    US
    MS-Off Ver
    Excel 2003
    Posts
    13

    VBA to insert this first monday of the week date function/value

    I am trying to replace my worksheet function which generates a date for the first monday of the weekday when given a date. I have managed to create a VBA code that works to insert this function in to cells. It is very ugly and slow and may make some of you sick to your stomach. I was hoping someone could provide a more reasonable way for trying to accomplish this task.

    'Insert Week Begin Formula into Column L
        
        rw = 2
        
        Do While Cells(rw, 1) <> ""
            
            Cells(rw, 12) = "=IF($A2<>"""",DATE(YEAR($A2),MONTH($A2),DAY($A2)-WEEKDAY($A2,3)),"""")"
            
        rw = rw + 1
        Loop
        
    'Copy the row references down for the Week Begin Formula
        Dim LastRow As Long
        LastRow = Cells(Rows.Count, "L").End(xlUp).Row
    
        Range("L2").Select
        Selection.AutoFill Destination:=Range("L2:L" & LastRow)

  2. #2
    Forum Contributor
    Join Date
    07-27-2012
    Location
    California, USA
    MS-Off Ver
    Excel 2003
    Posts
    198

    Re: VBA to insert this first monday of the week date function/value

    If you would like to use a worksheet formula, something like this may
    work for you by calculating just Monday dates.

    If you try it, with a starting date typed in cell A2, add this formula to A3: "=A2+1",
    and copy this formula down some, choose a date format for column A cells to show the weekday name,
    then you will see that the formula in column L displays the Monday date for the days
    Saturday through the following Friday. Use a date format to show weekday names in column L as well and copy it down as needed.

    =A2+(8-MOD(WEEKDAY(A2),7))-6
    Last edited by xLJer; 11-27-2012 at 11:26 PM.

  3. #3
    Registered User
    Join Date
    08-01-2012
    Location
    US
    MS-Off Ver
    Excel 2003
    Posts
    13

    Re: VBA to insert this first monday of the week date function/value

    That code works well when I paste it into Column L and copy it down. However, how would I rewrite that code to have the vba populate the cell for me (either with the formula itself or the value)?

    A-K are part of an exported .xls file and I will need column L to create itself.

  4. #4
    Registered User
    Join Date
    08-01-2012
    Location
    US
    MS-Off Ver
    Excel 2003
    Posts
    13

    Re: VBA to insert this first monday of the week date function/value

    I tried incorporating the code from post #2 into my code from post #1 and got:
    'Insert Week Begin Formula into Column L
        
        rw = 2
        
        Do While Cells(rw, 1) <> ""
            
            Cells(rw, 12) = "=A2+(8-MOD(WEEKDAY(A2),7))-6"
            
        rw = rw + 1
        Loop
        
    'Copy the row references down for the Week Begin Formula
        Dim LastRow As Long
        LastRow = Cells(Rows.Count, "L").End(xlUp).Row
    
        Range("L2").Select
        Selection.AutoFill Destination:=Range("L2:L" & LastRow)
    The result is that it copies the same formula placed in cell A2 all the way down. It stops at the correct row but the row references are not changing. What does the code look like to have these cell references change as they are copied down??

  5. #5
    Forum Contributor
    Join Date
    07-27-2012
    Location
    California, USA
    MS-Off Ver
    Excel 2003
    Posts
    198

    Re: VBA to insert this first monday of the week date function/value

    Here is your code, revised to calculate Monday dates based on the date
    listed in column A, and put them in column L. This code defines the week
    as Sunday through Saturday, but you can use the alternate Saturday through Friday
    code commented out, if needed.

    Your loop was not coded correctly, as it was just duplicating the same thing for each row.
    Since you chose to use AutoFill, I removed the unnecessary looping code.


    Sub Test_1()
    'Insert Week Begin Formula into Column L
    Dim LastRow As Long, rw As Long
        
        LastRow = Cells(Rows.Count, "A").End(xlUp).Row
    
        'Cells(2, 12).Formula = "=A2+(8-MOD(WEEKDAY(A2),7))-6"   'uses week of Saturday thru Friday
        Cells(2, 12).Formula = "=A2+(7-WEEKDAY(A2)-5)"           'uses week of Sunday thru Saturday
        
        'Copy the row references down for the Week Begin Formula
        Range("L2").Select
        Selection.AutoFill Destination:=Range("L2:L" & LastRow)
        
        'change formulas to values
        For rw = 2 To LastRow
            Cells(rw, 12).Value = Cells(rw, 12).Value
        Next
        
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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