excel数据分割/分配

时间:2012-04-13 09:11:56

标签: excel excel-vba vba

我在excel中获得了一个输入文件,它只是向下列“M2”。输入文件如下所示:

ru_utime     0.060              
ru_stime     0.140              
ru_maxrss    0                          
ru_ixrss     0                          
ru_ismrss    0                          
ru_idrss     0                          
ru_isrss     0                          
ru_minflt    4124                       
ru_majflt    0                          
ru_nswap     0                          
ru_inblock   0                          
ru_oublock   0                          
ru_msgsnd    0                          
ru_msgrcv    0                          
ru_nsignals  0                          
ru_nvcsw     47174                      
ru_nivcsw    4347                       
==================================================

然后它以相同的格式重复大约1000次

你可以看到它在同一列中有两位信息,然后下一串由===分隔 我想要做的是忽略每个位的标题并获取右边的信息并将它们移动到彼此下面的不同列。

我想要做的就是读取文件并将所有信息放在ru_utime: 0.060 下的不同列下

所以所有的数据都在“M2”下来,我想拿相应的标题信息并将它们移动到类似S2,T2,U2的每一个然后当它命中时=======为它做接下来就是它。如果这对任何人都有意义,我会非常感谢你的帮助。

目的是通过点击按钮自动完成移动,谢谢

加入:

Sub incorperate()
sn = Split(Join(Application.Transpose(Sheets("sheet2").Cells(1).CurrentRegion.Columns(1)), "|"), String(62, "=") & "|")
With Sheets("sheet1").Cells(1).CurrentRegion
st = .Rows(1).Offset(.Rows.Count).Resize(UBound(sn) + 1)
End With

For j = 0 To UBound(sn)
sq = Split(sn(j), "|")
For jj = 0 To UBound(sq) - 1
  st(j + 1, jj + 1) = Split(sq(jj))(UBound(Split(Trim(sq(jj)))))
Next
Next

Sheets("sheet1").Cells(Rows.Count, 1).End(xlUp).Offset(1).Resize(UBound(st), UBound(st, 2)) = st
End Sub

1 个答案:

答案 0 :(得分:1)

Sub move()
Dim x, y(), i&, j&, k&, s

x = Range("S1", Cells(1, Columns.Count).End(xlToLeft)).Value
With CreateObject("Scripting.Dictionary")
    .CompareMode = 1
    For i = 1 To UBound(x, 2)
        .Item(x(1, i)) = i
    Next i

    x = Application.Trim(Range("M2", Cells(Rows.Count, "M").End(xlUp)).Value)
    ReDim y(1 To UBound(x), 1 To .Count): j = 1

    For i = 1 To UBound(x)
        If InStr(x(i, 1), "==") = 0 Then
            s = Split(x(i, 1))
            If .Exists(s(0)) Then
                k = .Item(s(0)): y(j, k) = s(UBound(s))
            End If
        Else
            j = j + 1
        End If
    Next i
End With

[s2].Resize(j, UBound(y, 2)).Value = y()
End Sub