Mike
Mike

Reputation: 2751

Assign IF statement result with two result variables (IF, THEN, AND?)

Hmmm I can't find the best way to do this. Plus, this code doesn't work anyway. I want the text of "January" (and all 12 months) returned with two variables:

If month = "January" Then
        days = "31" And monthnum = "1"
End If

Suggestions?

Syntax that finally worked:

If month = "January" Then
        days = 31
        mon = 1
    ElseIf month = "February" Then
        days = 29
         mon = 2
'elseif for the rest of the months
End If

Upvotes: 0

Views: 2848

Answers (3)

Desert Spider
Desert Spider

Reputation: 778

You could also use the Case statement

Sub MonthValue()

For b 1 To 20

Select Case Range("A" & a).Value
Case January, March, May, June, August, October, December
ActiveCell.Offset(0,1).Value = 31
Case Feburary
ActiveCell.Offset(0,1).Value = 28
Case April, June, September, November
ActiveCell.Offset(0,1).Value = 30
Case Else ActiveCell.Offset(0,1).Value = 0
End Select

Next b
End Sub

Upvotes: 0

Hiten004
Hiten004

Reputation: 2481

Try this VBA syntext

Dim days As Integer
Dim monthnum As Integer

'if then elseif end

If Month = "January" Then
        days = 31
        monthnum = 1
ElseIf Month = "February" Then
        days = 28
        monthnum = 2
ElseIf Month = "March" Then
        days = 31
        monthnum = 1
 ' repeat for all other months
End If

Upvotes: 3

Alex K.
Alex K.

Reputation: 175896

Assuming english locale you could

sMonth = "april"

monthnum = month("1 " & sMonth)
days = day(dateserial(2001,iif(monthnum=12, 1, monthnum+1),0))

Upvotes: 3

Related Questions