如何将此CSV /表格数据转换为不同的形状?

时间:2015-06-15 17:54:01

标签: excel csv

我有一个稀疏的n列电子表格,其中前两列描述了一个人,其余的(n-2)列跟踪各种事件的RSVP和出勤数据(每个列占用一列)。它看起来像这样:

    PersonID, Name, Event29108294, Event01838401, Event10128384
    12345, John Smith, Registered - NoShow, Registered and Attended, RSVPed Maybe and did not attend
    982348, Mary Levitsky, Registered - NoShow, RSVPed No, 
    1873218, Alexa Maris, , Registered and Attended, 
    8294, Katlyn Johnson, , , Registered and Attended

我需要将其加载到一个数据库中,该数据库将出勤信息存储为“人和事件的每个人口交汇点的1行”。因此,我需要将数据转换为如下所示:

    EventID, PersonID, PersonName, Status
    Event29108294, 12345, John Smith, Registered - NoShow
    Event29108294, 982348, Mary Levitsky, Registered - NoShow
    Event01838401, 12345, John Smith, Registered and Attended
    Event01838401, 982348, Mary Levitsky, RSVPed No
    Event01838401, 1873218, Alexa Maris, Registered and Attended
    Event10128384, 12345, John Smith, RSVPed Maybe and did not attend
    Event10128384, 8294, Katlyn Johnson, Registered and Attended

(我知道包含名称不是正常形式......它们不会在最终的数据加载中......但是在进行数据加载之前它们对于人类校对很有用。)< / em>的

如何使用Excel或Windows PC用户可以轻松访问的任何其他工具执行此操作? (我已经安装了Excel,Microsoft LogParser,Microsoft Access和PortablePython。)

谢谢!

-K

1 个答案:

答案 0 :(得分:1)

假设您的事件位于不同的列中,这样的事情就可以了 -

Sub test()
Dim wsOrig As Worksheet
Set wsOrig = ActiveSheet

Dim wsDest As Worksheet
Set wsDest = Sheets("Sheet2")

Dim lcol As Integer
lcol = Cells(1, Columns.Count).End(xlToLeft).Column

Dim lrow As Integer
lrow = Cells(Rows.Count, "A").End(xlUp).row

Dim x As Integer
x = 2

wsDest.Cells(1, 1) = "EventID"
wsDest.Cells(1, 2) = wsOrig.Cells(1, 1)
wsDest.Cells(1, 3) = wsOrig.Cells(1, 2)
wsDest.Cells(1, 4) = "Status"


For i = 2 To lrow
    For j = 3 To lcol
        If Cells(i, j) <> "" Then
            wsDest.Cells(x, 1) = wsOrig.Cells(1, j)
            wsDest.Cells(x, 2) = wsOrig.Cells(i, 1)
            wsDest.Cells(x, 3) = wsOrig.Cells(i, 2)
            wsDest.Cells(x, 4) = wsOrig.Cells(i, j)
            x = x + 1
        End If
    Next
 Next


End Sub

如果您使用的是.csv,则可能需要避免使用两张纸。此外,使用数组可能有更优雅的方法。