最有效的VBA代码将stringvariables转换为整数

我有以下代码转换以下格式的string:
"G##"为整数##

 Dim str As String Dim int As Integer str = "G14" int = CInt(Right(str, Len(str) - 1)) 

但是这将定期在大型数据库上运行。

我想知道是否有其他可能更有效的替代scheme(特别是关于最后一行)?

在我的testing使用

  Mid$(strTest, 1, 1) = "0" lngTest = CLng(strTest) 

比使用时快30-40%

 CLng(Right$(strTest, Len(strTest) - 1)) 

这反过来又显着快于

 CLng(Right(strTest, Len(strTest) - 1)) 

我使用了Long因为它对Integer是优越的速度

对于多个替代,一个RegExp可能会进入它自己的。 这个开销太高,不适合这个样本

testing代码

 Sub Test() Dim dbTime As Double Dim strTest As String Dim lngTest As Long Dim lngCnt As Long strTest = "G14" dbTime = Timer For lngCnt = 1 To 1000000 lngTest = CLng(Right$(strTest, Len(strTest) - 1)) Next lngCnt Debug.Print Timer - dbTime dbTime = Timer For lngCnt = 1 To 1000000 lngTest = CLng(Right(strTest, Len(strTest) - 1)) Next lngCnt Debug.Print Timer - dbTimer dbTime = Timer For lngCnt = 1 To 1000000 Mid$(strTest, 1, 1) = "0" lngTest = CLng(strTest) Next lngCnt Debug.Print Timer - dbTime End Sub 

我试着用int = CInt(Mid $(str,2))代码进行100,000,000次迭代,Mid $语句稍微快了一点(在我的机器上是6秒),但这是很多次迭代。 但是,当我向你的Right函数中添加$时 ,它们以相同的速度运行。 使用string函数Right$对string的优化比变体版本Right更好。 所以唯一的build议,我必须使用string优化版本右$。

 Dim str As String Dim int As Integer str = "G14" int = CInt(Right$(str, Len(str) - 1))