|
- Sub Test()
- Dim SH As Worksheet
- Dim lngRow As Long, lngCol As Long, arr As Variant
- Dim objDic As Object, strKey As String, strItem As String
- Dim strSplit() As String, lngI As Long
-
- Set SH = Sheets("Sheet1")
- lngRow = SH.Range("F" & Rows.Count).End(xlUp).Row
- arr = SH.Range("F3:G" & lngRow)
-
- Set objDic = CreateObject("Scripting.Dictionary")
-
- '将数据装入字典
- For lngRow = LBound(arr) To UBound(arr)
- strKey = arr(lngRow, 1)
- strItem = arr(lngRow, 2)
- objDic(strKey) = objDic(strKey) & "," & strItem
- Next
-
- '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
- '按列显示:一个科目一列
- arr = SH.Range("A2:D2")
- For lngCol = 2 To UBound(arr, 2)
- strKey = arr(1, lngCol)
- strItem = objDic(strKey)
- strItem = Mid(strItem, 2)
- strSplit = Split(strItem, ",")
- SH.Cells(3, lngCol).Resize(UBound(strSplit) + 1, 1) = Application.WorksheetFunction.Transpose(strSplit)
- Next
- ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
-
- '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
- '按行显示:一个科目一行
- arr = SH.Range("B21:C29")
- For lngRow = 1 To UBound(arr)
- strKey = arr(lngRow, 1)
- strItem = objDic(strKey)
- strItem = Mid(strItem, 2)
- strSplit = Split(strItem, ",")
- arr(lngRow, 2) = strSplit(0)
-
- strSplit(0) = ""
- strItem = Join(strSplit, ",")
- objDic(strKey) = strItem
- Next
- SH.Range("B21:C29") = arr
- ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
- End Sub
复制代码 |
|