将文本隐藏到列并使用列名插入标题

时间:2018-06-02 17:24:07

标签: excel excel-vba excel-formula vba

下面是我可以将文本转换为列的代码。

我在寻找什么?我想用Tno,Host,Data,String,ID,Jno添加标题(向下移动第一行并添加标题)。你能帮忙解决这个问题吗?

Sub test()
Dim ws As Worksheet

Application.ScreenUpdating = False
For Each ws In ThisWorkbook.Worksheets
Select Case UCase(ws.Name)
    Case "MASTER", "DATA"
        'do nothing
    Case Else
        ws.Columns(1).TextToColumns Destination:=ws.Range("A1"), DataType:=xlDelimited, _
            TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, _
            Tab:=False, _
            Semicolon:=False, _
            Comma:=True, _
            Space:=False, _
            Other:=False, OtherChar:="|", _
            FieldInfo:=Array(Array(1, 1), Array(2, 1), Array(3, 1), Array(4, 1), Array(5, 1), Array(6, 1), Array(7, 1), Array(8, 1), Array(9, 1), Array(10, 1)), _
            TrailingMinusNumbers:=True
End Select
Next ws
Application.ScreenUpdating = True
End Sub

1 个答案:

答案 0 :(得分:0)

for each循环后添加以下代码:

Sub test()
Dim ws As Worksheet

Application.ScreenUpdating = False
For Each ws In ThisWorkbook.Worksheets
Select Case UCase(ws.Name)
    Case "MASTER", "DATA"
        'do nothing
    Case Else
        ws.Columns(1).TextToColumns Destination:=ws.Range("A1"), DataType:=xlDelimited, _
            TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, _
            Tab:=False, _
            Semicolon:=False, _
            Comma:=True, _
            Space:=False, _
            Other:=False, OtherChar:="|", _
            FieldInfo:=Array(Array(1, 1), Array(2, 1), Array(3, 1), Array(4, 1), Array(5, 1), Array(6, 1), Array(7, 1), Array(8, 1), Array(9, 1), Array(10, 1)), _
            TrailingMinusNumbers:=True
End Select
Next ws

    'insert header
    Range("A1").EntireRow.Insert
    Range("A1").Value = "Tno"
    Range("B1").Value = "Host"
    Range("C1").Value = "Data"
    Range("D1").Value = "String"
    Range("E1").Value = "ID"
    Range("F1").Value = "Jno"

Application.ScreenUpdating = True
End Sub