I'm trying to search for number-type characters in consecutive positions (at least 3) in a string. For example, if I have this string:
"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla purus dui, lobortis non 54leo non, feugiat venenatis urna. Morbi lobortis ligula tincidunt 1844763, accumsan massa vel, placerat libero. In a nisl in leo lacinia 243 ullamcorper eget id tortor. Cras vehicula malesuada luctus. Donec egestas non arcu in blandit. Donec eu lacinia ipsum, et consequat mi. Nulla 46626 laoreet viverra purus fringilla pellentesque. Mauris sit amet pulvinar velit, at dignissim lacus. Maecenas non sollicitudin ex. Fusce luctus enim eff43icitur aliquet finibus. Nam ac 1fermentum lacus."
I want my VBA script to return this:
1844763
243
46626
This is the script I'm currently working with:
start = 1
Do
If IsNumeric(Mid(Sheets("Sheet1").Cells(x, 1), start, 1)) Then
If start = Len(Sheets("Sheet1").Cells(x, 1)) Then
Exit Do
End If
If IsNumeric(Mid(Sheets("Sheet1").Cells(x, 1), start + 1, 1)) Then
If start + 1 = Len(Sheets("Sheet1").Cells(x, 1)) Then
Exit Do
End If
If IsNumeric(Mid(Sheets("Sheet1").Cells(x, 1), start + 2, 1)) Then
Sheets("Sheet1").Cells(x, 2).Interior.Color = RGB(255, 0, 0)
Sheets("Sheet1").Cells(x, 2) = Sheets("Sheet1").Cells(x, 2) & Mid(Sheets("Sheet1").Cells(x, 1), start, 3)
start = start + 3
While IsNumeric(Mid(Sheets("Sheet1").Cells(x, 1), start, 1))
Sheets("Sheet1").Cells(x, 2) = Sheets("Sheet1").Cells(x, 2) & Mid(Sheets("Sheet1").Cells(x, 1), start, 1)
start = start + 1
Wend
Sheets("Sheet1").Cells(x, 2) = Sheets("Sheet1").Cells(x, 2) & vbCrLf
End If
End If
End If
If Not IsNumeric(Mid(Sheets("Sheet1").Cells(x, 1), start, 1)) Then
start = start + 1
End If
Loop While inicio < Len(Sheets("Comments").Cells(x, 1))
The script works just fine with small strings (10-20 characters). Things get messy when dealing with strings like the one above (my computer slows down significantly and excel becomes non responsive forever). Do you have any idea on how to optimize this code?
Thank you!