0

嗯,我找不到最好的方法来做到这一点。另外,这段代码无论如何都不起作用。我希望返回带有两个变量的“一月”(以及所有 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
4

3 回答 3

3

假设您可以使用英语语言环境

sMonth = "april"

monthnum = month("1 " & sMonth)
days = day(dateserial(2001,iif(monthnum=12, 1, monthnum+1),0))
于 2013-01-04T17:13:25.407 回答
3

试试这个 VBA 语法

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
于 2013-01-04T16:52:26.970 回答
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
于 2013-01-05T21:35:00.190 回答