我有一个导出表,多次重复使用相同的列名。导出的结构是我知道哪个重复值适用于哪个级别,但是我很难在循环中循环并更新值。发生的事情是,下面的代码要么将每个标题设置为第一个Case方案,要么根本不设置任何内容。
这是我到目前为止所拥有的:
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
答案 0 :(得分:2)
下面的代码经过测试并且有效。注意下面与你的逻辑差异。
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