VBA code for date & month

sikander1986

Board Regular
Joined
Sep 6, 2008
Messages
66
Hi,
I want to automate the week & month columns i.e. B & C with the help of VBA. I have date in column A & based on the date the column B & C should get updated auotmatically. Suppose the date in column A is 18-Aug-11 then column B should have 15-Aug-11(starting day of the week i.e. Monday) & column C should be Aug'11. For your reference below is the example of screenshot.
A B C
DATE WEEK MONTH
27-Jul-11 25-Jul-11 Jul'11
11-Aug-11 8-Aug-11 AUG'11
17-Aug-11 15-Aug-11 AUG'11
18-Aug-11 15-Aug-11 AUG'11
19-Aug-11 15-Aug-11 AUG'11

Thanks for your help in advance.
 
now the value is getting updated as & when i am manually typing the date. However, it is not updating the Column B & C if i am pasting or doing pastespecial/values with more dates. In this case i have to press F2 key & press enter to get update Column B & C. But if i remove date the Column B & C is remaining as it is...
 
Upvote 0

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
If you are dealing with pasting more than one entry, you have to loop through Target

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim oneCell as Range
    On Error Goto ErrorOut
    With Target
        If .Column = 1 Then
            Application.EnableEvents = False
            For Each oneCell in .Columns(1)
                With oneCell 
                    If IsDate(.Value) Then
                        .Offset(0, 1).FormulaR1C1 = "=RC[-1]-(WEEKDAY(RC[-1],3))                .Offset(0, 2).FormulaR1C1 = "=TEXT(RC[-2],""mmm""""'""""yy"")"
                        .Offset(0, 1).Resize(1, 2).Value = .Offset(0, 1).Resize(1, 2).Value
                    End If
                End With
            Next oneCell
        End If
    End With
ErrorOut:
    Application.EnableEvents = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,599
Messages
6,179,828
Members
452,946
Latest member
JoseDavid

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