根据不同选项卡上的单元格值重命名选项卡



我有一个有 14 个选项卡的工作簿。我想重命名其中 4 个选项卡。要重命名的选项卡将使用不包含非法字符或长度限制的其他选项卡上的单元格值。

我研究了,但只发现单元格值在每个工作簿上的同一位置。

如果单元格值为空白或 0,我想隐藏选项卡。

要重命名的工作表:

  • 总结1
  • 总结 (2(
  • 总结 (3(
  • 总结 (4(

包含用于重命名的单元格值的工作表:

  • 总体总结
    • 摘要 1 的单元格 A24
    • 单元格 A25 用于摘要 (2(
    • 单元格 A26 用于摘要 (3(
    • 单元格 A27 用于摘要 (4(
Sheets("Summary 1").Name = Sheets("Overall Summary").Range("A24").Value

等。。。

对于有空白的单元格,您必须小心隐藏,因为您可能有多个。

例:

If Sheets("Overall Summary").Range("A24").Value = <> then
For Each ws In ThisWorkbook.Worksheets
If InStr(1, ws.Name, "Hide_", vbTextCompare) Then
i = i + 1
End If
Next ws
Sheets("OldName").Name = "Hide_" & i
End If

这个有望满足您的需求。

它会查看ActiveWorkbook中的每个工作表,如果名称包含"Summary"它将向下移动到"Overall Summary"工作表的第A列,并尝试获取要添加的新名称。

如果它在"Overall Summary"中查看的单元格为 0 或空白,它会将该摘要表重命名为"Hide X"但请注意,它可能只是为您隐藏工作表而不是:)

Sub RenameSummary()
Dim objRegex
Set objRegex = CreateObject("vbscript.regexp")
Dim offset As Long
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets
If InStr(LCase$(ws.name), "summary") > 0 Then
With objRegex
.Global = True
.Pattern = "[^d]+"
offset = CLng(.Replace(ws.name, vbNullString))
End With
With sheets("Overall Summary").Range("A" & (23 + offset))
If Len(.Value2) = 0 Or .Value2 = 0 Then
ws.name = "HIDE " & .row
'ws.Visible = xlSheetHidden     'can directly hide sheet
Else
ws.name = .Value2
End If
End With
End If
Next ws
End Sub

你可以试试这个

Sub Trythis()
Dim cell As Range
For Each cell in Worksheets("Overall Summary").Range("A24:A27") ‘ loop through relevant range of “Overall Summary” sheet
If Not IsEmpty(cell) And cell.Value <> 0 Then ‘ if current cell isn’t neither empty nor zero
Select Case cell.Row ‘check for current cell value and act accordingly
Case 24
Sheets("Summary 1").Name = cell.Value
Case 25
Sheets("Summary (2)").Name = cell.Value
Case 26
Smeets("Summary (3)").Name = cell.Value
Case 27
Sheets("Summary (4)").Name = cell.Value
End Select
End If
Next
End Sub

相关内容

最新更新