找到包含for循环的月份名称

时间:2015-10-21 23:31:44

标签: vba date

它只返回星期六,因为日期很简单,因为我只需要添加一个加1但是如何让它从1循环到7?还有几个月的名字呢

Sub netprofit()

Dim dates As Date
Dim count As Integer

Dim olweekdayname As String

olweekdayname = WeekdayName(Weekday(dates))

dates = DateSerial(2014, 9, 1)
For count = 2 To 366

Select Case Weekday(dates)

Case 1, 7

    Range("B" & count) = 0

    Range("C" & count) = 0

    Range("D" & count) = 0
    Range("E" & count) = olweekdayname


Case Else
    Range("C" & count) = Int((100 - 0 + 1) * Rnd + 0)
    Range("D" & count) = Range("B" & count) - Range("C" & count)
    Range("A" & count) = dates
    Range("E" & count) = olweekdayname

End Select
dates = dates + 1

Next
End Sub

1 个答案:

答案 0 :(得分:2)

此行olweekdayname = WeekdayName(Weekday(dates))必须在循环内部,否则olweekdayname将不会更改,因此您的更新代码低于,经过测试,工作正常。

Sub netprofit()
    Dim dates As Date
    Dim count As Integer
    dates = DateSerial(2014, 9, 1)
    For count = 2 To 366
        Select Case Weekday(dates)
        Case 1, 7
            Range("B" & count) = 0
            Range("C" & count) = 0
            Range("D" & count) = 0
            Range("E" & count) = WeekdayName(Weekday(dates))
            Range("F" & count) = MonthName(Month(dates))
        Case Else
            Range("C" & count) = Int((100 - 0 + 1) * Rnd + 0)
            Range("D" & count) = Range("B" & count) - Range("C" & count)
            Range("A" & count) = dates
            Range("E" & count) = WeekdayName(Weekday(dates))
            Range("F" & count) = MonthName(Month(dates))
        End Select
        dates = dates + 1
    Next
End Sub