返回列表 上一主題 發帖

[發問] 請問要如何切割數據以方便做圖?

本帖最後由 stillfish00 於 2013-12-20 22:01 編輯

回復 1# Genie
  1. Sub test()
  2.     Dim ar, d, arEQ, i As Long
  3.    
  4.     '複製來源
  5.     With Sheets("Sheet1")
  6.         .AutoFilterMode = False
  7.         .[A1].CurrentRegion.Copy Sheets("Sheet2").[A1]
  8.         .[A1].AutoFilter
  9.     End With
  10.    
  11.     With Sheets("Sheet2")
  12.         '排序
  13.         With .Sort
  14.             .SortFields.Clear
  15.             .SortFields.Add .Parent.Range("B2:B" & Rows.Count), xlSortOnValues, xlAscending
  16.             .SortFields.Add .Parent.Range("C2:C" & Rows.Count), xlSortOnValues, xlAscending
  17.             .SetRange .Parent.[A1].CurrentRegion
  18.             .Header = xlYes
  19.             .Orientation = xlTopToBottom
  20.             .SortMethod = xlPinYin
  21.             .Apply
  22.         End With
  23.         
  24.         '找出每段起始位置
  25.         ar = .[A1].CurrentRegion.Value
  26.         Set d = CreateObject("scripting.dictionary")
  27.         For i = 2 To UBound(ar)
  28.             If ar(i, 2) <> ar(i - 1, 2) Then d(ar(i, 2)) = i
  29.         Next i
  30.         
  31.         '由後往前分割,插入空白
  32.         arEQ = d.keys
  33.         For i = UBound(arEQ) To LBound(arEQ) + 1 Step -1
  34.             .Range(.Cells(d(arEQ(i)), "D"), .Cells(.Rows.Count, "D")).Insert xlToRight
  35.             .Rows(d(arEQ(i))).Insert xlDown
  36.         Next i
  37.         .[D1].Resize(, d.Count).Value = d.keys
  38.     End With
  39. End Sub
複製代碼

TOP

回復 3# Genie
可能你的版本比較舊,語法不支援
把排序那段換成
  1.         '排序
  2.         .[A1].CurrentRegion.Sort key1:=.[B1], order1:=xlAscending, key2:=.[C1], order2:=xlAscending, Header:=xlYes, Orientation:=xlTopToBottom
複製代碼
試看看

TOP

回復 5# Genie
可能吧...我沒辦法測試舊版本
寫法很多種,自己試試吧
ar = Range(.[A1],.[A1].end(xldown)).resize(,4).Value

TOP

        靜思自在 : 好事要提得起,是非要放得下,成就別人即是成就自己。
返回列表 上一主題