mickeystanford_alumni
Board Regular
- Joined
- May 11, 2022
- Messages
- 129
- Office Version
- 2021
- Platform
- Windows
- MacOS
Hi, I have a file which imports a query through power query, pretty simple query with a file directly. Then, in this data I use a vba code to automate the named range columns, by telling the code to name the range of the column with the concatenate of the first, second, and third row. The code works perfectly with one query, however, when I implement this code to a bigger file with more than 1 queries, it just bugs and takes ages to get executed, so I have to cancel.
Could anyone help me on how to solve it?
Could anyone help me on how to solve it?
VBA Code:
Sub UpdateWorkbookAndRefreshQueries()
Dim ws As Worksheet
Dim wb As Workbook
Dim lastCol As Integer
Dim i As Integer
Dim colName As String
Dim namedRange As Range
Dim validName As String
Dim name As name
Dim conn As WorkbookConnection
Set wb = ThisWorkbook
Set ws = wb.Sheets("DATAPACK") 'NAME OF SHEET
lastCol = ws.Cells(1, ws.Columns.Count).End(xlToLeft).Column
For i = 1 To lastCol
colName = ws.Cells(1, i).Value & "." & ws.Cells(2, i).Value & "." & ws.Cells(3, i).Value
validName = Replace(colName, " ", ".")
Set namedRange = ws.Range(ws.Cells(1, i), ws.Cells(ws.UsedRange.Rows.Count, i))
wb.Names.Add name:=validName, RefersTo:=namedRange
On Error GoTo 0
Next i
For Each conn In wb.Connections
conn.Refresh
Next conn
MsgBox "Columns named and queries refreshed successfully.", vbInformation
End Sub
Last edited by a moderator: