|
- Option Explicit
- '示例
- Sub Test()
- Dim arrSource As Variant, arrResult As Variant
- Dim lngCount As Long
-
- '抽取【A1:D5】区域的数据进行组合,将结果写入E1起始的单元格
- '不要求回传结果数量
- arrSource = Sheet1.Range("A1:D5")
- If CombinByCol(arrSource, arrResult) Then
- Sheet1.Range("E1").Resize(UBound(arrResult), UBound(arrResult, 2)) = arrResult
- End If
-
- '抽取【L1:N5】区域的数据进行组合,将结果写入E1起始的单元格
- '要求回传结果数量
- arrSource = Sheet1.Range("L1:N5")
- If CombinByCol(arrSource, arrResult, lngCount) Then
- Sheet1.Range("Q1").Resize(UBound(arrResult), UBound(arrResult, 2)) = arrResult
- MsgBox "共有组合" & lngCount & "种"
- End If
-
- End Sub
- '**主方法
- '在arrData 区域,逐列、逐行 提取数据进行组合,并将组合结果赋值给arrReturn,结果总数赋值给lngCount
- Function CombinByCol(arrData As Variant, arrReturn As Variant, Optional lngCount As Long) As Boolean
- Dim arrTemp As Variant, lngRow As Long
- arrReturn = ""
- For lngRow = LBound(arrData) To UBound(arrData)
- ExtractData arrData, arrTemp, LBound(arrData, 2), lngRow, arrReturn
- Next
-
- If IsArray(arrReturn) Then
- TransRowCol arrReturn
- lngCount = UBound(arrReturn)
- CombinByCol = True
- Else
- lngCount = 0
- CombinByCol = False
- End If
- End Function
- '递归查找
- Private Function ExtractData(ByRef arrSource As Variant, ByRef arrRes As Variant, ByVal ColID As Long, ByVal RowID As Long, arrReturn As Variant)
- Dim lngRow As Long, lngCol As Long, varTemp As Variant
- Dim blStart As Boolean, blEnd As Boolean
- Dim arrValue As Variant
-
- '如果是起始列,判断结果集是否存在,不存在则创建
- If ColID <= LBound(arrSource, 2) Then
- If Not IsArray(arrRes) Then ReDim arrRes(LBound(arrSource, 2) To UBound(arrSource, 2))
- ColID = LBound(arrSource, 2)
- End If
- '提取数据,判断是否为空,不为空则写入,否则退出
- varTemp = arrSource(RowID, ColID)
- If Not CheckData(varTemp) Then Exit Function
- arrRes(ColID) = varTemp
- '如果为结束列,表明提取成功,将结果写入,并退出
- If ColID = UBound(arrSource, 2) Then
- PutDataToResult arrRes, arrReturn
- Exit Function
- End If
- '递归 提取下一列的数据
- For lngRow = LBound(arrSource) To UBound(arrSource)
- ExtractData arrSource, arrRes, ColID + 1, lngRow, arrReturn
- Next
- End Function
- '将结果写入结果库
- Function PutDataToResult(arrValue As Variant, ByRef arrResultSet As Variant)
- Dim lngID As Long, lngCol As Long
- If IsArray(arrResultSet) Then
- lngID = UBound(arrResultSet, 2) + 1
- Else
- ReDim arrResultSet(LBound(arrValue) To UBound(arrValue), 0)
- lngID = 1
- End If
-
- ReDim Preserve arrResultSet(LBound(arrValue) To UBound(arrValue), 1 To lngID)
- For lngCol = LBound(arrValue) To UBound(arrValue)
- arrResultSet(lngCol, lngID) = arrValue(lngCol)
- Next
- End Function
- '行列转换
- Function TransRowCol(arr As Variant)
- Dim arrTemp As Variant, lngRow As Long, lngCol As Long
- ReDim arrTemp(LBound(arr, 2) To UBound(arr, 2), LBound(arr) To UBound(arr))
-
- For lngRow = LBound(arr) To UBound(arr)
- For lngCol = LBound(arr, 2) To UBound(arr, 2)
- arrTemp(lngCol, lngRow) = arr(lngRow, lngCol)
- Next
- Next
- arr = arrTemp
- End Function
- '判断 数据是否为空
- Function CheckData(ByRef Value As Variant) As Boolean
- Dim strTemp As String
- strTemp = Trim(CStr(Value))
- If strTemp = "" Then GoTo ExitFun
- CheckData = True
- Exit Function
- ExitFun:
- CheckData = False
- End Function
复制代码
|
评分
-
3
查看全部评分
-
|