在特定工作表中的按钮之间切换并循环

时间:2017-10-02 09:18:06

标签: excel vba excel-vba

我很难在VBA中创建我的第一个宏,我尝试制作简单的宏,它会在每分钟切换按钮以显示不同的数据。 现在我真的很沮丧,因为每次我改变一些东西都会收到不同的错误,因为我确实遇到了我想要选择的工作表参考问题。目前我收到错误“loop without do”,当我删除循环时,我得到编译错误“期望结束”。

请在下面找到我的“工作”,请告诉我如何使其工作。

Public Sub Switch()
Do
For Each ws In ThisWorkbook.Worksheets
ControlBoard.Activate
With ActiveWorkbook.SlicerCaches("Slicer_Project1")
    .SlicerItems("XX").Selected = True
    .SlicerItems("YY").Selected = False
    .SlicerItems("ZZ").Selected = False
    Application.Wait Now("00:01:00")
With ActiveWorkbook.SlicerCaches("Slicer_Project1")
    .SlicerItems("XX").Selected = False
    .SlicerItems("YY").Selected = True
    .SlicerItems("ZZ").Selected = False
     Application.Wait Now("00:01:00")

End With
With ActiveWorkbook.SlicerCaches("Slicer_Project1")
    .SlicerItems("XX").Selected = False
    .SlicerItems("YY").Selected = False
    .SlicerItems("ZZ").Selected = True
     Application.Wait Now("00:01:00")
End With
Loop

End Sub

好的,现在我改了一下,我得到运行时错误13(类型不匹配),我在第一行得到调试 - Application.Wait Now(“00:01:00”)

Public Sub Switch()
Dim DashBoard As Sheet3
Do

With ActiveWorkbook.SlicerCaches("Slicer_Project1")
    .SlicerItems("XX").Selected = True
    .SlicerItems("YY").Selected = False
    .SlicerItems("ZZ").Selected = False
    Application.Wait Now("00:01:00")
End With
With ActiveWorkbook.SlicerCaches("Slicer_Project1")
    .SlicerItems("XX").Selected = False
    .SlicerItems("YY").Selected = True
    .SlicerItems("ZZ").Selected = False
     Application.Wait Now("00:01:00")

End With
With ActiveWorkbook.SlicerCaches("Slicer_Project1")
    .SlicerItems("XX").Selected = False
    .SlicerItems("YY").Selected = False
    .SlicerItems("ZZ").Selected = True
     Application.Wait Now("00:01:00")
End With
Loop
End Sub

我通过添加+ TimeValue解决了类型不匹配问题 Application.Wait Now + TimeValue(“00:01:00”) 但切片机并不像我想要的那样表现。它让我在开始时(“XX”)真实,但进一步在它上面滴答作响(“YY”)真实而且(“XX”)仍然存在,真的,你可以帮助我解决它吗?

好的我已经解决了这个问题,将True语句放在第一行,它会不时冻结但是它有效,你能告诉我,如果可以添加关闭键,以避免esc + esc退出吗?

Public Sub Switch()
Dim DashBoard As Sheet3
Do

With ActiveWorkbook.SlicerCaches("Slicer_Project1")
    .SlicerItems("XX").Selected = True
    .SlicerItems("YY").Selected = False
    .SlicerItems("ZZ").Selected = False
    Application.Wait Now + TimeValue("00:00:10")
End With
With ActiveWorkbook.SlicerCaches("Slicer_Project1")
    .SlicerItems("YY").Selected = True
    .SlicerItems("XX").Selected = False
    .SlicerItems("ZZ").Selected = False
     Application.Wait Now + TimeValue("00:00:10")
End With
With ActiveWorkbook.SlicerCaches("Slicer_Project1")
    .SlicerItems("ZZ").Selected = True
    .SlicerItems("XX").Selected = False
    .SlicerItems("YY").Selected = False
     Application.Wait Now + TimeValue("00:00:10")
End With
Loop
End Sub

0 个答案:

没有答案