2015-10-20 28 views
0

如何讓列b在週末顯示「無」?代碼運行,但我沒有看到與沒有選擇的情況不同的東西。vba發現週末和選擇數組響應

Sub fixandresponse() 
Dim count As Integer 
Dim thedate As Date 
Dim typesofproblems() As Variant 
thedate = DateSerial(2014, 9, 1) 

typesofproblems = Array("bought new hardware", "Phone Support", "New User Request", "Rent Hardware") 


Select Case WeekdayName(thedate) 

Case 1, 7: Instr(typesofproblems) = "none" 
For count = 2 To 366 

Range("B" & count) = typesofproblems(WorksheetFunction.RandBetween(0, 3)) 
Range("A" & count) = thedate 

thedate = thedate + 1 
Next 

End Select 
End Sub 
+0

如果我刪除它運行選擇的情況下的東西它不工作。但我希望它在週末不說,我不知道如何。 – marvellosity123

+0

現在還說無效的程序調用 – marvellosity123

回答

0

幾件事情:

(1)WEEKDAYNAME()不返回一個數字,它需要一個整數,表示你想在一週的日子,並返回一個字符串的實際名稱例如「星期一」。你想要的功能是WeekDay()。

(2)迭代遍歷日期,循環是在錯誤的地方。 (3)instr()函數不會將值放入數組中,它會搜索一個字符串並以整數形式返回搜索字符串的起始位置。

試試這個:

Sub fixandresponse() 

Dim count As Integer 

Dim thedate As Date 

Dim typesofproblems() As Variant 

thedate = DateSerial(2014, 9, 1) 

typesofproblems = Array("bought new hardware", "Phone Support", "New User Request", "Rent Hardware") 
For count = 2 To 366 
    Select Case Weekday(thedate) 
      Case 1, 7 
       Range("B" & count) = "none" 
       Range("A" & count) = thedate 
     Case Else 
       Range("B" & count) = typesofproblems(WorksheetFunction.RandBetween(0, 3)) 
       Range("A" & count) = thedate 
    End Select 
    thedate = thedate + 1 
Next 



End Sub