(Excel)如何添加工作表名称作为每个列标题的前缀?

我有一个标题,在E列开始,可能会继续超过100列。

我试图改变每个列标题添加一个前缀(“选项卡”aka。工作表的名称) (即如果工作表被称为“饮料”,我想每个列标题前缀“饮料 – ” )

我将跨越多个工作表运行脚本,所以我试图find引用当前工作表名称的方法。


之前:(对于“饮料”工作表)

在这里输入图像说明

之后:(对于工作表“饮料”。注意:列不需要resize,只是做了演示) 在这里输入图像说明


我尝试过从这个线程调整代码,但是我不能得到它的工作。

这是我迄今为止的代码(非工作):

Sub Worksheet_Name_Prefix() Dim columnNumber As Long, x As Integer Dim myTab As ListObject Set myTab = ActiveSheet.ListObjects(rows.Count, 1) For x = 5 To rows.Count ' For Columns E through last header cell with value columnNumber = x myTab.HeaderRowRange(1, columnNumber) = ActiveSheet.Name Next x End Sub 

任何关于我的代码有什么问题的build议? 任何帮助将不胜感激。

我希望这可以帮助你…

 Sub Worksheet_Name_Prefix_v2() Dim h 'to store the last columns/header Dim rngHeaders As Range 'the whole range with the headers from E1 to x1 Dim i 'just and index Dim sht As Worksheet 'the sheet where you want the job done h = Range("E1").End(xlToRight).Column 'find the last column with the data/header Set rngHeaders = Range(Cells(1, 5), Cells(1, h)) 'the range with the headers E = column 5 'Cells 1 5 = E1 'Cells 1 h = x1 where x is the last column with data Set sht = ActiveSheet 'the sheet with the data, _ 'and we take the name of that sheet to do the job For Each i In rngHeaders 'for each cell in the headers (every cells in row 1) i.Value = sht.Name & " - " & i.Value 'set the value "sheet_name - cell_value" in every cell Next i End Sub 

如果你需要任何改进,请告诉我…我不知道我是否真正了解你所需要的东西。

编辑#1

在常规模块中使用这个:

 Option Explicit Sub goForEverySheet() Dim noSht01 As Worksheet 'store the first sheet Dim noSht02 As Worksheet 'store the second sheet Dim sht 'just a tmp var Set noSht01 = Sheets("AA") 'the first sheet Set noSht02 = Sheets("Word Frequency") 'the second sheet appTGGL bTGGL:=False For Each sht In Worksheets ' for each sheet inside the worksheets of the workbook If sht.Name <> noSht01.Name And sht.Name <> noSht02.Name Then 'IF sht.name is different to AA AND sht.name is diffent to WordFrecuency THEN 'TIP: 'If Not sht.Name = noSht01.Name And Not sht.Name = noSht02.name Then 'This equal 'IF (NOT => negate the sentence) sht.name is NOT equal to noSht01 AND ' sht.name is NOT equal to noSht02 THEN sht.Activate 'go to that Sheet! Worksheet_Name_Prefix_v3 'run the code End If ' Next sht 'next one please! appTGGL End Sub Sub Worksheet_Name_Prefix_v3() Dim h 'to store the last columns/header Dim rngHeaders As Range 'the whole range with the headers from E1 to x1 Dim i 'just and index Dim sht As Worksheet 'the sheet where you want the job done h = Range("E1").End(xlToRight).Column 'find the last column with the data/header Set rngHeaders = Range(Cells(1, 5), Cells(1, h)) 'the range with the headers E = column 5 'Cells 1 5 = E1 'Cells 1 h = x1 where x is the last column with data Set sht = ActiveSheet 'the sheet with the data, _ 'and we take the name of that sheet to do the job For Each i In rngHeaders 'for each cell in the headers (every cells in row 1) i.Value = sht.Name & " - " & i.Value 'set the value "sheet_name - cell_value" in every cell Next i End Sub Public Sub appTGGL(Optional bTGGL As Boolean = True) Debug.Print Timer Application.ScreenUpdating = bTGGL Application.EnableEvents = bTGGL Application.DisplayAlerts = bTGGL Application.Calculation = IIf(bTGGL, xlCalculationAutomatic, xlCalculationManual) End Sub 

你的代码没有运行,因为你不使用这行sht.Activate你说, for every sheet in the workbook do this ,但不是说要去每张纸,代码在同一张纸上运行n次(尽可能多的工作表中有两张)。 但是,如果你说, for every sheet do this ,并得到了每一张床单,做到这一点(less两张),你会得到你想要的