它只返回星期六,因为日期很简单,因为我只需要添加一个加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
答案 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