代码之家  ›  专栏  ›  技术社区  ›  RemoteMan213

Excel根据标题行将单列拆分为多列

  •  0
  • RemoteMan213  · 技术社区  · 8 年前

    我有一个excel文件,其中包含从LTSpice仿真导出的数据。有280次不同的运行,但数据导出为两列(时间和电压),在新运行开始时有一个运行单元。每次运行中的数据点数量不同。看起来像这样:

    Run 1/280
    Time1        Voltage1
    Time2        Voltage2
    Run 2/280
    Time1        Voltage1
    Time2        Voltage2
    Time3        Voltage3
    Run 3/280
    

    我希望将运行单元格作为行,并在其下方显示时间和电压列。

    Run 1/280                Run 2/280                Run 3/280
    Time1        Voltage1    Time1        Voltage1
    Time2        Voltage2    Time2        Voltage2
                             Time3        Voltage3
    

    我还没有找到一个简单的方法来做到这一点,所以任何帮助将不胜感激。

    2 回复  |  直到 8 年前
        1
  •  0
  •   DMM    8 年前

    没有VBA。。。

    对于输入列表的每一行,您需要标识其类型( Run x/xxx 标题或 terminal, voltage

    在下图中,列 A B 执行此任务。柱 A. B

    输出的标题行利用了以下事实:如果 array MATCH(x,array,0) 第一 中的元素 大堆 等于x。重复使用 SUMPRODUCT A. 到当前输出行和列对编号,然后 SUMPRODUCT公司 提供0,不幸的是 INDEX(array,SUMPRODUCT()) 术语计算结果为 INDEX(array,0) 它提供了 (*)-这不是我们想要的。

    enter image description here

    row 1 column E 输出区域中工作表的最大值-列的最大值 ,分别确定要求。输出尺寸过大(如图所示)不是问题,因为任何冗余位置的公式的计算结果仅为 "" .

    (*)实际上,对于单个列 大堆 =INDEX(array,0) 评估结果为 大堆 大堆 .

        2
  •  0
  •   Variatus    8 年前

    Sub SplitToColumns()
        ' 16 Sep 2017
    
        Dim WsS As Worksheet                    ' S = "Source"
        Dim WsD As Worksheet                    ' D = "Destination"
        Dim WsDName As String
        Dim RunId As String                     ' first word in "Run 1/280"
        Dim RowId As Variant                    ' value in WsS.Column(A)
        Dim Rl As Long                          ' last row (WsS)
        Dim Rs As Long, Rd As Long              ' row numbers
        Dim Cd As Long                          ' column (WsD)
    
        WsDName = "RemoteMan"                   ' change to a valid tab name
        Application.ScreenUpdating = False
        On Error Resume Next
        Set WsD = Worksheets(WsDName)
        If Err Then
            ' create WsD if it doesn't exist:
            Set WsD = Worksheets.Add(After:=Worksheets(Worksheets.Count))
            WsD.Name = WsDName
            Cd = -1
        Else
            ' continue adding new data to the right of existing,
            With WsD.UsedRange
                Cd = .Columns.Count - 1
                If Cd = 1 And .Rows.Count = 1 Then Cd = -1
            End With
        End If
    
        Set WsS = Worksheets("Remote")          ' change to a valid tab name
        With WsS
            ' presume "Run" & Time in column A, Voltage in Column B
            ' presume: no blank rows
            Rl = .Cells(Rows.Count, "A").End(xlUp).Row
            RunId = .Cells(2, 1).Value          ' row 2 must have the RunId
            RunId = Left(RunId, InStr(RunId, " ") - 1)
            For Rs = 2 To Rl                    ' assume data start in row 2 (A1 may not be blank!)
                RowId = .Cells(Rs, "A").Value
                If InStr(1, RowId, RunId, vbTextCompare) = 1 Then
                    Rd = 1                      ' first row to use in WsD
                    Cd = Cd + 2                 ' determine next columns
                End If
                WsD.Cells(Rd, Cd).Value = RowId
                WsD.Cells(Rd, Cd + 1).Value = .Cells(Rs, "B").Value
                Rd = Rd + 1                     ' next row to use
            Next Rs
        End With
        Application.ScreenUpdating = True
    End Sub