2015-10-20 36 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