Extract Right VBA

billandrew

Well-known Member
Joined
Mar 9, 2014
Messages
743
Trying to extract number value in string using VBA. The range dynamic.

Thanks for any help...

BeforeAfter
WEST 1 (LEFT)1
WEST 1 W1
WEST 22
SOUTH 1 (RIGHT)1

<tbody>
</tbody>
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Does it always follow the format:
- one word
- one space
- number
- another word (or end of string)?

So, we could say that the number ALWAYS appear after the first space in the string?

Are you just wanting to replace the existing entry with the number (in the same cell)?
 
Upvote 0
If my previous assumptions are correct, then a simple Text to Columns, returning only the second column when using the space as a delimiter, should work.
The VBA code for that would look something like:
Code:
Sub MyGetNums()
    Columns("A:A").TextToColumns Destination:=Range("A1"), DataType:=xlDelimited, _
        TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=True, Tab:=False, _
        Semicolon:=False, Comma:=False, Space:=True, Other:=False, FieldInfo _
        :=Array(Array(1, 9), Array(2, 1), Array(3, 9)), TrailingMinusNumbers:=True
End Sub
 
Upvote 0
One exception may occur, there may not be a word following the number.
Yes, that is what I meant by:
- another word (or end of string)?
See if the code in my previous post works for you (assuming data in column A).
 
Upvote 0
Works Great for the examples provided. Thank You

Although there may be text strings such as.

West 1 (North Region)
West 2 (Pacific Region)

<tbody>
</tbody>

In which case there is an additional space.. ugh
 
Upvote 0
I think if you just add another field value, it should work, i.e.
Code:
Sub MyGetNums()
    Columns("A:A").TextToColumns Destination:=Range("A1"), DataType:=xlDelimited, _
        TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=True, Tab:=False, _
        Semicolon:=False, Comma:=False, Space:=True, Other:=False, FieldInfo _
        :=Array(Array(1, 9), Array(2, 1), Array(3, 9)[COLOR=#ff0000], Array(4, 9)[/COLOR]), TrailingMinusNumbers:=True
End Sub
Note in adding additional field, the first number in the array is the field number (it is sequential), and the second is the data format (9 is "Do Not Import (skip)").
 
Upvote 0
awesome.

How does the array work? The 1, I see is for each field (The example has 4 fields). The second array (Number 1) captures the number?

Thank You very much
 
Upvote 0
The second array (Number 1) captures the number?
the second is the data format (9 is "Do Not Import (skip)").
If you perform a Text to Columns manually, on Step 3, you choose the format of each field the options are:
General - this is "1" (used for numbers)
Text - this is "2"
Date - this is "3"
Do not import column (skip) - this is 9

So the second field is the one we want, and we use "1" for that. The rest we use "9", since we do not want to import.
 
Upvote 0

Forum statistics

Threads
1,215,353
Messages
6,124,462
Members
449,163
Latest member
kshealy

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