|
- ' 更新代码
- Option Explicit
- Sub Demo()
- Const DATA_RNG = "C3:G7"
- Const START_CELL As String = "E5"
- Dim iCol As Long: iCol = 24 ' Col X
- Dim rngData As Range, colRes As New Collection, i As Long, aTxt, j As Long
- Set rngData = Range(DATA_RNG)
- FindCells 1, Range(START_CELL), rngData, START_CELL, colRes
- For i = 1 To colRes.Count
- aTxt = Split(colRes(i), ",")
- For j = 0 To UBound(aTxt)
- Cells(1 + j, iCol) = aTxt(j)
- Next
- iCol = iCol + 1
- Next
- End Sub
- Sub FindCells(iRow As Long, firstCell As Range, rngData As Range, sCells As String, ByRef colRes As Object)
- Dim i As Long, j As Long, fullRng As Range, c As Range
- Dim r As Range, sCellsList As String, RowCnt As Long
- Set r = Range(sCells)
- RowCnt = rngData.Rows.Count
- For i = iRow To RowCnt
- If i + rngData.Row - 1 <> firstCell.Row Then
- Set fullRng = Union(r.EntireRow, r.EntireColumn)
- For Each c In rngData.Rows(i).Cells
- If c.Column <> firstCell.Column Then
- If Intersect(c, fullRng) Is Nothing Then
- Set r = Union(c, r)
- sCellsList = sCells & "," & c.Address(0, 0)
- If i = RowCnt Then
- Debug.Print sCellsList
- colRes.Add sCellsList
- Else
- FindCells i + 1, firstCell, rngData, sCellsList, colRes
- End If
- End If
- End If
- Next
- End If
- Next
- End Sub
复制代码 |
评分
-
1
查看全部评分
-
|