Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim ws As Worksheet
Dim y As String
Set ws = Sheets(1) 'change sheet number to suit.
ws.Name = ws.Range("A1").Value 'change range to suit.
y = ws.Name
MsgBox "the worksheet name is now " & y
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim y$: y = [A1] 'change range to suit.
If Me.Name = y Or y = "" Then Exit Sub
On Error Resume Next
Me.Name = y
If Err.Number > 0 Then
MsgBox y & " is not a valid sheet name."
Else: MsgBox "The worksheet name is now " & y
End If
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
Dim y As Range: Set y = [A1] 'change range to suit.
If Not Intersect(Target, y) Is Nothing And y <> "" Then
On Error Resume Next
Me.Name = y
If Err.Number > 0 Then
MsgBox y & " is not a valid sheet name."
Else: MsgBox "The worksheet name is now " & y
End If
End If
End Sub
Sub RenameSheets()
Dim ws As Worksheet, x%, y%
x = 1
For y = 1 To Worksheets.Count
For Each ws In Worksheets
ws.Name = ws.[A2] & " - " & ws.[C2]
x = x + 1
Next
Next
Sheets(1).Select
For i = 1 To Sheets.Count
Sheets(i).Activate
Range("A1").Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlToRight)).Select
If ActiveSheet.ListObjects.Count < 1 Then
ActiveSheet.ListObjects.Add.Name = ActiveSheet.Name
End If
Next i
Dim wb As Workbook
Dim ws As Worksheet
Dim lo As ListObject
Dim sName As String
Dim sFormula As String
Dim wq As WorkbookQuery
Dim bExists As Boolean
Set wb = ActiveWorkbook
'Loop sheets and tables
For Each ws In ActiveWorkbook.Worksheets
For Each lo In ws.ListObjects
sName = lo.Name
sFormula = "Excel.CurrentWorkbook(){[Name=""" & sName & """]}[Content]"
'Check if query exists
bExists = False
For Each wq In wb.Queries
If InStr(1, wq.Formula, sFormula) > 0 Then
bExists = True
End If
Next wq
'Add query if it does not exist
If bExists = False Then
'Add query
wb.Queries.Add Name:=sName, _
Formula:="let" & Chr(13) & "" & Chr(10) & " Source = Excel.CurrentWorkbook(){[Name=""" & sName & """]}[Content]" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " Source"
'Add connection
wb.Connections.Add2 Name:="Query - " & sName, _
Description:="Connection to the '" & sName & "' query in the workbook.", _
ConnectionString:="OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=" & sName & ";Extended Properties=""""", _
CommandText:="SELECT * FROM [" & sName & "]", _
lCmdtype:=2, _
CreateModelConnection:=False, _
ImportRelationships:=False
End If
'Count connections
i = i + 1
Next lo
Next ws
End Sub
Dim ws As Worksheet, x%, y%
x = 1
For y = 1 To Worksheets.Count
For Each ws In Worksheets
ws.Name = ws.[A2] & " - " & ws.[C2]
x = x + 1
Next
Next
Dim ws As Worksheet
For Each ws In Worksheets
ws.Name = ws.[A2] & " - " & ws.[C2]
Next