VBA Macro - Change Date/Time into the Month

LNG2013

Active Member
Joined
May 23, 2011
Messages
466
I need help writing a VBA Macro that looks at the Date/Times in Column A and then puts the correct Month in Column B.


Because the column's switch positions I would need to do it by column name.

Column A*******************Column B
1. Date*********************Month
2. 5/3/2010 15:07*************5
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
As a macro:

Code:
Sub test()
Dim x As Long
x = Range("A" & Rows.Count).End(xlUp).Row
With Range("B2:B" & x)
    .Formula = "=MONTH(A2)"
    .Value = .Value
End With
End Sub
 
Upvote 0
Hi HOTPEPPER,

That works as long as the data stays in column A. Is there a way to do it by the column name? I want it to look at column "Date", and fill in in the column "Month"



As a macro:

Code:
Sub test()
Dim x As Long
x = Range("A" & Rows.Count).End(xlUp).Row
With Range("B2:B" & x)
    .Formula = "=MONTH(A2)"
    .Value = .Value
End With
End Sub
 
Upvote 0
Code:
Sub test()
Dim dc As Range, mc As Range, x As Long
Set dc = Range("1:1").Find("Date", , , xlWhole)
If dc Is Nothing Then MsgBox "Date Column not Found", vbExclamation: Exit Sub
Set mc = Range("1:1").Find("Month", , , xlWhole)
If dc Is Nothing Then MsgBox "Month Column not Found", vbExclamation: Exit Sub
x = Cells(Rows.Count, dc.Column).End(xlUp).Row
With mc.Offset(1).Resize(x - 1)
    .Formula = "=MONTH(" & dc.Range("A2").Address(0, 0) & ")"
    .Value = .Value
End With
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