欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 财经 > 创投人物 > EXCELWPS工作表批量重命名(按照sheet1中A列内容)

EXCELWPS工作表批量重命名(按照sheet1中A列内容)

2024/10/25 21:16:12 来源:https://blog.csdn.net/a1b1cc1/article/details/142824138  浏览:    关键词:EXCELWPS工作表批量重命名(按照sheet1中A列内容)

将工作表名称批量重命名(按照sheet1中A列内容)

  1. 打开WPS Office的Excel文件。
  2. 按 Alt + F11 打开VBA编辑器。
  3. 在VBA编辑器中,插入一个新模块:点击 插入 -> 模块。
  4. 将以下代码粘贴到模块中:
  5. 运行→运行宏
Sub RenameSheetsBasedOnSheet1()  Dim ws As Worksheet  Dim sheet1 As Worksheet  Dim i As Long, lastRow As Long  Dim newName As String  Dim nameExists As Boolean  ' Set the sheet1 worksheet (assuming it's named "Sheet1")  Set sheet1 = ThisWorkbook.Sheets("Sheet1")  ' Find the last row with data in column A of Sheet1  lastRow = sheet1.Cells(sheet1.Rows.Count, 1).End(xlUp).Row  ' Loop through all sheets except Sheet1  For Each ws In ThisWorkbook.Sheets  If ws.Name <> sheet1.Name Then  ' Get the new name from Sheet1's A column  i = i + 1  If i <= lastRow Then  newName = sheet1.Cells(i, 1).Value  ' Check if the new name is valid and not already used  nameExists = False  For Each wks In ThisWorkbook.Sheets  If wks.Name = newName And wks.Name <> ws.Name Then  nameExists = True  Exit For  End If  Next wks  If newName <> "" And Not nameExists Then  ' Rename the sheet  On Error Resume Next ' In case of any error (e.g., invalid sheet name)  ws.Name = newName  If Err.Number <> 0 Then  MsgBox "Error renaming sheet to " & newName & ": " & Err.Description, vbCritical  Err.Clear  End If  On Error GoTo 0 ' Reset error handling  Else  If newName = "" Then  MsgBox "Empty name found in Sheet1 A" & i & ". Skipping this rename.", vbExclamation  Else  MsgBox "Name """ & newName & """ already exists. Skipping this rename.", vbExclamation  End If  End If  Else  Exit For ' No more names to assign  End If  End If  Next ws  MsgBox "Sheets have been renamed based on Sheet1 A column where possible.", vbInformation  
End Sub

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com