2013-01-12 44 views
3

我看遍了關於這個問題的simular話題,但卡住了,我需要在2003年的Excel轉換與數據的列到列與多個行Excel 2003中

VBA專家的一些幫助,我有一排數據如下

(Project Code) ...(Milestone1) (Date1)  (Milestone2) (Date2) ... (Milestone 10)(Date3) 
     ColumnA ... ColumnAG  ColumnAH ColumnAI  ColumnAJ ... ColumnAY ColumnAZ 
     Header1  Header33  Header34 Header35  Header36 ... Header51 Header52 
rowkey datavalue1  datavalue33 datavalue34 datavalue35 datavalue36 ... datavalue51 

我有一個非規範化的數據集,其中datavalues可以或可以不爲空在一行接一行的基礎。我需要對它們進行標準化。

 1234567  start 12/12/2012 build  12/01/2013 ... finish 01/02/2014   
     1234568  plan  14/12/2012 contract 02/02/2013 ... NULL NULL 

將成爲

 1234567  header33 start 12/12/2012 
     1234567  header34 build 12/01/2013 
     ... 
     1234567  header51 finish 01/02/2014 
     1234568  header33 plan  14/12/2012 
     1234568  header34 contract 02/02/2013 

我行約百,含10個里程碑和10個相關的日期名稱項目數據,以及需要拆分這些,所以我有多行列出每個項目代碼的里程碑標題/里程碑名稱/日期....如果里程碑名稱和數據是白色,則記錄可以跳過...

Th ERE是simular後Convert row with columns of data into column with multiple rows in Excel 2007,但不`噸知道如何改變這適用於上述場景...

任何幫助,將不勝感激 感謝

+0

如果您發現它有幫助,請在下面標記爲正確的答案。謝謝。 – Lopsided

回答

0

好吧,這個代碼還真是不太先進了,但有點乏味。我爲你設計了一個應該完成你所需要的工作。它充滿了你可以用來幫助學習代碼的評論(如果你想自己再做一次)。祝你好運!

Sub normalizeData() 

Dim i As Integer, j As Integer, k As Integer 
Dim r As Integer 
Dim bReport As Workbook, Report As Worksheet, Report2 As Worksheet 
Dim m(0 To 9) As Integer 'This sets up the variables for your milestones 
Dim mileString As String, dateString As String 

Set Report = Excel.ActiveSheet 
Set bReport = Report.Parent 
Set Report2 = bReport.Worksheets.Add 

'There are two ways to identify your milestones to the subroutine: either have some kind of identifier to search _ 
for within the value of the milestone cell; or define them explicitly. I'm going to do the latter since I don't know _ 
what might be in those cells. 

m(0) = 33 'The column number for each milestone 
m(1) = 35 
m(2) = 37 
m(3) = 39 
m(4) = 41 
m(5) = 43 
m(6) = 45 
m(7) = 47 
m(8) = 49 
m(9) = 51 


r = Report.UsedRange.Rows.Count 'Get the last row number and assign it to a _ 
           short variable (just to make things easier). 

Report2.Cells(1, 1).Value = "Normalized Data" 'Placing a header row allows us to use used range without having _ 
               to worry about identifying the first iteration. If you've ever tried this _ 
               by yourself you probably know what I'm referring to. 

With Report2.Range("A1:D1") 'Just makin it purdy 
    .Merge 
    .HorizontalAlignment = xlCenter 
    .Interior.Color = RGB(0, 20, 99) 
    .Font.Color = RGB(224, 238, 255) 
    .Font.Bold = True 
    .Font.Size = 14 
End With 


For i = 2 To r 
    For j = 0 To 9 
     If Report.Cells(i, m(j)).Value <> "" And _ 
     Report.Cells(i, m(j)).Value <> "Null" Then 'I'm not sure if the empty values will be null or blank strings. 
      mileString = Report.Cells(i, m(j)).Value 
      dateString = Report.Cells(i, m(j) + 1).Value 
      k = Report2.UsedRange.Rows.Count + 1 
      Report2.Cells(k, 1).Value = Report.Cells(i, 1).Value 
      Report2.Cells(k, 2).Value = Report.Cells(1, m(j)).Value 'Assuming your header is on the first row 
      Report2.Cells(k, 3).Value = mileString 
      Report2.Cells(k, 4).Value = dateString 
      Report2.Cells(k, 4).NumberFormat = "MM/dd/yyyy" 
     End If 
    Next j 
Next i 

For i = 2 To Report2.UsedRange.Rows.Count 
    If i Mod 2 = 0 Then 
     Report2.Range("A" & i & ":D" & i).Interior.Color = RGB(227, 235, 252) 'Alternating row color for easier reading. 
    End If 
Next i 

'************** This part adds borders. Omit this block if you don't want them.************ 
With Report2.Range("A1:D" & Report2.UsedRange.Rows.Count) 
    .Borders(xlEdgeTop).Weight = xlThin 
    .Borders(xlEdgeBottom).Weight = xlThin 
    .Borders(xlLeft).Weight = xlThin 
    .Borders(xlRight).Weight = xlThin 
    .Borders(xlInsideHorizontal).Weight = xlThin 
    .Borders(xlInsideVertical).Weight = xlThin 
End With 
'****************************************************************************************** 



End Sub 

PS:確保在運行宏時位於非規格化工作表上。它被設計成將該工作表引用爲活動工作表,並將標準化數據放入新的工作表中。

另請注意,我使用了一個數組來識別里程碑,但我相信您可以像使用For...Next循環一樣輕鬆地使用第2步增量來提高可伸縮性。例如,在這種情況下,for循環的開頭看起來像For j = 33 to 51 Step 2。假設你所有的里程碑都是兩列相隔的。