|

楼主 |
发表于 2017-2-8 16:05
|
显示全部楼层
Sub 按单元格字符串中的数字升序排列()
Dim ws As Worksheet
Dim myrange As Range
Dim myrow As Long
Dim mycolumn As Long
Dim i As Long
Set ws = Worksheets(1)
With ws
With .Range("a1").CurrentRegion
mycolumn = .Columns.Count
myrow = .Rows.Count
End With
.Columns(mycolumn + 1).Insert
For i = 2 To myrow
.Cells(i, mycolumn + 1).Value = findnumber(.Cells(i, 1).Text)
Next i
Set myrange = .Range("a1").CurrentRegion
With myrange
.Sort key1:=.Cells(1, mycolumn + 1), order1:=xlAscending, key2:=.Cells(1, 1), order2:=xlAscending, Header:=xlYes
End With
.Columns(mycolumn + 1).Delete
End With
Set myrange = Nothing
Set ws = Nothing
End Sub
Function findnumber(mystring As String)
Dim i As Integer
Dim j As Integer
Dim mynum As String
For i = 1 To Len(mystring)
If IsNumeric(Mid(mystring, i, 1)) Then
j = j + 1
mynum = mynum & Mid(mystring, i, 1)
End If
If j = 1 Then mynum = CInt(Mid(mystring, i, 1))
Next i
findnumber = CLng(mynum)
End Function
|
|