I am creating a automated tracker for work related data management. I have one sheet which contains all the button out of which the below is associated with the arrange macro.
Sub Arrange()
Sheets("Consolidated Sheet").Select
ActiveWorkbook.Queries.Add Name:="Invoked Function", Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Source = KMIP_Cleanup(Query2)" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " Source"
ActiveWorkbook.Queries.Add Name:="Arranged Links", Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Source = Arranged_Data(#""Invoked Function"")" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " Source"
ActiveWorkbook.Worksheets.Add
With ActiveSheet.ListObjects.Add(SourceType:=0, Source:= _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=""Invoked Function"";Extended Properties=""""" _
, Destination:=Range("$A$1")).QueryTable
.CommandType = xlCmdSql
.CommandText = Array("SELECT * FROM [Invoked Function]")
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.ListObject.DisplayName = "Invoked_Function"
.refresh BackgroundQuery:=False
End With
ActiveWorkbook.Worksheets.Add
With ActiveSheet.ListObjects.Add(SourceType:=0, Source:= _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=""Arranged Links"";Extended Properties=""""" _
, Destination:=Range("$A$1")).QueryTable
.CommandType = xlCmdSql
.CommandText = Array("SELECT * FROM [Arranged Links]")
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.ListObject.DisplayName = "Arranged_Links"
.refresh BackgroundQuery:=False
End With
Sheets("Macros").Select
Application.CommandBars("Queries and Connections").Visible = False
End Sub
Every time I click the button it creates two new sheets which is what I want but on the other hand it gives random name like Sheet 21 and Sheet 22. And after deleting it and re-running it will give Sheet 23 and Sheet 24. I want to give defined name to these sheets.