2013-05-22 68 views
1

我需要編寫一個函數來獲取幫助,該函數將採用一串字符串並根據字符串中的數字字符(升序)將它分類爲一個新的字符串數組? 說我有字符串的以下數組:字宏中的字符串排序

"abc53ddd", "2zzz2yyy", "14"

我需要它們列爲

"14", "2zzz2yyy" and "abc53ddd"

如該串中的字符數是14, 22 and 53. 我可以去除串入數字部分和排序陣列,但我無法恢復剩餘的字符,以正確的順序將它們列出...

+0

你已經嘗試過什麼嗎? –

回答

0

將ADO與斷開的記錄集一起使用。請參閱http://technet.microsoft.com/en-us/library/ee176578.aspx。它速度快,強大。恕我直言,這是最好的。

'recordset definition 
Dim rst As New ADODB.Recordset 'must be reference "Microsoft ActiveX Data Objects" 
'OR use automation (without reference) 
'Dim rst 
'Set rst = CreateObject("ADOR.Recordset") 

'fields definition - how many you want (name, type, lenght) 
rst.Fields.Append "field1", ADODB.DataTypeEnum.adVarChar, 255 
rst.Fields.Append "number", ADODB.DataTypeEnum.adInteger 

rst.Open 

'add values, for example: 
Dim rc As ADODB.Record 
rst.AddNew "field1", "abc53ddd" 
rst.AddNew "field1", "2zzz2yyy" 
rst.AddNew "field1", "14" 

'make numbers from string and store in field [number] 
Dim reg As New RegExp 
'OR 
'Set reg = CreateObject("vbscript.regexp") 
reg.Pattern = "\D" 'non-digit 
reg.Global = True 

rst.MoveFirst 
Do Until rst.EOF 
    rst!Number = Val(reg.Replace(rst!field1, "")) 'delete non-digit 
    rst.MoveNext 
Loop 

'sort! 
rst.Sort = "number ASC" 'sort by field1 ascending 

'show output 
rst.MoveFirst 
Do Until rst.EOF 
    Debug.Print rst!field1, rst!Number 
    rst.MoveNext 
Loop