0

我有一些 vba 代码可以从其 Web 配置页面上的设备每分钟 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
4

2 回答 2

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

那是你要的吗?

菲利普

于 2013-03-11T14:18:02.577 回答
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 中找到的列下方的 D 列中查找第一个“x”。根据那里的内容,它将(现在)在调试窗口——你可以把你的代码放在那里。当您在 C1 中输入“停止”,或者在 C 和 D 中都找到“x”时,它会停止。

于 2013-03-11T19:20:30.843 回答