|
- Sub Test()
- myCount '无参数或参数为0 ,正序
-
- ' myCount 1 '参数非零,倒序
- End Sub
- Function myCount(Optional intDirection As Integer = 0)
- Dim lngRows As Long, lngRow As Long
- Dim arrSource As Variant, arrResult As Variant, arrTemp As Variant
- Dim strCurr As String, strPre As String
- Dim lngCount As Long, lngID As Long
-
- lngRows = Sheet1.Range("A" & Rows.Count).End(xlUp).Row
- arrSource = Sheet1.Range("A1:A" & lngRows)
-
- strPre = arrSource(1, 1)
- lngID = 0
- lngCount = 1
- ReDim arrResult(1 To 2, 1 To 1)
- For lngRow = 2 To lngRows
- strCurr = arrSource(lngRow, 1)
- If Trim(strPre) = "" And lngRow <> lngRows Then
- strPre = strCurr
- lngCount = 1
- Else
- If strCurr <> strPre Then
- lngID = lngID + 1
- ReDim Preserve arrResult(1 To 2, 1 To lngID)
- arrResult(1, lngID) = strPre
- arrResult(2, lngID) = lngCount
- strPre = strCurr
- lngCount = 1
- Else
- lngCount = lngCount + 1
- End If
- If lngRow = lngRows Then
- ReDim Preserve arrResult(1 To 2, 1 To lngID + 1)
- arrResult(1, lngID + 1) = strPre
- arrResult(2, lngID + 1) = lngCount
- End If
- End If
- Next
-
- arrResult = Application.WorksheetFunction.Transpose(arrResult)
-
-
- If intDirection <> 0 Then
- arrTemp = arrResult
- lngID = UBound(arrTemp)
- For lngRows = LBound(arrTemp) To UBound(arrTemp)
- arrResult(lngID - lngRows + LBound(arrTemp), 1) = arrTemp(lngRows, 1)
- Next
- End If
-
- Sheet1.Range("D1").Resize(UBound(arrResult), 2) = arrResult
- End Function
复制代码 |
|