Convert text to number in VBA

lionginass

New Member
Joined
Jul 29, 2016
Messages
24
Hello,


I have 4 month: January, February, March and April in range A2:D2

January actually means 1; February - 2; march - 3 and April 4

I need a code, that converts month name to number and return number

Lets say row 3:
February, february, April, April = 12 (2+2+4+4)
row 4:
January, April, January, March = 9 (1+4+1+3

replace all doesnt fit here, because i need those names in my worksheet

something with vlookop would work fine, but i dont need additional sheets or cells.. in other words i need that only month names and answer would be seen.

thanks for help
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
[TABLE="width: 500"]
<tbody>[TR]
[TD][/TD]
[TD]A[/TD]
[TD]B[/TD]
[TD]C[/TD]
[TD]D[/TD]
[TD]E[/TD]
[/TR]
[TR]
[TD]1
[/TD]
[TD]February[/TD]
[TD]April[/TD]
[TD]February[/TD]
[TD]April[/TD]
[TD]12[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]March[/TD]
[TD]January[/TD]
[TD]January[/TD]
[TD]April[/TD]
[TD]9[/TD]
[/TR]
</tbody>[/TABLE]



I need to create vba code (function) to return value 12 in cell E1 and 9 in cell E2
(January =1, February =2, march = 3, april = 4)
Table must show month names so replace function does not fit here

thanks
 
Upvote 0
Code:
Public Function SumMonth(Rng As Range) As Long
Dim Cel As Range, monthArr()
Dim v As Variant
monthArr = Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December")
For Each Cel In Rng
         v = Application.Match(Cel.Value, monthArr, 0)
         If IsNumeric(v) Then SumMonth = SumMonth + v
Next
End Function
 
Upvote 0
Why a VBA function when the native formula (function) does exactly that?
 
Upvote 0
Maybe
Code:
Function AddMonths(Rng As Range)
AddMonths = Evaluate("SUMPRODUCT(--(MONTH(1& " & Rng.Address & ")))")
End Function
Used like

Excel 2013/2016
ABCDE
25FebruaryAprilFebruaryApril12
26MarchJanuaryJanuaryApril9
Quote
Cell Formulas
RangeFormula
E25=AddMonths(A25:D25)
 
Upvote 0

Forum statistics

Threads
1,224,819
Messages
6,181,153
Members
453,021
Latest member
Justyna P

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