从列到行(重复+关联时间)

时间:2018-03-12 12:36:56

标签: excel vba excel-vba excel-formula

我有一张Excel表格,如下所示:

Booking #,  Arrival Passed  Berthing Date   UnBerthing Date Departure Passed 
94260   13/05/2017 15:30    13/05/2017 16:00    31/05/2017 20:44    31/05/2017 20:58
94708   15/05/2017 16:56    15/05/2017 17:15    16/05/2017 00:00    16/05/2017 00:04
94709   20/05/2017 09:54    20/05/2017 10:26    20/05/2017 18:07    20/05/2017 18:17
94710   24/05/2017 16:09    24/05/2017 16:35    25/05/2017 01:03    25/05/2017 01:08
94711   29/05/2017 10:30    29/05/2017 10:45    29/05/2017 17:33    29/05/2017 17:38
94716   17/05/2017 18:10    17/05/2017 18:25    18/05/2017 01:08    18/05/2017 01:14
94717   22/05/2017 17:31    22/05/2017 17:50    23/05/2017 00:55    23/05/2017 01:03
94718   27/05/2017 10:52    27/05/2017 11:07    27/05/2017 18:54    27/05/2017 19:00
94719   31/05/2017 19:04    31/05/2017 19:18    01/06/2017 01:45    01/06/2017 01:52

我需要以下列格式:

Booking #   Event   Time
94260   Arrival Passed  13/05/2017 15:30
94260   Berthing Date   13/05/2017 16:00
94260   UnBerthing Date 31/05/2017 20:44
94260   Departure Passed    31/05/2017 20:58
94708   Arrival Passed  15/05/2017 16:56
94708   Berthing Date   15/05/2017 17:15
94708   UnBerthing Date 16/05/2017 00:00
94708   Departure Passed    16/05/2017 00:04

我非常感谢你的帮助。

谢谢。

注意:有628行,我需要对它们进行转换。

1 个答案:

答案 0 :(得分:0)

我发现您已经找到了一个数据透视表解决方案,但如果您或任何人正在寻找VBA解决方案,那么这将满足您的需求:

Sub SortData()

Dim i As Integer
Dim LRow
Dim DataRow
Dim NewRow

DataRow = 2 ' This is the row of the first set of data, Row 2 because we dont want to include the header row
NewRow = 2 ' This is the row that will change for each piece of information from the DataRow

' in this example we will put the data into the G,H,I colmuns of the same sheet, you will need to change the range references if you want to output the data elsewhere.

' Add the title of each new colomn
Range("G1").Value = "Booking #"
Range("H1").Value = "Event"
Range("I1").Value = "Time"

LRow = Cells(Rows.Count, 1).End(xlUp).Row - 1 ' Find the last Row and take away 1 as we do not need to inculde the header row, remove -1 if you have no header row
For i = 1 To LRow ' repeat code until last row

Range("G" & NewRow).Value = Range("A" & DataRow).Value
Range("H" & NewRow).Value = Range("B1").Value ' Title 'Arrival Passed', this could be hardcoded in.
Range("I" & NewRow).Value = Range("B" & DataRow).Value
NewRow = NewRow + 1 ' increase the NewRow value by 1 ready for the next part of the DataRow

Range("G" & NewRow).Value = Range("A" & DataRow).Value
Range("H" & NewRow).Value = Range("C1").Value ' Title 'Berthing Date', this could be hardcoded in.
Range("I" & NewRow).Value = Range("C" & DataRow).Value
NewRow = NewRow + 1 ' increase the NewRow value by 1 ready for the next part of the DataRow

Range("G" & NewRow).Value = Range("A" & DataRow).Value
Range("H" & NewRow).Value = Range("D1").Value ' Title 'UnBerthing Date', this could be hardcoded in.
Range("I" & NewRow).Value = Range("D" & DataRow).Value
NewRow = NewRow + 1 ' increase the NewRow value by 1 ready for the next part of the DataRow

Range("G" & NewRow).Value = Range("A" & DataRow).Value
Range("H" & NewRow).Value = Range("E1").Value ' Title 'Depature Passed', this could be hardcoded in.
Range("I" & NewRow).Value = Range("E" & DataRow).Value
NewRow = NewRow + 1 ' increase the NewRow value by 1 ready for the next part of the DataRow

DataRow = DataRow + 1 ' Now move onto the next data row.
Next i


End Sub