Refresh certain queries if the value of functions changes

北慕城南 提交于 2020-06-17 15:45:11

问题


The manual entry of one of the following numerical values ​​will be entered in cell "AC1": 1; 2; 3; 4 or 5. Each of them represents a range of cells that will take priority among all the others. Each of the cells in the range "A2:A86" contains a function with a similar structure: = ‘SheetX’AX! . For example: = ‘Sheet1’B4! ; = ‘Sheet2’B4! ; = ‘Sheet6’B4! ; etc.

The ranges of cells that form groups are: Range 1 = "A2:A18"; range 2 = "A19:A35"; range 3 = "A36:A52", range 4 = "A53:A69"; and range 5 = "A70:A86". For example: By manually entering the value "2” in cell "AC1”, then I am exclusively forcing to detect changes in the functions of the range "A19:A35". Changes that may occur in another range would not be of interest to me at this time.

From this range of cells I need to detect the cell that meets two conditions: “> = 5”; maximum value.

I continue with the previous example: If "A20" = 6.80; "A31" = 7.01, the remaining cells of the range “<5”, then the cell of interest will be "A31".

The objective of my article is to assign the task to the cell that meets all the previous conditions the task of refreshing some queries of my interest.

I continue with the example: Then the queries would be constantly refreshed in a loop: “Query - Query6”; "Query - Consulta9"; "Query - Consulta10". The remaining queries in my workbook would not refresh.

If the cells in the range determined in “AC1” do not meet the condition: “> = 5”, then All queries in my workbook would be loop refreshed, I don't want to have to wait any time for query refresh to occur.

Note:

  • My workbook contains 20 queries.
  • The number of queries to refresh is 3, if the established conditions are met.
  • The data entry in "A2:A86" is through functions. There is no manual entry in this range.
  • The data entry in "AC1" is manual.

The following VBA code can work as a model to achieve what you want.

Disadvantages:

  • No change in function values ​​is detected. It only refreshes queries if the entry of values ​​is done manually.
  • Query refreshes are time-bound and loops do not occur.
  • Cell “AC1” is not taken into account. In short, this code must be adapted to the requested situation.
Private Sub Worksheet_Change(ByVal Target As Range)

    If Not Application.Intersect(Target, Worksheets("Conclusion").Range("B1:B2")) Is Nothing Then
        set_refreshPeriod
    End If

End Sub

Sub set_refreshPeriod()

Dim wks As Worksheet
Dim rng1 As Range, rng2 As Range
Dim lngPeriod As Long
Dim i As Integer
Dim internalQuerys As Variant 'The 20 Querys which update all 2 Minutes
Dim externalQuerys As Variant 'The 3 Querys which update if B1>B2

Set wks = ActiveWorkbook.Worksheets("Conclusion")
Set rng1 = wks.Range("B1")
Set rng2 = wks.Range("B2")

'The name of your Query always starts wiht "Query - " in english or "Abfrage - " in German
'You can add here more Querys
internalQuerys = Array("Abfrage - Tabelle1", _
                        "Abfrage - Tabelle2", _
                        "Abfrage - Tabelle3")

externalQuerys = Array("Abfrage - File1", _
                        "Abfrage - File2", _
                        "Abfrage - File3")

    'if B1 > B2 then do not refresh the internalQuerys every 2 Minutes else do it
    If rng1 > rng2 Then
        lngPeriod = 0 'do not refresh internalQuerys

        'Refresh the the external Querys
        For i = 0 To UBound(externalQuerys)
            ThisWorkbook.Connections(externalQuerys(i)).OLEDBConnection.Refresh
        Next

    Else: lngPeriod = 2 'Time 2 Minutes for internal Querys
    End If

    'Set the RefreshPeriod for the internal Querys
    For i = 0 To UBound(internalQuerys)
        ThisWorkbook.Connections(internalQuerys(i)).OLEDBConnection.RefreshPeriod = lngPeriod
    Next

End Sub

来源:https://stackoverflow.com/questions/62066980/refresh-certain-queries-if-the-value-of-functions-changes

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