2016-04-26 41 views
0

我有原始數據,我想按日期進行梳理,該數據是這種形式按日期排序數據:與Excel

month:april-2014 
offer | value 
ofr x | 2132 
ofr y | 135 
. 
. 
. 
month:mai-2014 
offer | value 
ofr x | 5115 
ofr z | 513 
ofr y | 651 

,並如此下去,還有每個月都有apear優惠和其他消失的人。

我希望它看起來像這樣:

offer | april-2014 |mai 14 | june .... 
ofr x  123   5  6 
ofr y  5   1  6 
ofr z 
ofr a 

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

+0

您是否正在尋找結果,例如新列表,新工作表,同一工作表中的其他地方,刪除舊列表並用新的替換? –

+0

是你的源數據中的任何空行嗎?還是直接從最後一個報價到下一行的月份? –

回答

0

嘗試重構像這樣的數據和使用數據透視表?

Date  | offer | value 
may-2014 |ofr x | 5115 
may-2014 |ofr z | 513 
may-2014 |ofr y | 651 
0

這第一部分代碼正在爲您進行重新整理。其他重要的事情是隻發送one column from your selected range to the function。一些重要的事情要記住的是,如果關鍵詞「月」不在文本中的同一位置,則可能需要編寫搜索條件,但提供的單詞本身並不在後面的行中沒有空格。另一點需要注意的是,這是對待一切。這意味着如果源單元格是文本,則目標單元格將是文本。將日期從文本轉換爲Excel的序列,這是一個單獨的問題,並且有很多方法可以實現。

顯式的選項 子SortOffer(OfferList作爲範圍)

Dim CounterX As Long, CounterY As Long, jCounter As Long, icounter As Long, MonthCount As Long, UniqueOffers As Long 
Dim inlist As Boolean 
Dim unsorted() As Variant 
Dim sorted() As Variant 

MonthCount = WorksheetFunction.CountIf(OfferList, "month*") 
UniqueOffers = CountUnique(OfferList.Columns(1).Cells) - MonthCount - 1 


ReDim sorted(1 To UniqueOffers + 1, 1 To MonthCount + 1) As Variant 

unsorted = OfferList 

CounterX = 1 
jCounter = 1 
sorted(1, 1) = "offer" 


For CounterY = LBound(unsorted, 1) To UBound(unsorted, 1) 

    If Left(unsorted(CounterY, 1), 5) = "month" Then 
     CounterX = CounterX + 1 
     sorted(1, CounterX) = Right(unsorted(CounterY, 1), Len(unsorted(CounterY, 1)) - 6) 
    Else 
     inlist = False 
     For icounter = 2 To jCounter 
      If unsorted(CounterY, 1) = sorted(icounter, 1) Then 
       sorted(icounter, CounterX) = unsorted(CounterY, 2) 
       inlist = True 
      End If 
     Next icounter 

     If Not inlist And unsorted(CounterY, 1) <> "offer" And unsorted(CounterY, 1) <> "" Then 
      jCounter = jCounter + 1 
      sorted(jCounter, 1) = unsorted(CounterY, 1) 
      sorted(jCounter, CounterX) = unsorted(CounterY, 2) 
     End If 
    End If 
Next CounterY 

Range("F1").Resize(UBound(sorted, 1), UBound(sorted, 2)).Value = sorted 

End Sub 

接下來的這個函數計算唯一條目的數量範圍和不計空格。我偶然發現了this code on this web page。如果您從此計數中減去月數,您將知道您的表中有多少個優惠。這是重要的,因爲它會告訴你how to size your arrayalt link),您將在以後寫回爲你的結果

Function CountUnique(ByVal MyRange As Range) As Integer 
    Dim Cell As Range 
    Dim J As Integer 
    Dim iNumCells As Integer 
    Dim iUVals As Integer 
    Dim sUCells() As String 

    iNumCells = MyRange.Count 
    ReDim sUCells(iNumCells) As String 

    iUVals = 0 
    For Each Cell In MyRange 
     If Cell.Text > "" Then 
      For J = 1 To iUVals 
       If sUCells(J) = Cell.Text Then 
        Exit For 
       End If 
      Next J 
      If J > iUVals Then 
       iUVals = iUVals + 1 
       sUCells(iUVals) = Cell.Text 
      End If 
     End If 
    Next Cell 
    CountUnique = iUVals 
End Function 

現在,公正的情況下鏈接不掩蓋它,這個答案這是一個學習的教訓對我來說是@JNevill,@Ralph,@findwindow,@ Gary'sStudent和@ScottCraner向我講授了各種各樣的部分。如果我錯過了某人,我會感激你我也確信這些人中的任何一個人都可以做到這一點,並且花費不到10小時的時間寫出來8)。