Excel VBA script - stealing focus

一世执手 提交于 2021-01-29 21:35:55

问题


I'm working on a script that pings computers from a list periodically and returns information.

My problem is, whenever the the script is running, it steals focus from other excel windows.
For example if if I'm typing in another workbook when the scrip runs, it jumps (to the cell that was last selected) and continues writing in the cell.

Here is the script:

Sub autoping_cb()

Dim c As Range
Dim thePing As Variant
Dim TryCount As Integer
Dim TryAgainCount As Integer
Dim TryNextRun As Boolean

TryNextRun = False

Set sht = Application.ThisWorkbook.Worksheets(1)
LastRow = sht.Cells(sht.Rows.Count, "B").End(xlUp).Row

Dim chb As Shape
Set chb = ThisWorkbook.Worksheets(1).Shapes("autoping")

If chb.ControlFormat.Value = xlOn Then



sht.Range("H3").Value = Replace(sht.Range("H3").Value, ",", ".")


TryCount = 1

    If sht.Range("H4") <> "" And IsNumeric(sht.Range("H4")) = True And sht.Range("H4") = Int(sht.Range("H4")) And sht.Range("H3") <> "" And IsNumeric(sht.Range("H3")) = True Then
        TryAgainCount = sht.Range("H4").Value
            If TryAgainCount = 0 Then
                TryNextRun = True
            End If
        Do Until chb.ControlFormat.Value = xlOff            


            Wait ThisWorkbook.Worksheets(1).Range("H3").Value * 60 '<-- replace to 60 after testing

            For Each c In Application.Worksheets(1).Range("B3:B" & LastRow)
                    If chb.ControlFormat.Value = xlOff Then
                        End

                    ElseIf chb.ControlFormat.Value = xlOn Then
                        If ispcname(c.Value) = True Or isip(c.Value) = True Then
                            If c.Offset(0, 2) = "--->" And TryNextRun = False Then



                            Else
                                c.Offset(0, 1) = nslookup(c.Value)
                                thePing = sPing(c.Value)
                                c.Offset(0, 2) = thePing(0)
                                c.Offset(0, 3) = GetErrorCode(thePing(1))

                                If c.Offset(0, 2).Value = "--->" Then
                                    sht.Range("B" & c.Row & ":E" & c.Row).Style = "Bad"
                                ElseIf c.Offset(0, 2).Value < 50 Then
                                    sht.Range("B" & c.Row & ":E" & c.Row).Style = "Good"
                                Else
                                    sht.Range("B" & c.Row & ":E" & c.Row).Style = "Neutral"
                                End If

                            End If
                        End If


                    End If



                sht.Range("B2:E" & LastRow + 1).Columns.AutoFit
            Next c

        If TryNextRun = False And TryCount < TryAgainCount Then
            TryCount = TryCount + 1
            Debug.Print 1
        ElseIf TryNextRun = False And TryCount >= TryAgainCount Then
            TryNextRun = True
            TryCount = 1
            Debug.Print 2
        ElseIf TryNextRun = True And TryAgainCount <> 0 Then
            TryNextRun = False
            Debug.Print 3
        End If


        Loop

    Else
        MsgBox "invalid 'Ping every'/'try offline after' integer"
    End If

End If

End Sub

It's a bit messy I know :-)


回答1:


Beacuse all excel sheets are running on one thread (one Excel.exe instance, you can see one presence in task manager).

If you are running more excel instance, your sheet are working independently.

You can do one of these possibilities :

-simple open new Excel.exe from start menu, icon, etc

-windows tray excel icon right click then alt+click on Microsoft Excel

-start command (or shortcut or batch file): Excel.exe "xls path" /x

-vba

Sub OpenNewExcelInstance()

Dim xlApp As Excel.Application
Set xlApp = New Excel.Application
xlApp.Workbooks.Add
xlApp.Visible = True
Set xlApp = Nothing

End Sub

-modify your registry to force open in new instance

-modify your Personal.xlsb




回答2:


i think the easiest solution is to use the task -scheduler, and start your macro from there. In the extend properties choose "run whether user is logged on or not", then this is started in a separate task.



来源:https://stackoverflow.com/questions/60559124/excel-vba-script-stealing-focus

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!