2015-10-21 61 views
1

它僅返回星期六,因爲日期很簡單,因爲我只需要添加一個加1,但是如何讓它從1到7循環?還怎麼換了個名字,以及查找具有for循環的月份名稱

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

它不會阻止它從週六上市的星期六。 – marvellosity123

回答

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 
+0

這就是我需要做的一切嗎?我不需要擁有函數的名字,而只需要實際的功能。幫助學習了很多東西 – marvellosity123