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...

[TABLE="class: grid, width: 500, align: center"]
<tbody>[TR]
[TD]Before[/TD]
[TD]After[/TD]
[/TR]
[TR]
[TD]WEST 1 (LEFT)[/TD]
[TD]1[/TD]
[/TR]
[TR]
[TD]WEST 1 W[/TD]
[TD]1[/TD]
[/TR]
[TR]
[TD]WEST 2[/TD]
[TD]2[/TD]
[/TR]
[TR]
[TD]SOUTH 1 (RIGHT)[/TD]
[TD]1[/TD]
[/TR]
</tbody>[/TABLE]
 
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

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
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!:laugh:
 
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!:laugh:
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,223,911
Messages
6,175,323
Members
452,635
Latest member
laura12345

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