使用Excel,如何通过在单元格中输入数据来暂停执行vba脚本

时间:2013-03-11 13:53:04

标签: excel excel-vba vba

我有一些vba代码可以从其网页配置页面上的设备每分钟获取4次信息。

我需要这样做,因为当我在列C中放置x并继续直到我在页面D的下一行放置x。 我有一个我可以调用的函数,它将告诉X是否在d中相对于c处于适当的位置。

我想做的是有一个按钮,说好,准备扫描。然后在c中输入第一个值时启动,然后在输入d值时停止。 在VBA脚本实际运行时,我也遇到了输入值的方法。

有什么建议吗?感谢。

以下是检查列的代码。

Public Function BackgroundScan(MonitorSpreadsheet As Boolean) As Boolean
Dim LastStart As Integer
Dim LastStop As Integer

intDebug = 1

Select Case MonitorSpreadsheet
    Case True
        'We are actively testing
        If intDebug = 1 Then MsgBox "we ARE monitoring the spreadsheet."
        'Call scanning routine here
        'Get the status TestingInProgress
        LastStart = FindLastStartRow("SVQ")
        LastStop = FindLastStopRow("SVQ")
        If intDebug = 1 Then MsgBox "LastStart " & LastStart
        If intDebug = 1 Then MsgBox "LastStop " & LastStop
        Select Case LastStart
            Case Is < 20
                'We have not started.
                If intDebug = 1 Then MsgBox "We have not started."
                BackgroundScan = False
                'Loop around, and check again
            Case Else
                'ok we have started, now check to see if we have stopped.
                Select Case LastStop
                    Case Is < LastStart
                        '**** We ARE testing!!! ****
                        If intDebug = 1 Then MsgBox "We are testing, and haven't finished."
                        BackgroundScan = True
                    Case LastStart
                        'LastStart and LastStop are the same line, we have started AND finished
                        If intDebug = 1 Then MsgBox "We have started AND finished!"
                        BackgroundScan = False
                        'Loop around, and check again
                    Case Else
                        'We have finished testing, and the test spanned multiple rows
                        BackgroundScan = False
                        If intDebug = 1 Then MsgBox "We started on one line, and finished on another."
                End Select
        End Select
    Case False
        'we are not actively testing
        If intDebug = 1 Then MsgBox "We are NOT monitoring the spreadsheet."
        BackgroundScan = False
    Case Else
        MsgBox "Error: Boolean variable reports: " & MonitorSpreadsheet
        BackgroundScan = False
End Select

结束功能

以下是扫描网页的代码。

Private Sub CommandButton1_Click()
Dim Some         As String 'can't resist a good pun!
Dim intDelay     As Integer
Dim intMinDelay  As Integer
Dim i            As Integer
Dim s            As Integer
Dim RunStart     As Date
Dim WhichSVBeam As String
Dim lLen As Integer
Dim CurrentSVID As String
Dim CurrentBeamID As String
Dim PreviousSVID As String
Dim PreviousBeamID As String
Dim ColonLocation As Integer
'*******************************************************
'***             Test Continuous Button              ***
'***         Where n is specified in cell A6         ***
'*******************************************************

'grab the number of minutes between checking values
intMinDelay = GetValues("A7")

RunStart = Now

'Do this until the end of time, or the execution is halted.
Do 'uncomment do when we are sure the DoEvents will work as we expect
    WhichSVBeam = Scan_SVBeam(PreviousSVID, PreviousBeamID)

    If InStr(WhichSVBeam, ":") Then
        lLen = Len(WhichSVBeam)
        ColonLocation = InStr(WhichSVBeam, ":")
        'MsgBox WhichSVBeam & ", " & ColonLocation
        CurrentSVID = Left(WhichSVBeam, ColonLocation - 1)
        'MsgBox CurrentSVID
        CurrentBeamID = Right(WhichSVBeam, lLen - ColonLocation)
        'MsgBox CurrentBeamID
    Else
        'no colon, nothing to parse (this shouldn't happen)
        MsgBox "No ':' from Scan_SVBeam"
    End If

    'Call sCheckExecutionTimeGap(RunStart)
    'loop for the number of minutes we specified
    For i = 1 To intMinDelay
        'check every second for events
        For s = 1 To 240
            Call AppSleep(250)
            DoEvents
        Next s
    Next i
Loop

End Sub

2 个答案:

答案 0 :(得分:0)

在伪代码中,它将是:

start when column c=x
    begin loop
        get data
        check value of column d
        if column d= x exit loop
    next loop iteration
end

是你想要的吗?

菲利普

答案 1 :(得分:0)

将定期运行的一段代码示例,允许您更改电子表格中要检查的值,如下所示:

Sub testCell()
Dim r1, r2 As Integer
Dim stopIt As Boolean

r1 = doWeStart
r2 = doWeStop(r1)

Debug.Print "The value of cell C1 is now " & [C1].Value
If r1 = 0 Then Debug.Print "We haven't started yet"
If r1 > 0 And r2 = 0 Then Debug.Print "We start but don't stop"
If r1 > 0 And r2 > 0 Then Debug.Print "We started and stopped"

If [C1].Value Like "stop" Or r1 > 0 And r2 > 0 Then stopIt = True Else stopIt = False

If Not stopIt Then
    Application.OnTime Now + TimeValue("00:00:05"), "testCell"
End If

End Sub
'
Function doWeStart()
    Dim xrow As Integer
    ' save old selection
    Set r = Selection
    xrow = 0

    ' search for "x" in column C
    On Error Resume Next
    xrow = Application.WorksheetFunction.Match("x", [C:C], 0)
    doWeStart = xrow

End Function
'
Function doWeStop(r1)
    Dim xrowd As Integer
    xrowd = 0

    ' search for "x" in column D, starting at row r1
    On Error Resume Next
    xrowd = Application.WorksheetFunction.Match("x", Range("D" & r1, "D1048576"), 0)
    If xrowd > 0 Then
        doWeStop = xrowd + r1 - 1
    Else
        doWeStop = 0
    End If

End Function

这将每隔五秒运行一次,将在C列中查找第一个“x”,在C中找到第一个“x”。根据具体情况,它将(现在)打印调试窗口中的消息 - 您可以将代码放在那里。当你在C1中输入“stop”,或者在C和D中都找到“x”时,它就会停止。