iT邦幫忙

0

EXCEL VBA 資料庫統計數量並貼至矩形方式目錄表

  • 分享至 

  • xImage

https://ithelp.ithome.com.tw/upload/images/20220423/20120368vWllwFdeq2.jpg

工作表1為原始資料,工作表2為統計資料及(重複)數量並排序,工作表3為五大欄型號數量目錄表,能依工作表2導入並依不固定資料筆數排成矩形方式,例如81筆,第一欄17筆,其餘16筆。

原始資料如下,希望能產出工作表2及工作表3,請各位大師幫忙了!
128a
517B
130a
128a
131a
129a
133a
130a
131a
132a
132a
135a
133a
133a
135a

129a
511b
133a
517B
128a
235C
236C
237C
238C
239C
240C
241C
242C
243C
244C
245C
246C
247C
248C
249C
250C
251C
252C
253C
254C
255C
256C
257C
258C
259C
260C
261C
262C
263C
264C
265C
266C
267C
268C
269C
270C
271C
272C
273C
274C
275C
276C
277C
278C
279C
280C
281C
282C
283C
284C
285C
286C
287C
288C
289C
290C
291C
292C
293C
294C
295C
296C
297C
298C
299C
300C
301C
302C
303C
304C
305C
306C

blanksoul12 iT邦研究生 5 級 ‧ 2022-04-25 08:30:56 檢舉
"不固定資料筆數排成矩形方式" 是怎麼不規則? 有什麼規則嗎?
字典去重覆及計數量,再按"不固定"循環放值
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

1
blanksoul12
iT邦研究生 5 級 ‧ 2022-04-25 09:30:27
最佳解答

自己改一改資料放那吧

Sub test()

    Set d = CreateObject("Scripting.Dictionary")
    arr = Range("a1:g" & [a1048576].End(xlUp).Row)
    For i = 1 To UBound(arr)
        If WorksheetFunction.Trim(Cells(i, "a")) <> "" Then
            d(arr(i, 1)) = d(arr(i, 1)) + 1
        End If
    Next
    k = d.keys
    t = d.items
    
    [c1].Resize(1, 2) = Array("型號", "重覆次數")
    [c2].Resize(d.Count, 1) = Application.Transpose(k)
    [d2].Resize(d.Count, 1) = Application.Transpose(t)
    
    y = 1
    x = 4
    For i = 0 To UBound(k)
        If i = 0 Then
            x = x + 2
        ElseIf i Mod 5 = 0 Then
            y = y + 2
            x = 4
            x = x + 2
        Else
            x = x + 2
        End If
        Cells(y, x) = "型號"
        Cells(y, x + 1) = "重覆次數"
        Cells(y + 1, x) = k(i)
        Cells(y + 1, x + 1) = t(i)
    Next

End Sub

看更多先前的回應...收起先前的回應...
peterzxcv iT邦新手 5 級 ‧ 2022-04-25 10:52:04 檢舉

工作表2的原始資料標題也被拉入統計(arr = Range("a2:g" & [a1048576].End(xlUp).Row)有調整),工作表3排序方式5大欄( ElseIf i Mod 5 = 0 Then 有調整),排序是直排如圖,能再調整嗎,感謝
https://ithelp.ithome.com.tw/upload/images/20220425/20120368QsMtO1zet7.jpg
https://ithelp.ithome.com.tw/upload/images/20220425/20120368x8DS2hBhSE.jpg

淺水員 iT邦大師 6 級 ‧ 2022-04-25 11:47:16 檢舉

問答區主要是技術交流
這個回答已經把你需要的東西都寫上去了
最後的修改最好是自己來
單純請人寫的話建議走發包的流程

PS. 之前的問題會有人直接寫好,是因為問題的性質不同,並不是說這邊原本就會直接幫人寫好的喔

blanksoul12 iT邦研究生 5 級 ‧ 2022-04-25 12:17:18 檢舉

解決方法大約這樣吧,如沒任何 vba 基礎,我給什麼對你也沒有什麼好處

peterzxcv iT邦新手 5 級 ‧ 2022-04-25 13:20:22 檢舉

了解,我只是想主題能有完整語法呈現,作為技術範本,也謝謝兩位大師的建議

blanksoul12 iT邦研究生 5 級 ‧ 2022-04-26 08:19:08 檢舉

大家交流一下吧,我自己也不是猛,參考一下

Sub test()

    Set d = CreateObject("Scripting.Dictionary")
    arr = Range("a2:g" & [a1048576].End(xlUp).Row)
    For i = 1 To UBound(arr)
        If WorksheetFunction.Trim(Cells(i, "a")) <> "" Then
            d(arr(i, 1)) = d(arr(i, 1)) + 1
        End If
    Next
    k = d.keys
    t = d.items
    
    [c1].Resize(1, 2) = Array("型號", "重覆次數")
    [c2].Resize(d.Count, 1) = Application.Transpose(k)
    [d2].Resize(d.Count, 1) = Application.Transpose(t)
    
    row_count = WorksheetFunction.RoundUp((UBound(k) + 1) / 5, 0)
    remind_no = UBound(k) + 1
    
    y = 1
    x = 4
    j = 5
    i = 0
    Do
        If i = 0 Then
            x = x + 2
        ElseIf record_no Mod row_count = 0 Then
            y = 1
            x = x + 2
            j = j - 1
            remind_no = remind_no - record_no
            row_count = WorksheetFunction.RoundUp(remind_no / j, 0)
            record_no = 0
        Else
            y = y + 2
        End If
        Cells(y, x) = "型號"
        Cells(y, x + 1) = "重覆次數"
        Cells(y + 1, x) = k(i)
        Cells(y + 1, x + 1) = t(i)
        i = i + 1
        record_no = record_no + 1
    Loop Until i = UBound(k) + 1
    

End Sub

rogeryao iT邦超人 7 級 ‧ 2022-04-26 11:21:03 檢舉

借花獻佛,玩一下...
也希望版主的目的是技術交流,而不是來要程式碼的

Private Sub CommandButton1_Click()
    Set d = CreateObject("Scripting.Dictionary")
    arr = Range("a2:g" & [a1048576].End(xlUp).Row)
    For i = 1 To UBound(arr)
        '排除型號空白
        If WorksheetFunction.Trim(arr(i, 1)) <> "" Then
            d(arr(i, 1)) = d(arr(i, 1)) + 1
        End If
    Next
    k = d.keys
    t = d.items
    
    [c1].Resize(1, 2) = Array("型號", "重覆次數")
    [c2].Resize(d.Count, 1) = Application.Transpose(k)
    [d2].Resize(d.Count, 1) = Application.Transpose(t)

   '排序
    Columns("C:D").Select
    ActiveWorkbook.Worksheets("工作表1").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("工作表1").Sort.SortFields.Add Key:=Range("C2:C" & d.Count + 1), _
        SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("工作表1").Sort
        .SetRange Range("C1:D" & d.Count + 1)
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With

    Y = 3
    X = 7
    j = 5
    i = 0
    record_no = 0
    
    remind_no = UBound(k) + 1
    row_count = WorksheetFunction.RoundUp(remind_no / j, 0)
    
    Worksheets("工作表1").Activate
    Worksheets("工作表1").Cells(1, X + 2) = "商品目錄表"
    Worksheets("工作表1").Range(Worksheets("工作表1").Cells(1, X + 2), Worksheets("工作表1").Cells(1, X + 2 + (j * 2) - 1)).Select
    With Selection
      .HorizontalAlignment = xlCenterAcrossSelection '跨欄置中
      '填滿的顏色: 36 : 淺黃色
      .Interior.ColorIndex = 36
    End With

    Do
        If i = 0 Then
            X = X + 2
        ElseIf record_no Mod row_count = 0 Then
            Y = 3
            X = X + 2
            j = j - 1
            remind_no = remind_no - record_no
            row_count = WorksheetFunction.RoundUp(remind_no / j, 0)
            record_no = 0
        Else
            Y = Y + 1
        End If
        
        If Y = 3 Then
          Cells(2, X) = "型號"
          Cells(2, X + 1) = "重覆次數"
        End If
        
        Cells(Y, X) = Cells(i + 2, 3)
        Cells(Y, X + 1) = Cells(i + 2, 3 + 1)
        i = i + 1
        record_no = record_no + 1
    Loop Until i = UBound(k) + 1

End Sub

https://ithelp.ithome.com.tw/upload/images/20220426/20085021elBn5te2UD.png

peterzxcv iT邦新手 5 級 ‧ 2022-04-26 20:42:00 檢舉

我自己寫的迴圈及代數很多(土法煉鋼式)@@",但沒兩位大師寫的簡潔有力且執行快速,謝謝大師幫忙,邏輯性寫法值得學習,目前發覺有時要人工補寫幾筆在表上,所以我調整blanksoul12大師的語法先讓前四大欄均衡,第五大欄留些空格好能接續,高手語法畢竟讓人看了很清楚知道甚麼作用且觀念,所以也還使用這語法,有時剩的空格只一兩格不夠銜接時就要再全放入程式執行即可,再次感謝blanksoul12及rogeryao的幫忙。

Sub test()

Range("A2:a999").Sort Key1:=Cells(1, 1), order1:=xlAscending

    Set d = CreateObject("Scripting.Dictionary")
    arr = Range("a2:g" & [a1048576].End(xlUp).Row)
    For i = 1 To UBound(arr)
        If WorksheetFunction.Trim(Cells(i, "a")) <> "" Then
            d(arr(i, 1)) = d(arr(i, 1)) + 1
        End If
    Next
    k = d.keys
    t = d.items
    
    [c1].Resize(1, 2) = Array("型號", "重覆次數")
    [c2].Resize(d.Count, 1) = Application.Transpose(k)
    [d2].Resize(d.Count, 1) = Application.Transpose(t)
    
    Z = (UBound(k) \ 5) + 1
   y = 6
    x = 1
    For i = 0 To UBound(k)
        If i < Z Then
            x = x + 1
        ElseIf i Mod Z = 0 Then
            y = y + 2
            x = 1
            x = x + 1
        Else
            x = x + 1
        End If
        Cells(x + 1, y) = k(i)
        Cells(x + 1, y + 1) = t(i)
    Next
  End Sub
blanksoul12 iT邦研究生 5 級 ‧ 2022-04-28 10:04:37 檢舉

你這個寫法好像會出問題的啊,會變成三級而不是兩級的

peterzxcv iT邦新手 5 級 ‧ 2022-04-28 19:41:32 檢舉

我執行語法看起來正常如圖,請問三級、兩級是甚麼,不太懂能多指導,謝謝
https://ithelp.ithome.com.tw/upload/images/20220428/201203689QHVKGl0Q1.jpg

我要發表回答

立即登入回答