根据时间

时间:2015-08-27 13:48:50

标签: excel vba excel-vba

我正在开发一个像仪表板一样工作的电子表格,并且有一个显示所有工作表的用户窗体。用户可以选择他想要的纸张并基于指定的时间开始仅在所选纸张之间移动的呈现。 这是代码:

    Public StartStop As Integer, NSheetsSel As Integer, i As Integer, Aux As Integer
Public SheetsSel As Variant
Public Sub StartButton_Click()

StartStop = 1
NSheetsSel = 0
With Me.SheetsBox
    For i = 0 To .ListCount - 1
        If .Selected(i) Then
            NSheetsSel = NSheetsSel + 1
        End If
    Next
    'ReDim SheetsSel(30) As Integer
    ReDim SheetsSel(NSheetsSel) As Integer
    For i = 0 To .ListCount - 1
        If .Selected(i) Then
            SheetsSel(i) = i
        End If
    Next
End With
Aux = 0
Repeat2
End Sub

Public Sub UserForm_Initialize()
Dim WSheets() As String, size As Integer, i As Integer
    size = Sheets.Count
    ReDim WSheets(size)
    For i = 1 To Sheets.Count
        WSheets(i - 1) = Workbooks("Packaging Dashboard.xlsm").Sheets(i).Name
    Next i
SheetsBox.List = WSheets
End Sub

Public Sub Repeat()
alertTime = Now + TimeValue(TimeP / 86400)
Application.OnTime alertTime, "Repeat2"
End Sub

Public Sub Repeat2()
Dim Test As Integer
Test = SheetsSel(Aux)
If StartStop = 1 Then
Worksheets(Test).Activate
Aux = Aux + 1
If Aux = SheetsSel.Length - 1 Then
Aux = 0
End If
Repeat
End If
End Sub

我遇到的第一个问题是它不能ReDim SheetsSel(NSheetsSel) As Integer。它在SheetsSel(i) = i上显示“下标超出范围”。当我将其重新调整为特定大小ReDim SheetsSel(30) As Integer时,它会传递该部分并在Worksheets(Test).Activate处崩溃,说“下标超出范围”。有什么想法吗?

2 个答案:

答案 0 :(得分:0)

您需要SheetsSel数组的单独索引:

ReDim SheetsSel(NSheetsSel) As Integer
For i = 0 To .ListCount - 1
    If .Selected(i) Then
        n = n + 1
        SheetsSel(n) = i
    End If
Next

答案 1 :(得分:0)

我决定不使用userform和数组,所以我觉得它非常简单。感谢所有试图帮助我的人。这是我做过的代码,以防有人遇到类似的问题。谢谢你们。

Public Timep As Date, StartStop As Boolean
Sub StartP()
Timep = Sheets("Present").Range("B1").Value / 86400
StartStop = True
Slide
End Sub
Sub StopP()
StartStop = False
End Sub

Sub Slide()
'
' Slide Macro
'
'
If StartStop Then
    Sheets("1 Day ScoreCard All Packaging").Select
    alertTime = Now + Timep
    Application.OnTime alertTime, "Slide2"
End If
End Sub
Public Sub Slide2()
If StartStop Then
Sheets("1 Day ScoreCard Pail Line").Select
    alertTime = Now + Timep
    Application.OnTime alertTime, "Slide3"
End If
End Sub
Public Sub Slide3()
If StartStop Then
Sheets("1 Day ScoreCard Auto Line").Select
    alertTime = Now + Timep
    Application.OnTime alertTime, "Slide4"
End If
End Sub
Public Sub Slide4()
If StartStop Then
Sheets("1 Day ScoreCard Capping Line").Select
    alertTime = Now + Timep
    Application.OnTime alertTime, "Slide5"
End If
End Sub
Public Sub Slide5()
If StartStop Then
Sheets("1 Day ScoreCard Half Gallon").Select
    alertTime = Now + Timep
    Application.OnTime alertTime, "Slide6"
End If
End Sub
Public Sub Slide6()
If StartStop Then
Sheets("1 Day ScoreCard 2.5").Select
    alertTime = Now + Timep
    Application.OnTime alertTime, "Slide7"
End If
End Sub
Public Sub Slide7()
If StartStop Then
Sheets("1 Day ScoreCare X").Select
    alertTime = Now + Timep
    Application.OnTime alertTime, "Slide"
End If
End Sub