Vba 循环标题,并为重复的值提供选定的、不同的名称

Vba 循环标题,并为重复的值提供选定的、不同的名称,vba,excel,Vba,Excel,我有一个导出表,可以多次重复使用相同的列名。导出的结构使我知道哪个重复值适用于哪个级别,但我很难在范围内循环并更新值。下面的代码要么将每个头都设置为第一个案例场景,要么什么都不设置 以下是我目前掌握的情况: Dim i As Integer Dim lastCol As Long Dim Cell As Range Dim lr As String Dim lrb As Integer lr = "1st level Close Record" lrb = 1 lastCol = Cells(

我有一个导出表,可以多次重复使用相同的列名。导出的结构使我知道哪个重复值适用于哪个级别,但我很难在范围内循环并更新值。下面的代码要么将每个头都设置为第一个案例场景,要么什么都不设置

以下是我目前掌握的情况:

Dim i As Integer
Dim lastCol As Long
Dim Cell As Range
Dim lr As String
Dim lrb As Integer

lr = "1st level Close Record"
lrb = 1
lastCol = Cells(1, Columns.Count).End(xlToLeft).Column
Debug.Print lastCol

For Each Cell In Range(Cells(1, 1), Cells(1, lastCol))
    Select Case Cell.Value = "Close Record"
        Case lrb = 1
            Debug.Print Cell.Value
            Cell.Value = lr
            lr = "2nd Level Close Record"
            lrb = 2
        Case lrb = 2
            Cell.Value = lr
            lr = "3rd Level Close Record"
            lrb = 3
        Case lrb = 3
            Cells.Value = lr
    End Select
Next

下面的代码经过测试,可以正常工作。注意下面与你的逻辑上的不同

Option Explicit

Sub Test()

Dim i As Integer
Dim lastCol As Long
Dim Cell As Range
Dim lr As String
Dim lrb As Integer

lr = "1st level"
lrb = 1
lastCol = Cells(1, Columns.Count).End(xlToLeft).Column
Debug.Print lastCol

For Each Cell In Range(Cells(1, 1), Cells(1, lastCol))
    Select Case Cell.Value = "Close Record"
        Case True
            Select Case lrb
                Case Is = 1
                    Cell.Value = lr & " " & Cell.Value
                    lr = "2nd Level"
                    lrb = 2
                Case Is = 2
                    Cell.Value = lr & " " & Cell.Value
                    lr = "3rd level"
                    lrb = 3
                Case Is = 3
                    Cell.Value = lr & " " & Cell.Value
            End Select
    End Select
Next

End Sub

我的日子不好过,到底是什么问题?你写的不清楚。看见您是否在预期的工作表上运行此代码?否则,它可能会失败。将所有变量限定为其父对象<代码>单元格到
工作表(“mySheet”)。单元格
其中mySheet=您的工作表名称您的代码甚至不编译:(
Dim Cell as String
loop@Gregory:您的接受率很低,只有29%。因此不必麻烦您。请花些时间阅读帮助页,特别是名为和的部分。更重要的是,请阅读。您还需要了解。@ScottHoltzman感谢您的提醒--已更新我的帖子和上面的代码。我正在所选的工作表上运行代码。我想我理解了背后的逻辑:首先将单元格设置为范围。然后,嵌套所选的大小写。首先,它检查单元格。Value=“Close Record”是真的。如果是真的,它将开始另一个select案例,检查lrb设置为什么,如果lrb等于该特定数字,则执行相关代码。对吗?没错!@Gregory