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

时间:2016-01-27 00:16:03

标签: excel vba excel-vba

我有一个标题从E列开始,可能会持续100多列。

我正在尝试更改每个列标题以添加前缀("标签" aka。工作表)的名称(即如果调用工作表'饮料',我希望每个列标题都以" Beverage为前缀 - ")

我将在多个工作表上运行脚本,所以我试图找到一种方法来引用当前的工作表名称。

之前:(适用于工作表"饮料")

enter image description here

之后:(适用于工作表"饮料"注意:列不需要调整大小,只需要进行演示) enter image description here

我尝试过调整this thread的代码,但是我无法让它工作。

以下是我到目前为止的代码(非工作):

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

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

1 个答案:

答案 0 :(得分:2)

我希望这可以帮助你...

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,但是您没有说要转到每张工作表,代码运行 {{ 1}} 在同一张表中的时间(工作簿中的许多工作表少两个)。但是,如果你说n,并且得到了其中一张,并且这样做(少于两张),你会得到你想要的