Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Excel中重命名列标题_Excel_Vba - Fatal编程技术网

在Excel中重命名列标题

在Excel中重命名列标题,excel,vba,Excel,Vba,我在工作表中重复了相同的列,例如 单位ID 单位名称 好几次 我希望这些列标题根据它们的出现情况进行更改。 例如,第一次出现的单元ID将被替换为单元ID_1,第二次出现的将被重命名为单元ID_2,依此类推。 任何帮助都会很好 您将这些标题排成一行,对吗?声明某个整数变量:Dim i为integer并将其设置为1i=1,在循环中遍历该行,每次遇到指定的标题时,递增i,并将其附加到名称中: If Cells(1, j).Value = "Unit ID" Then 'assumed that f

我在工作表中重复了相同的列,例如

  • 单位ID
  • 单位名称
好几次

我希望这些列标题根据它们的出现情况进行更改。
例如,第一次出现的
单元ID
将被替换为
单元ID_1
,第二次出现的将被重命名为
单元ID_2
,依此类推。

任何帮助都会很好

您将这些标题排成一行,对吗?声明某个整数变量:
Dim i为integer
并将其设置为1
i=1
,在循环中遍历该行,每次遇到指定的标题时,递增
i
,并将其附加到名称中:

If Cells(1, j).Value = "Unit ID" Then 'assumed that first row contains headers and j is loop variable
    Cells(1, j).Value = Cells(1, j).Value & "_" & i
    i = i + 1
End If

你把这些标题排成一行,对吗?声明某个整数变量:
Dim i为integer
并将其设置为1
i=1
,在循环中遍历该行,每次遇到指定的标题时,递增
i
,并将其附加到名称中:

If Cells(1, j).Value = "Unit ID" Then 'assumed that first row contains headers and j is loop variable
    Cells(1, j).Value = Cells(1, j).Value & "_" & i
    i = i + 1
End If
试试这个:

Option Explicit

Sub Demo()
    Dim lastCol As Long, idCount As Long, nameCount As Long, headerRow As Long
    Dim rng As Range, cel As Range

    headerRow = 1       'row number with headers
    lastCol = Cells(headerRow, Columns.Count).End(xlToLeft).Column 'last column in header row
    idCount = 1
    nameCount = 1
    Set rng = Sheets("Sheet1").Range(Cells(headerRow, 1), Cells(headerRow, lastCol)) 'header range

    For Each cel In rng                     'loop through each cell in header
        If cel = "Unit ID" Then             'check if header is "Unit ID"
            cel = "Unit ID_" & idCount      'rename "Unit ID" using idCount
            idCount = idCount + 1           'increment idCount
        ElseIf cel = "Unit Name" Then       'check if header is "Unit Name"
            cel = "Unit Name_" & nameCount  'rename "Unit Name" using nameCount
            nameCount = nameCount + 1       'increment nameCount
        End If
    Next cel
End Sub
试试这个:

Option Explicit

Sub Demo()
    Dim lastCol As Long, idCount As Long, nameCount As Long, headerRow As Long
    Dim rng As Range, cel As Range

    headerRow = 1       'row number with headers
    lastCol = Cells(headerRow, Columns.Count).End(xlToLeft).Column 'last column in header row
    idCount = 1
    nameCount = 1
    Set rng = Sheets("Sheet1").Range(Cells(headerRow, 1), Cells(headerRow, lastCol)) 'header range

    For Each cel In rng                     'loop through each cell in header
        If cel = "Unit ID" Then             'check if header is "Unit ID"
            cel = "Unit ID_" & idCount      'rename "Unit ID" using idCount
            idCount = idCount + 1           'increment idCount
        ElseIf cel = "Unit Name" Then       'check if header is "Unit Name"
            cel = "Unit Name_" & nameCount  'rename "Unit Name" using nameCount
            nameCount = nameCount + 1       'increment nameCount
        End If
    Next cel
End Sub

得到答案,下面是代码-

    Dim wbCurrent As Workbook
    Dim wsCurrent As Worksheet
    Dim nLastCol, i, j, k As Integer
    Set wbCurrent = ActiveWorkbook
    Set wsCurrent = wbCurrent.ActiveSheet
    nLastCol = wsCurrent.Cells.Find("*", LookIn:=xlValues, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
    j = 0
For i = nLastCol To 1 Step -1
    j = j + 1
   ' k = InStr(1, wsCurrent.Cells(1, i).Value, "Sweep TID", vbTextCompare) > 0
        If InStr(1, wsCurrent.Cells(1, i).Value, "unitid", vbTextCompare) = 0 Then
            wsCurrent.Cells(1, i).Value = "unitid_" & j
        End If
        If InStr(1, wsCurrent.Cells(1, i).Value, "UnitName", vbTextCompare) > 0 _
        Then
            wsCurrent.Cells(1, i).Value = "UnitName_" & j
        End If
    Next i

上面的代码用数据标识最后一列,并在循环中使用它

得到了答案,下面是代码-

    Dim wbCurrent As Workbook
    Dim wsCurrent As Worksheet
    Dim nLastCol, i, j, k As Integer
    Set wbCurrent = ActiveWorkbook
    Set wsCurrent = wbCurrent.ActiveSheet
    nLastCol = wsCurrent.Cells.Find("*", LookIn:=xlValues, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
    j = 0
For i = nLastCol To 1 Step -1
    j = j + 1
   ' k = InStr(1, wsCurrent.Cells(1, i).Value, "Sweep TID", vbTextCompare) > 0
        If InStr(1, wsCurrent.Cells(1, i).Value, "unitid", vbTextCompare) = 0 Then
            wsCurrent.Cells(1, i).Value = "unitid_" & j
        End If
        If InStr(1, wsCurrent.Cells(1, i).Value, "UnitName", vbTextCompare) > 0 _
        Then
            wsCurrent.Cells(1, i).Value = "UnitName_" & j
        End If
    Next i

上面的代码用数据标识最后一列,并在循环中使用它

我在下面的代码中尝试过,但同样的代码似乎不起作用,也没有引发任何错误-我在下面的代码中尝试过,但同样的代码似乎不起作用,也没有引发任何错误-注意,这是一个关于为什么应该这样做的好文本。注意,下面是一个关于你为什么应该这样做的好文章。嗨,Mrig,谢谢。但是什么是cel?因为我们还没有定义它。因此,程序运行时不会出错,但不会更改value@AP1979-
cel
是类型为
Range
的变量。其声明在代码顶部,请参见
Dim rng As Range,cel As Range
。您好,谢谢。但是什么是cel?因为我们没有定义它。因此,程序运行时不会出错,但不会更改value@AP1979-
cel
是类型为
Range
的变量。其声明位于代码顶部,请参见
Dim rng As Range,cel As Range