Extracting Strings From A Single Dynamic String

Ark68

Well-known Member
Joined
Mar 23, 2004
Messages
3,868
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I have a string value that takes the form of ###/###.
I am looking for VBA code suggestions to isolate the numbers (as text) preceding the "/" and proceeding the "/", and then convert each to true numbers.
Both numeric values will be whole numbers, and could be of any length (value) from 1 - x digits.

Can anyone offer up a solution?

I have determined the position of the "/", and the length of the string (respectively) with this code:
Code:
spos = InStr(.Cells(srw, cl), "/")
slen = Len(.Cells(srw, cl))

Example 1:
string (from .cells(row,col)) = "32/3"
spos = 3
slen = 4
fwdnum = 32
bcknum = 3

Example 2:
string (from .cells(row,col)) = "1091/155"
spos = 5
slen = 8
fwdnum = 1091
bcknum = 155
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.

Eric W

MrExcel MVP
Joined
Aug 18, 2015
Messages
10,807
Perhaps:

VBA Code:
Sub SplitString()
Dim fwdnum As Long, bcknum As Long, str1 As String

    str1 = "12345/678"
 
    fwdnum = Left(str1, InStr(str1 & "/", "/") - 1)
    bcknum = Mid(str1, Len(fwdnum & "") + 2)

End Sub
 
Solution

steve the fish

Well-known Member
Joined
Oct 20, 2009
Messages
8,388
Office Version
  1. 365
Platform
  1. Windows
Like this?

VBA Code:
Sub test()

Dim str As String, fwdnum As Long, bcknum As Long

str = "123/456"

fwdnum = Split(str, "/")(0)
bcknum = Split(str, "/")(1)

MsgBox fwdnum
MsgBox bcknum

End Sub
 

Ark68

Well-known Member
Joined
Mar 23, 2004
Messages
3,868
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Thank you both! Each one appears to be providing the results I'm needing!
 

Watch MrExcel Video

Forum statistics

Threads
1,129,549
Messages
5,636,941
Members
416,953
Latest member
prakashkumar

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
Top