使用两个结果变量(IF,THEN,AND?)分配IF语句结果

时间:2013-01-04 16:44:59

标签: excel vba if-statement

嗯,我找不到最好的办法。此外,此代码无论如何都不起作用。我希望“January”(以及所有12个月)的文本返回两个变量:

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

建议?

最终奏效的语法:

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

3 个答案:

答案 0 :(得分:3)

试试这个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

答案 1 :(得分:3)

假设英语语言环境可以

sMonth = "april"

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

答案 2 :(得分:0)

您也可以使用Case语句

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
相关问题