VBA将列值设置为表标题

时间:2017-03-21 17:15:39

标签: excel vba import format

我有另一个问题,绝对不知道解决它......

我有一个这样的文本文件:

id:3
name:S3_SVC_HE21_SVC04
status:online
IO_group_id:1
IO_group_name:io_grp1
hardware:CG8
actual_different:no
actual_valid:yes
memory_configured:48
memory_actual:48
memory_valid:yes
cpu_count:2
cpu_socket:1
cpu_configured:6 core Intel(R) Xeon(R) CPU E5645 @ 2.40GHz
cpu_actual:6 core Intel(R) Xeon(R) CPU E5645 @ 2.40GHz
cpu_valid:yes
cpu_socket:2
cpu_configured:6 core Intel(R) Xeon(R) CPU E5645 @ 2.40GHz
cpu_actual:6 core Intel(R) Xeon(R) CPU E5645 @ 2.40GHz
cpu_valid:yes
adapter_count:3
adapter_location:1
adapter_configured:Four port 8Gb/s FC adapter
adapter_actual:Four port 8Gb/s FC adapter
adapter_valid:yes
adapter_location:0
adapter_configured:Two port 1Gb/s Ethernet adapter
adapter_actual:Two port 1Gb/s Ethernet adapter
adapter_valid:yes
adapter_location:2
adapter_configured:Four port 8Gb/s FC adapter
adapter_actual:Four port 8Gb/s FC adapter
adapter_valid:yes
ports_different:no

id:4
name:S1_SVC_HE27_SVC02
status:online
IO_group_id:1
IO_group_name:io_grp1
hardware:CG8
actual_different:no
actual_valid:yes
memory_configured:48
memory_actual:48
memory_valid:yes
cpu_count:2
cpu_socket:1
cpu_configured:6 core Intel(R) Xeon(R) CPU E5645 @ 2.40GHz
cpu_actual:6 core Intel(R) Xeon(R) CPU E5645 @ 2.40GHz
cpu_valid:yes
cpu_socket:2
cpu_configured:6 core Intel(R) Xeon(R) CPU E5645 @ 2.40GHz
cpu_actual:6 core Intel(R) Xeon(R) CPU E5645 @ 2.40GHz
cpu_valid:yes
adapter_count:3
adapter_location:1
adapter_configured:Four port 8Gb/s FC adapter
adapter_actual:Four port 8Gb/s FC adapter
adapter_valid:yes
adapter_location:0
adapter_configured:Two port 1Gb/s Ethernet adapter
adapter_actual:Two port 1Gb/s Ethernet adapter
adapter_valid:yes
adapter_location:2
adapter_configured:Four port 8Gb/s FC adapter
adapter_actual:Four port 8Gb/s FC adapter
adapter_valid:yes
ports_different:no

我想将其格式化为:

enter image description here

用语言: 文本文件将导入:

Workbooks.OpenText Filename:=lsnodehwf, DataType:=xlDelimited, _
        TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
        Semicolon:=True, Comma:=False, Space:=False, Other:=True, OtherChar:=":"

列A将是此表的标题。 必须插入B列......

有没有人有想法?

我不知道如何开始...... 也许我需要一个来自linux shell的更好的输出文件..但是没有这些技能......

2 个答案:

答案 0 :(得分:0)

以下是执行所需操作的基本方法:

Sub Separate_Headers_from_Data()
    r = ActiveCell.Row
    c = ActiveCell.Column

    Dim d As String
    d = ""

    Do Until Cells(r, c) = ""
      If d = "" Then d = Cells(r, c) Else d = d & "|" & Cells(r, c)
      r = r + 1
    Loop

    Sheets.Add
    d_s = Split(d, "|")

    For X = 0 To UBound(d_s)
    Cells(1, X + 1) = Split(d_s(X), ":")(0)
    Cells(2, X + 1) = Split(d_s(X), ":")(1)
    Next X
    MsgBox "Done..."
End Sub

答案 1 :(得分:0)

我明白了 - 不是那么漂亮。

但它有效:)

Sub transp()


Dim lngRow As Long
Dim lngLastRowOfSection As Long
Dim rngFind As Range

lngRow = 1

rngname = 3
temp = 1
Do Until Cells(lngRow + 1, 1).Value = ""

    lngLastRowOfSection = Cells(lngRow, 1).End(xlDown).Row

    Set slcFind = Range(Cells(lngRow, 1), Cells(lngLastRowOfSection, 1))
    slcFind.Copy
    Set targetRange = Worksheets("Tabelle2").Cells(6, 1)

     targetRange.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=True


    lngRow = Cells(lngLastRowOfSection, 1).End(xlDown).Row
    If lngRow >= Rows.count Then Exit Do

Loop

lngRow = 1
rngname = 3
i = 7
Do Until Cells(lngRow + 1, 1).Value = ""

    lngLastRowOfSection = Cells(lngRow, 1).End(xlDown).Row

    Set slcFind = Range(Cells(lngRow, 2), Cells(lngLastRowOfSection, 2))
    slcFind.Copy
    Set targetRange = Worksheets("Tabelle2").Cells(i, 1)

     targetRange.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=True


    lngRow = Cells(lngLastRowOfSection, 1).End(xlDown).Row
    If lngRow >= Rows.count Then Exit Do
i = i + 1
Loop
End Sub

谢谢你们的帮助:)

祝你好运, 弗洛里安