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.
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
B2 would have the formula =A2-WEEKDAY(A2, 3)

C2 could have the formula =A2-DAY(A2)+1 , which returns an Excel serial date.
The cell could have the custom format mmm'yy so it would show in the desired format.
or
C2 could have the formula =TEXT(A2, "mmm'yy") , which returns a text string.
 
Upvote 0
thanks for your reply.
i have tried this formula but as the data is huge so it is taking more time to calculate & at the same time making the file size more as i have excel 2003.

it would be helpful if you could tell me the same solution in VBA code i.e. macros

Thanks in advance..
 
Upvote 0
i have no knowledge of VBA. So i am looking for ways from where i can Learn how to write VBA code.
But regarding the problem i haven't tried any VBA code.
 
Upvote 0
This is as good a teaching problem as any.
You could start by writting a routine that took the value in A1 and wrote the value of
A1-WEEKDAY(A1,3) into cell B1.

The vba syntax for using worksheet functions is Application.WorksheetFunction.Weekday(someValue, 3) .

Or you could record a macro of you putting that formula in B1, copying it, and pasting special/values.
 
Upvote 0
Hi mikerickson,

As you have said i have tried by recording the macro for 1 value in cell A2 & got the below code.

Sub Macro1()
'
' Macro1 Macro
'
' Keyboard Shortcut: Ctrl+t
'
ActiveCell.FormulaR1C1 = "1-Aug-2011"
Range("B2").Select
ActiveCell.FormulaR1C1 = "=RC[-1]-(WEEKDAY(RC[-1],3))"
Range("B2").Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("C2").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "=TEXT(RC[-2],""mmm""""'""""yy"")"
Range("C2").Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End Sub

And this code works only when i run the macro by using the shortcut key or from the menu. I want to create macro in such a way that it should run automatically. I mean to say that if i enter any value in Column A the Column B & Column C should get update automatically.And as i said before i have huge data in the file for this 3 columns so how to make the code work for other cells in this columns.

Thank you...
 
Upvote 0
Now that we have code that does what you want once, let's make it dance the way that you want it to.

This is the Recorder's code, with comments. Also, since VBA doesn't need to select cells with a mouse, the Selecting has been removed

Code:
ActiveCell.FormulaR1C1 = "1-Aug-2011": Rem user enters date in Column A

Rem formula is put in column B
Range("B2").FormulaR1C1 = "=RC[-1]-(WEEKDAY(RC[-1],3))"
Rem which is then copy/paste valued
Range("B2").Copy
Range("B2").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False

Rem same for column C
Range("C2").FormulaR1C1 = "=TEXT(RC[-2],""mmm""""'""""yy"")"
Range("C2").Copy
Range("C2").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False

Let's base everything off of the ActiveCell, by using a With...End With construct.
Code:
ActiveCell.FormulaR1C1 = "1-Aug-2011"

With ActiveCell
    .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 With
.Offset(0,1).Resize(1, 2) is the range that starts one cell to the right and is one row by two column.
If ActiveCell was A2 (like when you recorded the macro), .Offset(0,1).Resize(1, 2) would be B2:C2.
The .Value = .Value is a quick way to do the same as copy/PasteValues.

Since you want this triggered everytime the user enters something into column A, we would put our routine in the Worksheet_Change event. This is found in the sheet's code module, rather than the normal module where the Macro Recorder put it's routine.
When in the VB Editor, look at the project Explorer and double click on the icon for the sheet where you want this to occur.
When that window opens, choose Worksheet from the left dropdown at the top of the window and then Change from the right drop down (not SelectionChange)

The window should have created an empty Worksheet Change event that looks like this. Just waiting to be filled with code.
Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)

End Sub
This routine will run any time any cell in the sheet is changed, but you only want action to be taken when a date is entered in column A. To make this restriction, we use the Target argument. Target is those cells that are changed. We want to code to run when a) only one cell is changed, b) that cell is in column A and c) a date is what is entered. So we test for those conditions
Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Target.Cells.Count = 1 And Target.Column = 1 And IsDate(Target.Value Then
        
        Rem code

    End If
End Sub
Putting it all together, we get
Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    On Error Goto ErrorOut
    With Target
        If .Cells.Count = 1 And .Column = 1 And IsDate(.Value) Then
             Application.EnableEvents = False
            .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
ErrorOut:
    Application.EnableEvents = True
End Sub
The Application.EnableEvents is set to false so that the changes that the code makes don't trigger another Change event, which would trigger another Change event, etc...... setting EnableEvents to false keeps the Change event from running. But it needs to be set to True at the end, so that it will run the next time the user enters data.

The Error code is so that if an error occurs while EnableEvents is false (like if the user protects the sheet), if that occurs, the error will be handled and EnableEvents will be set to True.

I know this is a pretty big chunk of info to absorb, but if you can understand this, you'll be able to do it by yourself, from Macro Recorder to error protected event code, the next time.
 
Last edited:
Upvote 0
Hi,
sorry for the late reply.
Thanks alot for the wonderful explanation u have given, i have learned something new from you.
I have pasted the code as u have said. But if i put a data in A7 & press enter & again i have to move the cursor to the cell a7 then the Column B & Column C are getting updated with the required information. Otherwise they are not working. And if for example i have pasted around 2 months date in columns A then i have to move the cursor to all cells so that i get updated in column B & C.

And one thing suppose if i remove the date in Column A the Column B & C should be Blank. Where as in this case its not happening.
 
Upvote 0

Forum statistics

Threads
1,224,598
Messages
6,179,814
Members
452,945
Latest member
Bib195

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