Reference Numbers in a Text Box VBA

bvokey

New Member
Joined
Jun 1, 2015
Messages
35
Hello,

I am trying to reference a certain word or number in a text box and assign a variable to it. From this variable I want to adjust the height of an object.
For example, in the text box if I enter "Object height is 300 meters" or "Object height is 350.9 m" I want the macro to find 300 (350.9 in 2nd ex.) and then assign it to a variable. Is this possible?

Thanks.
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Is there only 1 text box? and if it always going to be 1 number only in the text?
 
Upvote 0
Well no, there were be multiple, but each text box will control a different object. And no, in most instances there will be more than one number in the text box. If it helps though it would be acceptable to have an "@" in front of the number which I want to reference. For ex. "Object @ 300m".
 
Upvote 0
to make it clear, so it always will have @ before the number you want to extract and only before that number?
 
Upvote 0
EDIT: try this (corrected version)

Code:
Option Explicit
Sub theCode()
Dim strNum As String
Dim i As Long
With ActiveSheet.Shapes
For i = 1 To .Count
    If .Item(i).Type = msoTextBox Then
        strNum = .Item(i).TextFrame.Characters.Text
        ExtractNumber (strNum)
    
    End If
Next i
End With
 
End Sub

Function ExtractNumber(ByVal strNum As String) As Double
Dim i As Integer, iFirst As Integer, iLast As Integer

For i = 1 To Len(strNum)
    If IsNumeric(Mid(strNum, i, 1)) Then
        If iFirst = Empty Then
            iFirst = i
        Else
            iLast = i
        End If
    Else
        If Not iLast = Empty Then Exit For
    End If
Next i
ExtractNumber = Mid(strNum, iFirst, iLast - iFirst + 1)
End Function
 
Last edited:
Upvote 0
Sorry above code didn't take into account the decimal places and eventual commas after thousands. Please see adjusted below.

Code:
Option Explicit
Sub theCode()
Dim strNum As String
Dim i As Long
With ActiveSheet.Shapes
For i = 1 To .Count
    If .Item(i).Type = msoTextBox Then
        strNum = .Item(i).TextFrame.Characters.Text
        ExtractNumber (strNum)
    
    End If
Next i
End With
 
End Sub

Function ExtractNumber(ByVal strNum As String) As Double
Dim dNum As Double
Dim i As Integer, iFirst As Integer, iLast As Integer

For i = 1 To Len(strNum)
    If IsNumeric(Mid(strNum, i, 1)) Or Mid(strNum, i, 1) = "." Or Mid(strNum, i, 1) = "," Then
        If iFirst = Empty Then
            iFirst = i
        Else
            iLast = i
        End If
    Else
        If Not iLast = Empty Then Exit For
    End If
Next i
ExtractNumber = Mid(strNum, iFirst, iLast - iFirst + 1)
End Function
 
Upvote 0

Forum statistics

Threads
1,203,099
Messages
6,053,523
Members
444,669
Latest member
Renarian

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top