VBA if语句通过数据透视表更改组

时间:2016-11-15 17:03:27

标签: excel vba excel-vba

希望创建一个按钮,该按钮将根据当前已分组的内容更改数据透视表组。这是我到目前为止所做的:

Range("EE6").Select    
If group Start:=40756 then
Selection.Group Start:=40695, End:=True, By:=7, Periods:=Array(False, False, False, True, False, False, False)
Else
Range("EE6").Select
Selection.Group Start:=40756, End:=True, By:=7, Periods:=Array(False, False, False, True, False, False, False)
End if

但我得到编译错误预期:然后或转到第一行。 谁能告诉我这里做错了什么?

1 个答案:

答案 0 :(得分:0)

请尝试以下操作(假设EE6是包含该组第一个值的单元格):

Dim startDate As Date
If Range("EE6").Value = "<01/08/2011" Then 'The date format may differ on your computer - use whatever is appropriate for you
    startDate = DateSerial(2011, 1, 6) 
Else
    startDate = DateSerial(2011, 1, 8)
End If

Range("EE6").Group Start:=startDate, End:=True, By:=7, Periods:=Array(False, False, False, True, False, False, False)

如果您实际上尝试在星期一开始和星期三开始之间切换分组,请尝试以下操作:

Dim startDate As Date
Dim tempVal As String
tempVal = Range("EE6").Value
If IsDate(tempVal) Then
    startDate = CDate(tempVal)
ElseIf InStr(tempVal, " - ") > 0 Then
    startDate = DateValue(Left(tempVal, InStr(tempVal, " - ") - 1))
Else
    MsgBox "Invalid data"
    Exit Sub
End If

If startDate Mod 7 = 2 Then
    startDate = 4
Else
    startDate = 2
End If
Range("EE6").Group Start:=startDate, End:=True, By:=7, Periods:=Array(False, False, _
        False, True, False, False, False)