[英]Rename Multiple sheets in Excel with cell value from same sheet in VBA
I am currently working on a VBA project.我目前正在研究 VBA 项目。 I have a workbook with multiple tabs from different workbooks.
我有一个工作簿,其中包含来自不同工作簿的多个选项卡。 The names of all the tabs are the same, however since they come from different files, I would like to name them based on the filenames they are extracted from.
所有选项卡的名称都是相同的,但是由于它们来自不同的文件,我想根据提取它们的文件名来命名它们。 The filenames are present in the cell EC1 of every tab.
文件名存在于每个选项卡的单元格 EC1 中。 I would like to name all the sheets in the workbook based on the value present in cell EC1 of each individual sheet.
我想根据每个工作表的单元格 EC1 中存在的值来命名工作簿中的所有工作表。
I have the following code:我有以下代码:
Sub RenameSheet()
Dim rs As Worksheet
For Each rs In Sheets
rs.Name = rs.Range("EC1")
Next rs
End Sub
I have been getting a 1004 error from the above code.我从上面的代码中得到了 1004 错误。
I tried this code too:我也试过这段代码:
Sub RenameSheet()
Dim xWs As Worksheet
Dim xRngAddress As String
Dim xName As String
Dim xSSh As Worksheet
Dim xInt As Integer
xRngAddress = Application.ActiveCell.Address
On Error Resume Next
Application.ScreenUpdating = False
For Each xWs In Application.ActiveWorkbook.Sheets
xName = xWs.Range(xRngAddress).Value
If xName <> "" Then
xInt = 0
Set xSSh = Nothing
Set xSSh = Worksheets(xName)
While Not (xSSh Is Nothing)
Set xSSh = Nothing
Set xSSh = Worksheets(xName & "(" & xInt & ")")
xInt = xInt + 1
Wend
If xInt = 0 Then
xWs.Name = xName
Else
If xWs.Name <> xName Then
xWs.Name = xName & "(" & xInt & ")"
End If
End If
End If
Next
Application.ScreenUpdating = True
End Sub
Some sheets do get renamed, however some do not.有些工作表确实被重命名,但有些则没有。 I have checked for duplicate sheet names, and there are none.
我检查了重复的工作表名称,但没有。 I have also checked if the filename is in the correct range (cell), and it is present.
我还检查了文件名是否在正确的范围(单元格)内,并且它存在。
There might be problems with the value if it contains some special characters.如果该值包含一些特殊字符,则可能存在问题。 The excel sheets can have some restrictions for their names, if thats the problem, my code could be the solution.
excel 表的名称可能有一些限制,如果这是问题,我的代码可能是解决方案。 It cuts the string to a maximum length of 31 chars and deletes all the special chars which are not allowed in names.
它将字符串剪切到最大长度为 31 个字符,并删除名称中不允许的所有特殊字符。
Sub RenameSheet()
Dim rs As Worksheet
For Each rs In Sheets
sheetName = without_special_chars(rs.Range("EC1").Value)
If Len(sheetName) > 31 Then
sheetName = Left(sheetName, 31)
End If
rs.Name = sheetName
Next rs
End Sub
Function without_special_chars(text As String) As String
Dim i As Integer
Const special_chars As String = "-.,:;#+ß'*?=)(/&%$§!~\}][{"
For i = 1 To Len(special_chars)
text = Replace(text, Mid(special_chars, i, 1), "")
Next i
without_special_chars = text
End Function
A Quick Fix快速修复
Your first code should have been something like this:您的第一个代码应该是这样的:
Sub renameWorksheetsQF() Dim ws As Worksheet For Each ws In ThisWorkbook.Worksheets ws.Name = ws.Range("EC1").Value Next ws End Sub
Note the not so subtile differences.请注意不那么微妙的差异。
In Depth深入
Option Explicit
Sub renameWorksheets()
On Error GoTo clearError
Const cAddress As String = "A1" ' "EC1"
Dim wb As Workbook: Set wb = ThisWorkbook ' workbook containing this code
Dim ws As Worksheet
Dim cel As Range
Dim oName As String
Dim nName As String
For Each ws In wb.Worksheets
oName = ws.Name
Set cel = ws.Range(cAddress)
If IsError(cel) Then
Debug.Print "Cell '" & cAddress & "' in worksheet '" _
& oName & "' contains the error value '" & cel.Text & "'."
Else
If IsEmpty(cel) Then
Debug.Print "Cell '" & cAddress & "' in worksheet '" _
& oName & "' is an empty cell."
Else
nName = CStr(cel.Value)
On Error GoTo RenameError
If oName <> nName Then
ws.Name = nName
Else
Debug.Print "Worksheet '" & oName _
& "' had previously been renamed."
End If
On Error GoTo clearError
End If
End If
Next ws
ProcExit:
Exit Sub
RenameError:
Debug.Print "Run-time error '" & Err.Number & "': " & Err.Description
Debug.Print " Could not rename '" & oName & "' to '" & nName & "'."
Resume Next
clearError:
Debug.Print "Run-time error '" & Err.Number & "': " & Err.Description
Debug.Print " Unexpected error."
Resume ProcExit
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.