VBA在EXCEL中将标题与子标题组合在一起?

时间:2014-10-09 15:01:03

标签: vba excel-vba excel

Sub insertHeader()

    Dim rng1 As Range
    Dim ws As Worksheet
    Dim c1 As Range
    Dim rng2 As Range

    Set ws = Worksheets("Sheet1")
    Set rng1 = ws.Range("A2:AV2")

    For Each c1 In rng1
        If c1.Value <> "" Then
            c1.Value = c1.Offset(-1) & "_" & c1.Value
        End If
    Next

End Sub

我正在尝试将标题与子标题组合起来

见下图pix-

提前致谢!! FIRST

Result

2 个答案:

答案 0 :(得分:0)

Sub insertHeader()

Dim rng1 As Range
Dim ws As Worksheet
Dim c1 As Range


Set ws = Worksheets("Sheet1")
Set rng1 = ws.Range("A1:AV1")

For Each c1 In rng1
    If c1.Value <> "" Then
        c1.Offset(1, 0) = c1.Value & "_" & c1.Offset(1, 0)
        c1.Offset(1, 1) = c1.Value & "_" & c1.Offset(1, 1)
        c1.Offset(1, 2) = c1.Value & "_" & c1.Offset(1, 2)
        c1.Offset(1, 3) = c1.Value & "_" & c1.Offset(1, 3)
        c1.Offset(1, 4) = c1.Value & "_" & c1.Offset(1, 4)

    End If
Next


End Sub

答案 1 :(得分:0)

Sub insertHeader()

    Dim rng1 As Range
    Dim ws As Worksheet
    Dim c1 As Range
    Dim rng2 As Range
    Dim currHeader

    Set ws = Worksheets("Sheet1")
    Set rng1 = ws.Range("A2:AV2")
    currHeader = ""     

    For Each c1 In rng1
        If Len(c1.Offset(-1).Value)>0 Then
            currHeader = c1.Offset(-1).Value     
        End If
        If c1.Value <> "" & currHeader <> "" Then
            c1.Value = currHeader  & "_" & c1.Value
        End If
    Next

End Sub