2015-06-15 69 views
0

我有一個稀疏的n列電子表格,其中前兩列描述了一個人,其餘(n-2)列記錄了各種事件(每個列都佔用一列)的RSVP和出勤數據。它看起來像這樣:如何將此CSV /表格數據轉換爲不同形狀?

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 

(我知道這是不正常的形式包括的姓名......他們不會在最後的數據負載...但他們在進行數據加載之前,對於人類校對是很好的。)

我該如何使用Excel或使用Windows PC用戶輕鬆訪問的任何其他工具來執行此操作? (我已安裝Excel,Microsoft LogParser,Microsoft Access和PortablePython已安裝。)

謝謝!

-K

+0

您需要創建一個文件才能進入數據庫,而不是將信息獲取到excel(數據透視表)? – Raystafarian

+0

正確 - 首要示例是電子表格WAS的單個選項卡充當聯繫人管理數據庫和事件出勤管理數據庫時數據的存儲方式。底部示例是數據需要在具有3個表格的正確關係數據庫的EventAttendanceByContacts表中進入其新家的方式:Contacts,Events,&EventAttendanceByContacts。 –

+0

您的數據是針對不同單元格中的事件還是使用逗號的單元格? – Raystafarian

回答

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是你可能需要避免使用兩片。此外,可能有一個更優雅的方式來做到這一點與數組。

+0

你很精彩 - 謝謝! –