|
发表于 2013-6-22 00:36
|
显示全部楼层
本楼为最佳答案
本帖最后由 adders 于 2013-6-21 11:38 编辑
If Range is not big (like you said, A1:A100), just use Collection:- Sub tt1()
- Range("B1:B100").ClearContents
- Dim Cel As Range, colMG As Collection, i As Byte
- Set colMG = New Collection
-
- On Error Resume Next
- For Each Cel In Range("A1:A100")
- colMG.Add Cel, CStr(Cel)
- Next Cel
- On Error GoTo 0
-
- For i = 1 To colMG.Count
- Range("B" & i) = colMG(i)
- Next i
- End Sub
复制代码 With a huge Range in A column, for speed, you can combine array and collection:- Sub tt2()
- Range("B1:B" & Range("B1").End(xlDown).Row).ClearContents
- Dim a(), b(), colMG As Collection, i As Long, n As Long
- a = Range("A1:A" & Range("A1").End(xlDown).Row)
- Set colMG = New Collection
-
- On Error Resume Next
- For i = LBound(a) To UBound(a)
- colMG.Add a(i, 1), CStr(a(i, 1))
- Next i
- On Error GoTo 0
-
- n = colMG.Count
- ReDim b(1 To n)
- For i = 1 To n
- b(i) = colMG(i)
- Next i
-
- Range("B1").Resize(n, 1) = Application.Transpose(b)
- End Sub
复制代码 Example file attached:
|
|