尽管有任何后台程序运行,仍然保持用户表单窗口处于活动状态

时间:2015-08-18 12:10:29

标签: excel vba excel-vba

我每分钟都会在工作表上运行一个宏。它复制并粘贴其他工作表中的数据并更新现有图表。

提供了一个命令按钮,可以改变x&的范围。在该图表上的y系列。 当用户单击命令按钮时,将显示用户表单,以便用户输入最小和最大范围。 但是,由于宏每秒都在运行,因此用户窗体焦点在宏运行期间丢失,用户每次都必须选择用户窗体。它非常讨厌。

宏不会选择/激活工作表上的任何单元格/图表。

我如何停止用户表格失去它的重点并保持活跃,尽管后端有任何程序在运行?

1 个答案:

答案 0 :(得分:0)

这是每秒运行的代码。 '限制'是用户表单名称。

Sub doitagain()
If Len(Dir(path1 + "\mon")) <> 0 Then
            If Limits.Visible Then
            Limits.rangecb1.SetFocus
            End If

            Application.ScreenUpdating = False
            Application.EnableEvents = False
            Application.Interactive = False

            fromcopy = path1 + "\mon"
            tocopy = path + "\mon.csv"
            On Error Resume Next
            FileCopy fromcopy, tocopy
            filepath = path + "\mon.csv"
            Set wtarget = Workbooks.Open(filepath)
            wtarget.Windows(1).Visible = False

            curiteration = m - 2
            i = ii + 1
            n = 1
            Do While wtarget.Worksheets("mon").Cells(i, 2).Value <> curiteration
            i = i + 1
            n = n + 1
                If n > ThisWorkbook.Worksheets("Convergence_Plot").Cells(1, 6).Value Then
                Application.ScreenUpdating = True
                Application.EnableEvents = True
                Application.Interactive = True
                Workbooks("mon.csv").Close savechanges = False
                GoTo abc
                End If
            Loop
            j = i
            Do While wtarget.Worksheets("mon").Cells(j, 1).Value <> ""
            j = j + 1
            Loop
            j = j - 1
            mi = m
            For k = i To j
            ThisWorkbook.Worksheets("Convergence_Plot").Cells(mi, 1).Value = wtarget.Worksheets("mon").Cells(k, 2).Value
            mi = mi + 1
            Next
            lastcol = 1
            Do While wtarget.Worksheets("mon").Cells(i, lastcol).Value <> ""
            lastcol = lastcol + 1
            Loop
            lastcol = lastcol - 1
            mi = m
            For k = i To j
            ThisWorkbook.Worksheets("Convergence_Plot").Cells(mi, 4).Value = wtarget.Worksheets("mon").Cells(k, lastcol - noofsubsdoagain).Value
            mi = mi + 1
            Next
            mi = m
            For k = i To j
            ThisWorkbook.Worksheets("Convergence_Plot").Cells(mi, 3).Value = wtarget.Worksheets("mon").Cells(k, lastcol - 1 - noofsubsdoagain).Value
            mi = mi + 1
            Next
            mi = m
            For k = i To j
            ThisWorkbook.Worksheets("Convergence_Plot").Cells(mi, 2).Value = wtarget.Worksheets("mon").Cells(k, lastcol - 2 - noofsubsdoagain).Value
            mi = mi + 1
            Next

            For k = 1 To noofsubsdoagain
            mi = m
            Dim ki As Integer
            For ki = i To j
            ThisWorkbook.Worksheets("Convergence_Plot").Cells(mi, 4 + k).Value = wtarget.Worksheets("mon").Cells(ki, lastcol - noofsubsdoagain + k).Value
            mi = mi + 1
            Next
            Next
            Application.Interactive = True
            Set wtarget = Nothing
            Workbooks("mon.csv").Close savechanges = False
            mi = m
            For k = i To (j - 1)
            m = m + 1
            Next
            i = 3
            Do While ThisWorkbook.Worksheets("Convergence_Plot").Cells(i, 1).Value <> ""
            i = i + 1
            Loop
            If i > 3 Then
            i = i - 1
            End If
            Application.ScreenUpdating = True
            Application.EnableEvents = True
            Application.Interactive = True
            If ThisWorkbook.Worksheets("Convergence_Plot").Cells(1, 5).Value = 1 Then
            ThisWorkbook.Worksheets("Convergence_Plot").TextBox1.Value = i - 2
            Else
            ThisWorkbook.Worksheets("Convergence_Plot").TextBox1.Value = i - (ThisWorkbook.Worksheets("Convergence_Plot").Cells(1, 7).Value - ThisWorkbook.Worksheets("Convergence_Plot").Cells(1, 6).Value + 2)
            End If

            ThisWorkbook.Worksheets("Convergence_Plot").ChartObjects("Chart 1").Chart.FullSeriesCollection(1).XValues = "=Convergence_Plot!$A$3:$A$" & i
            ThisWorkbook.Worksheets("Convergence_Plot").ChartObjects("Chart 1").Chart.FullSeriesCollection(1).Values = "=Convergence_Plot!$B$3:$B$" & i
            ThisWorkbook.Worksheets("Convergence_Plot").ChartObjects("Chart 1").Chart.FullSeriesCollection(2).Values = "=Convergence_Plot!$C$3:$C$" & i
            ThisWorkbook.Worksheets("Convergence_Plot").ChartObjects("Chart 1").Chart.FullSeriesCollection(3).Values = "=Convergence_Plot!$D$3:$D$" & i
            Dim letter As String
            letter = "D"
            For k = 1 To 10
            ThisWorkbook.Worksheets("Convergence_Plot").ChartObjects("Chart 1").Chart.FullSeriesCollection(3 + k).Values = "=Convergence_Plot!$" & Chr(Asc(letter) + k) & "$3:$" & Chr(Asc(letter) + k) & "$" & i
            Next

            If Limits.Visible Then
            Limits.rangecb1.SetFocus
            End If
End If
abc:
timetorun = Now + TimeValue("00:00:03")
Application.OnTime timetorun, "doitagain", , True
End Sub