返回列表 上一主題 發帖

[發問] 抓取篩選後儲存格內容至對應的工作表

回復 1# Michelle-W
試試看!
  1. Sub Ex()
  2.     Dim lg As Variant, ctn As Variant, xi As Integer
  3.     Dim dic As Object, sp As Variant, sh As Worksheet
  4.    
  5.     Set sh = Worksheets("05月")
  6.     Set dic = CreateObject("scripting.dictionary")
  7.    
  8.     With sh
  9.         For Each lg In .Range("B1:I1")
  10.             .Select
  11.             
  12.             dic(lg.Value) = ""
  13.             For Each ctn In .Range("A2:A7")
  14.                 If ctn.Offset(, lg.Column - 1) = "V" Then
  15.                     dic(lg.Value) = dic(lg.Value) + IIf(dic(lg.Value) = "", "", ",") + ctn.Value
  16.                 End If
  17.             Next
  18.             sp = Split(dic(lg.Value), ",")
  19.             '  Cells(15, lg.Column).Resize(UBound(sp) + 1) = Application.Transpose(Array(sp))    '  展示用
  20.             
  21.             For xi = 1 To Worksheets.Count
  22.                 If Worksheets(xi).Name = lg.Value Then Worksheets(xi).Select: Exit For
  23.             Next xi
  24.             If xi > Worksheets.Count Then
  25.                  Sheets.Add After:=Sheets(Worksheets.Count)
  26.                  ActiveSheet.Name = lg.Value
  27.             End If
  28.             With Worksheets(lg.Value)
  29.                 .[A1] = sh.[A1]
  30.                 .[B1] = lg.Value
  31.                 .[A2].Resize(UBound(sp) + 1) = Application.Transpose(Array(sp))
  32.                 .[B2].Resize(UBound(sp) + 1) = "V"
  33.             End With
  34.         Next
  35.     End With
  36. End Sub
複製代碼

TOP

回復 3# Michelle-W

資料.rar (1.1 MB)

TOP

回復 8# Michelle-W
請參考!





各自分類資料.rar (33.09 KB)

TOP

回復 8# Michelle-W
  1. Sub 各自分類()
  2.     Dim lg As Variant, ctn As Variant, xi As Boolean
  3.     Dim dic As Object, sp As Variant
  4.     Dim sh As Worksheet, wks As Worksheet
  5.    
  6.     Set sh = Worksheets("選單")
  7.     Set dic = CreateObject("scripting.dictionary")
  8.    
  9.     With sh
  10.         For Each lg In .Range("C1", .Range("C1").End(xlToRight))
  11.             .Select
  12.             
  13.             dic(lg.Value) = ""
  14.             For Each ctn In .Range("A2", Range("A2").End(xlDown))
  15.                 If ctn.Offset(, lg.Column - 1) = "V" Then
  16.                     dic(lg.Value) = dic(lg.Value) + IIf(dic(lg.Value) = "", "", ",") + ctn.Value
  17.                 End If
  18.             Next
  19.             If dic(lg.Value) <> "" Then
  20.                sp = Split(dic(lg.Value), ",")
  21.                 Cells(15, lg.Column).Resize(UBound(sp) + 1) = Application.Transpose(Array(sp))    '  展示用
  22.                
  23.                 xi = tblExist(lg.Value)      '  判斷表單是否存在
  24.                 If xi = False Then
  25.                     Sheets.Add After:=Sheets(Worksheets.Count)
  26.                     ActiveSheet.Name = lg.Value
  27.                 End If
  28.                
  29.                 With Worksheets(lg.Value)
  30.                     .Cells.Clear
  31.                     .[A1] = sh.[A1]
  32.                     .[B1] = lg.Value
  33.                     .[A2].Resize(UBound(sp) + 1) = Application.Transpose(Array(sp))
  34.                     .[B2].Resize(UBound(sp) + 1) = "V"
  35.                 End With
  36.             End If
  37.         Next
  38.     End With
  39. End Sub

  40. Sub 新增選單()
  41.     Dim rng As Range, rng2 As Range
  42.    
  43.     刪除工作表
  44.     With Worksheets("選單")
  45.         .Cells.Clear
  46.         Set rng = Sheets("總表").Range("A2", Sheets("總表").[A2].End(xlDown))
  47.         .[C1].Resize(, rng.Count - 1) = Application.Transpose(rng)
  48.         Set rng = Sheets("總表").Range("A16", Sheets("總表").[B16].End(xlDown))
  49.         rng.Copy .[A1]
  50.     End With
  51. End Sub

  52. Function tblExist(tblName As String) As Boolean
  53.     Dim xi As Integer
  54.    
  55.     tblExist = False
  56.     For xi = 1 To Worksheets.Count
  57.         If Worksheets(xi).Name = tblName Then tblExist = True: Exit Function
  58.     Next xi
  59. End Function

  60. Sub 刪除工作表()
  61.     Dim xi As Integer
  62.    
  63.     Application.DisplayAlerts = False
  64.     For xi = Worksheets.Count To 2 Step -1
  65.         If Worksheets(xi).Name <> "總表" And Worksheets(xi).Name <> "選單" Then
  66.             Worksheets(xi).Delete
  67.         End If
  68.     Next xi
  69.     Application.DisplayAlerts = True
  70. End Sub

  71. Sub 刪除各分頁()
  72.     刪除工作表
  73.     With Worksheets("選單")
  74.         .Range("C2:" & Chr(64 + .[C1].End(xlToRight).Column) & 65535).Clear
  75.     End With
  76. End Sub
複製代碼

TOP

回復 8# Michelle-W
我有點好奇你想像之整體作業的流程實際是如何進行的?
如果同時有5月與6月的資料要新增進去專屬工作表內,
又是怎說?

TOP

回復 12# Michelle-W

TOP

本帖最後由 c_c_lai 於 2016-7-19 13:12 編輯

回復 14# Michelle-W
是不是這樣?
各自分類資料.rar (30.73 KB)

TOP

        靜思自在 : 得理要饒人,理直要氣和。
返回列表 上一主題