VBA Code for converting a date into a month for use in Pivot Tables

spraff

New Member
Joined
Oct 8, 2014
Messages
18
I currently use the following VBA module code to convert dates to a Week Number:

Function VBWeekNum(dt As Date, WkStart As Long) As Long​
VBWeekNum = DatePart("ww", dt, WkStart)​
End Function​



And use the forumula to populate the cell (our week goes Fri-Thurs)

=VBWeekNum([@[Date PST]],6)

I'd like to use something similar for converting dates to month names for use in Pivot Tables (am I missing something, as it seems there should be an inherent way to do this w/in Excel - currently I'm just manually adjusting the months so my Slicer has ONE button for each month rather than one per individual date/time).

I'm a complete novice in VBA. I've tried hacking away at code provided from two other sites to no avail.

Any help greatly appreciated!



 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Thank you V_Malkoti

But, I'm still a noob! :confused:

So, writing the module with this doesn't suffice:
Function MonthName( _​
ByVal Month As Integer, _​
Optional ByVal Abbreviate As Boolean = False _​
) As String​

End Function​


Nor does:
Function MonthName()​

Dim thisMonth As Integer​
Dim name As String​
thisMonth = 4​
' Set Abbreviate to True to return an abbreviated name.​
name = MonthName(thisMonth, True)​
' name now contains "Apr".​

End Function​

What do I need in the Module box? And what formula would I need? I'm using this for a worksheet that has 114,000+ unique date/time stamps (one per row) in the Time PST column.

Many thanks!
 
Upvote 0
Create a UDF. Paste following code in a module:

Code:
Function GetMonthName(dt As Date, abbr As Boolean) As String
    GetMonthName = MonthName(Month(dt), abbr)
End Function

you can then use it in spreadsheet cells as:

=GetMonthName(B1, False)
 
Last edited:
Upvote 0
Awesome! Thank you V! Much appreciated!

It didn't work in the spreadsheet I wanted it to work in, but did in another. Will try to hunt down why it's not working in the desired xls. Again, thank you!
 
Upvote 0

Forum statistics

Threads
1,224,608
Messages
6,179,872
Members
452,949
Latest member
Dupuhini

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