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>
 
You are welcome!
I like it when people don't just want answers, but want to understand the logic behind it!:)
 
Upvote 0

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
The answer in my opinion is the easy way out. Understanding the logic is the key to developing code on your own and then possibly assisting others as you and the many others on this forum continue to do daily.

Thanks again.
 
Upvote 0
As always, there is more than one way to code a routine, so here is another macro that you can consider...
Code:
[table="width: 500"]
[tr]
	[td]Sub GetNum()
  Dim Addr As String
  Addr = "A1:A" & Cells(Rows.Count, "A").End(xlUp).Row
  Range(Addr) = Evaluate(Replace("IF({1},MID(LEFT(@,FIND("" "",@&"" "",FIND("" "",@)+1)),FIND("" "",@)+1,99))", "@", Addr))
End Sub[/td]
[/tr]
[/table]
And, while this looks "longer", it is actually quite fast...
Code:
[table="width: 500"]
[tr]
	[td]Sub GetNumWhichIsSecondField()
  Dim R As Long, Data As Variant
  Data = Range("A1", Cells(Rows.Count, "A").End(xlUp))
  For R = 1 To UBound(Data)
    Data(R, 1) = Split(Data(R, 1))(1)
  Next
  Range("A1").Resize(UBound(Data)) = Data
End Sub[/td]
[/tr]
[/table]
 
Upvote 0
And, while this looks "longer", it is actually quite fast...
Are we in "bizarro" world, Rick?
Usually, it is me posting the "loop" solutions, and you posting the more concise non-loop solutions!:LOL:
 
Upvote 0
Are we in "bizarro" world, Rick?
Usually, it is me posting the "loop" solutions, and you posting the more concise non-loop solutions!:LOL:
Well, to be fair, I did post two routines the first of which was a non-looping solution.:wink: I included the looping solution as an afterthought, sort of to go along with my statement "As always, there is more than one way to code a routine". See, not so bizarre after all.:LOL:
 
Upvote 0
See, not so bizarre after all
I guess maybe "ironic" would be a better word!
Maybe it is just amusing to no one else but me!;)
 
Upvote 0
I guess maybe "ironic" would be a better word!
Maybe it is just amusing to no one else but me!;)
No, I understand you bemusement completely. I will be the first to admit that some (a lot?) of my solutions would be hard to read/fathom by someone early in their programming career, so having a familiar looking looping solution in a thread is definitely useful. Usually, someone posts a looping solution in a thread like this, but no one did... and, as I said, my constructing/posting one was sort of an afterthought.
 
Upvote 0
I will be the first to admit that some (a lot?) of my solutions would be hard to read/fathom by someone early in their programming career, so having a familiar looking looping solution in a thread is definitely useful. Usually, someone posts a looping solution in a thread like this, but no one did... and, as I said, my constructing/posting one was sort of an afterthought.
Agreed. It is nice to show the different ways.

Its kind of funny actually. I started off doing a looping solution, they thought "Wait, we could just use Text-to-Columns here!". So I scratched my looping solution.
 
Upvote 0

Forum statistics

Threads
1,216,033
Messages
6,128,427
Members
449,450
Latest member
gunars

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