|
练手
- Public Sub wang_way()
- Set wb = Application.ThisWorkbook
- Set sht = wb.Worksheets("原数据")
- Set dic = CreateObject("Scripting.Dictionary")
- With sht
- eRow = .Cells(.Rows.Count, 1).End(xlUp).Row
- Set rng = .Range("A2:Z" & eRow)
- rng.Sort rng.Cells(1, 2), order1:=xlAscending, Header:=xlNo
- arr = rng.Value
- For i = LBound(arr) To UBound(arr)
- Key = Left(arr(i, 2), 1)
- If Not dic.exists(Key) Then dic(Key) = dic.Count + 2
- Next i
- rng.Sort rng.Cells(1, 1), order1:=xlAscending, Header:=xlNo
- arr = rng.Value
- Dim ar(): ReDim ar(1 To UBound(arr), 1 To 1 + dic.Count)
- For i = LBound(arr) To UBound(arr)
- Key = Left(arr(i, 2), 1)
- ar(i, 1) = arr(i, 1)
- ar(i, dic(Key)) = arr(i, 2)
- Next i
- br = dic.keys
- .Range("h1").Resize(1, 1 + dic.Count).Value = Array("品名", "区域A", "区域B", "区域C", "区域D")
- .Range("h2").Resize(UBound(arr), 1 + dic.Count).Value = ar
- End With
- End Sub
复制代码 |
|