1

Before asking this question I have already tried solutions previously stated in the forum. Have Excel VBA wait for PowerQuery data refresh to continue and How to wait for a Power Query refresh to finish? I've tried "DoEvents" with "BackgroundQuery = False", and more, nothing works

My goal: Refresh the table, format table(resize, align, and hide data), replace blank cells with "-". The format and replace blank procedures are written separately which I call after query refresh. When I step through the code everything works perfect. However, when I run the macro the query refreshes and blanks are replaced, but the data is not formatted how I specified.

I'm not sure if this is due to a flaw in the code, a byproduct of working on a slow network, or me just being a rookie at all of this and not realizing what I'm doing wrong.

Solutions I've tried: 1. Disable "Background Refresh" in the properties section. 2. Wrote the refresh piece both ways:

With ActiveWorkbook.Connections("Query - QueryName").OLEDBConnection
    brfresh = .BackgroundQuery
    .BackgroundQuery = False
    .Refresh
    .BackgroundQuery = brfresh

End With

and

ActiveWorkbook.Connections("Query - QueryName").Refresh

I also tried adding an application.wait time of 5 seconds after refresh.

Here is the Module if this helps:

Sub RefreshMRIQuery()

   Dim brfresh As Boolean
   Dim StartT As Date
   Dim EndT As Date
   StartT = Now
   EndT = StartT + TimeValue("00:00:05")

    Sheets("MRI").Select
    Range("A1").Select

   With ActiveWorkbook.Connections("Query - MASTER ROLLER INPUT").OLEDBConnection
        brfresh = .BackgroundQuery
        .BackgroundQuery = False
        .Refresh
        .BackgroundQuery = brfresh

    End With

    Application.Wait EndT

    Range("A1").Select
    RemoveBlanks
    Range("A1").Select
    FormatMRITable
    Range("A1").Select


End Sub


Sub FormatMRITable()

    Columns("B:B").Select
    Selection.EntireColumn.Hidden = True
    Columns("C:C").Select
    Selection.EntireColumn.Hidden = True
    Rows("1:1").RowHeight = 44.25
    Range("MASTER_ROLLER_INPUT").Select
    With Selection
        .HorizontalAlignment = xlGeneral
        .VerticalAlignment = xlCenter
        .WrapText = False
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .ReadingOrder = xlContext
        .MergeCells = False
    End With
    With Selection
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlCenter
        .WrapText = False
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .ReadingOrder = xlContext
        .MergeCells = False
    End With
    With Selection
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlBottom
        .WrapText = False
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .ReadingOrder = xlContext
        .MergeCells = False
    End With
    With Selection
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlCenter
        .WrapText = False
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .ReadingOrder = xlContext
        .MergeCells = False
    End With
    Columns("G:G").ColumnWidth = 10
    Columns("G:G").ColumnWidth = 13.57
    Columns("H:H").ColumnWidth = 11.57
    Columns("I:I").ColumnWidth = 11.29
    Columns("I:I").ColumnWidth = 14
    Columns("J:J").ColumnWidth = 12.71
    Range("A2").Select
End Sub


Public Sub RemoveBlanks()
Dim MRI As ListObject
Set MRI = Worksheets("MRI").ListObjects("MASTER_ROLLER_INPUT")
Dim r As Range

    For Each r In MRI.DataBodyRange
        If r.Value = "" Then r.Value = "-"
    Next r

End Sub

Sorry if the formatting isn't correct and/or if I have unnecessary steps-I've been learning from scratch for about a month and Google has been the source of all my programming knowledge. If I need to elaborate on anything please let me know, thanks!

Connor
  • 21
  • 1
  • 4

0 Answers0