0

I have 3 workbook web queries on a single data sheet , and I have a dropdown object with a list of months in the year (1-12). My idea for the automation was to have the query formula update based on the user selection of the dropdown value and update the query formula accordingly and refresh.

The VBA code works fine, but I get this message for one of the queries.

enter image description here

The reason is that the query (ex when changed from month 2 to month 3), has 2 more lines and is not the exact table height.

Any ideas how to debug this / circumvent this message. Code below:

Sub DropDown9_Change()

Dim wbconn As WorkbookConnection, qT As QueryTable
Dim wB As Workbook, wS As Worksheet

'For Each wbconn In ThisWorkbook.Connections
'Debug.Print wbconn.Name & " - " & wbconn.OLEDBConnection.CommandText & " - " & _
'wbconn.OLEDBConnection.SourceDataFile
''wbconn.Refresh
'Next wbconn

Set wB = Workbooks("OH Burdening Template.xlsb")

If ShData.Shapes("Drop Down 9").ControlFormat.Value > _
ShCalendar.Range("B3").Value Then

    MsgBox "Cannot be based on future periods!", vbExclamation

Else
    'Refresh WB queries
    Call Refresh_Queries(ShData.Shapes("Drop Down 9").ControlFormat.Value, wB, ShData)

End If

ShData.Columns.AutoFit

End Sub


Private Function Refresh_Queries(ByVal Period As Integer, ByVal wB As Workbook, _
ByVal thisSheet As Worksheet)

With Application
.StatusBar = "Now refreshing queries on :" & ShData.Name
.ScreenUpdating = False
.EnableEvents = False
End With

Dim I As Integer, LObj As ListObject
Dim strL As Integer, str As String
Dim Pos As Integer
Dim F As String
Dim startPos As Integer

str = "?year=2018&period="
strL = Len(str)

For I = 1 To wB.Queries.Count

    On Error GoTo view_err

    F = wB.Queries(I).Formula
    Pos = VBA.InStr(1, F, str, vbBinaryCompare)
    startPos = Pos + strL

    'Debug.Print F
    'Replacing the period part of the string with the period entered in the dropdown

    F = WorksheetFunction.Replace(F, startPos, 1, Period)
    wB.Queries.Item(I).Formula = F

    'Debug.Print Mid(F, startPos, 1)

Next I

For Each LObj In thisSheet.ListObjects
    Application.StatusBar = "Refreshing " & LObj.Name
    LObj.QueryTable.Refresh False
    Debug.Print LObj.Name & Chr(32) & "Refreshed successfully!"
Next LObj

With Application
.StatusBar = False
.ScreenUpdating = True
.EnableEvents = True
End With

Set LObj = Nothing

Exit Function

view_err:
Debug.Print LObj.Name & Chr(32) & "Refresh Failed!"
MsgBox Err.Description & " (" & Err.Number & ")", vbExclamation

With Application
.StatusBar = False
.ScreenUpdating = True
.EnableEvents = True
End With

End Function
braX
  • 11,506
  • 5
  • 20
  • 33
Mike Mirabelli
  • 402
  • 3
  • 16
  • Application.DisplayAlerts = False before and set to true at end if otherwise your code is fine? – QHarr Mar 21 '18 at 18:25
  • @QHarr, do you mean before the call to the function? Because I disable events in my function code. – Mike Mirabelli Mar 21 '18 at 18:26
  • 1
    Excel needs to resize the table because there's new data (etc) but it can't because you have other stuff in the way. – ashleedawg Mar 21 '18 at 18:39
  • "I have 3 web queries on a single data sheet" - a while back I started treating Excel sheets as data entities: one data sheet only ever contains one single table. Never had this kind of problem ever since. Is there any particular reason why the 3 tables need to be on the same sheet? – Mathieu Guindon Mar 21 '18 at 19:01
  • @ashleedawg I understand there is new data. I tried changing the refresh style to xlInsertEntireRows , but I'm wondering if I should change it to xlOverwritecells. – Mike Mirabelli Mar 21 '18 at 19:03
  • @MathieuGuindon Not really I could technically place them on different sheets, but the idea was to minimize the number of tabs on my template, so because they are small tables I ideally want them all on one sheet. – Mike Mirabelli Mar 21 '18 at 19:04
  • I hear ya. If they's just data that the rest of the workbook uses, I'd make them hidden sheets - then it doesn't matter how many sheets there are :) – Mathieu Guindon Mar 21 '18 at 19:05
  • Is there no property / fix for this issue? This is bugging me :S – Mike Mirabelli Mar 21 '18 at 19:10
  • 1
    The table needs to expand to accomodate the query results, but as @ashleedawg noted, another table is in the way. You need to make sure there's sufficient available space to refresh. IMO the best way to ensure that is to only ever have 1 table per worksheet. YMMV, but at the end of the day, you need extra columns (rows?) somewhere. – Mathieu Guindon Mar 21 '18 at 19:27
  • 1
    agreed - always isolate each raw data source away from formatting, formulas, and other data sources. – ashleedawg Mar 21 '18 at 20:28
  • Ok thanks I guess I will put in on a separate sheet and try that . – Mike Mirabelli Mar 21 '18 at 20:28

0 Answers0