返回列表 上一主題 發帖

請問該如何使用排序並將需要資料帶出至sheet2

本帖最後由 GBKEE 於 2012-6-8 16:44 編輯

回復 1# yueh0720
  1. Option Explicit
  2. Sub Ex()
  3.     Dim Ar(), xl As Integer
  4.     With Sheets(1)                              '第1個工作表
  5.         .AutoFilterMode = False                 '取消 這工作表的自動篩選
  6.         Ar = .Range("A1").CurrentRegion.Value   '資料轉入陣列
  7.          .Range("A1").CurrentRegion.Sort Key1:=.Range("H2"), Order1:=xlAscending, Key2:=.Range( _
  8.             "A2"), Order2:=xlAscending, Header:=xlYes                     '排序
  9.          .Range("IV:IV") = ""                   '清除IV欄資料
  10.          .Columns("H:H").AdvancedFilter Action:=xlFilterCopy, CopyToRange:=.Range("IV1"), CriteriaRange:=.Range("IU1:IU2"), Unique:=True
  11.                                                 'AdvancedFilter 進階篩選: H欄不重複資料  篩選到.Range("IV1")
  12.         xl = 2                                  '從 第2列 開始
  13.         Do While .Range("IV" & xl) <> ""        '條件成立: 執行迴圈
  14.             If Sheets.Count < xl Then Sheets.Add , Sheets(Sheets.Count)  '工作表數小於xl:新增工作表
  15.             .Range("A1").AutoFilter Field:=8, Criteria1:=.Range("IV" & xl)      '自動篩選: 第8欄=.Range("IV" & xl)
  16.             .Range("A1").CurrentRegion.SpecialCells(xlCellTypeConstants).Copy Sheets(xl).[A1] '篩選到的資料:複製到 指定工作表的[A1]
  17.             xl = xl + 1                         '從第2列: 往下一列
  18.         Loop
  19.         .AutoFilterMode = False
  20.          .Range("A1").CurrentRegion.Value = Ar  '取出陣列資料 置回
  21.     End With
  22. End Sub
複製代碼

TOP

        靜思自在 : 地上種了菜,就不易長草;心中有善,就不易生惡。
返回列表 上一主題