Extracting Numeric / Alpha-Numeric data from Cells using VBA

danburgan

New Member
Joined
Sep 10, 2014
Messages
3
Good Afternoon All,

I am trying to automate a task to take data from one column and copy a district identifier to another. The data and the expected outcome are below.

[TABLE="width: 272"]
<colgroup><col><col></colgroup><tbody>[TR]
[TD]District[/TD]
[TD]Output[/TD]
[/TR]
[TR]
[TD]Congressional District 1[/TD]
[TD="align: right"]1[/TD]
[/TR]
[TR]
[TD]Congressional District 2[/TD]
[TD="align: right"]2[/TD]
[/TR]
[TR]
[TD]Congressional District 3[/TD]
[TD="align: right"]3[/TD]
[/TR]
[TR]
[TD]Congressional District 4[/TD]
[TD="align: right"]4[/TD]
[/TR]
[TR]
[TD]Congressional District 5[/TD]
[TD="align: right"]5[/TD]
[/TR]
[TR]
[TD]State Senate District 21[/TD]
[TD="align: right"]21[/TD]
[/TR]
[TR]
[TD]District 21[/TD]
[TD="align: right"]21[/TD]
[/TR]
[TR]
[TD]District 30A[/TD]
[TD]30A[/TD]
[/TR]
[TR]
[TD]District 30B[/TD]
[TD]30B[/TD]
[/TR]
[TR]
[TD]District 31A[/TD]
[TD]31A[/TD]
[/TR]
[TR]
[TD]5th Judicial Circuit [/TD]
[TD="align: right"]5[/TD]
[/TR]
[TR]
[TD]5th Judicial Circuit [/TD]
[TD="align: right"]5[/TD]
[/TR]
[TR]
[TD]Legislative District 21 [/TD]
[TD="align: right"]21[/TD]
[/TR]
</tbody>[/TABLE]


I can easily pull data from the first few rows using Mid, but I have three different scenarios to look at and the amount of data in the sheet is variable.

Any assistance with this is greatly appreciated.

Dan
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Since I just noticed that there are some strings with numbers at the beginning, I think this one will work better.

=LEFT(MID(A1,MIN(SEARCH({0,1,2,3,4,5,6,7,8,9},A1&1234567890)),LEN(A1)),FIND(" ",A1))
 
Upvote 0
AlKey,

So close and thank you for the assistance.

When I add the formula and view results we catch 2 out of 3 of the scenarios.

[TABLE="width: 272"]
<tbody>[TR]
[TD]District[/TD]
[TD]Output[/TD]
[/TR]
[TR]
[TD]Congressional District 1[/TD]
[TD]1[/TD]
[/TR]
[TR]
[TD]Congressional District 2[/TD]
[TD]2[/TD]
[/TR]
[TR]
[TD]Congressional District 3[/TD]
[TD]3[/TD]
[/TR]
[TR]
[TD]Congressional District 4[/TD]
[TD]4[/TD]
[/TR]
[TR]
[TD]Congressional District 5[/TD]
[TD]5[/TD]
[/TR]
[TR]
[TD]State Senate District 21[/TD]
[TD]21[/TD]
[/TR]
[TR]
[TD]District 21[/TD]
[TD]21[/TD]
[/TR]
[TR]
[TD]District 30A[/TD]
[TD]30A[/TD]
[/TR]
[TR]
[TD]District 30B[/TD]
[TD]30B[/TD]
[/TR]
[TR]
[TD]District 31A[/TD]
[TD]31A[/TD]
[/TR]
[TR]
[TD]5th Judicial Circuit[/TD]
[TD]5th[/TD]
[/TR]
[TR]
[TD]5th Judicial Circuit[/TD]
[TD]5th[/TD]
[/TR]
[TR]
[TD]Legislative District 21[/TD]
[TD]21[/TD]
[/TR]
</tbody>[/TABLE]


The last is removing the "th" or "st" from the number values. These would show up as lowercase always.
 
Upvote 0
Assuming your numbers are in either the first or last "word" (as all your examples show), then this formula should do what you want...

=IF(ISNUMBER(-LEFT(A2)),""&-LOOKUP(0,-LEFT(A2,ROW(INDEX(A:A,1):INDEX(A:A,LEN(A2))))),TRIM(RIGHT(SUBSTITUTE(A2," ",REPT(" ",99)),99)))
 
Upvote 0
Rick and AlKey. Thank you both. I was making this much more difficult than what it needed to be.

Thanks again.
 
Upvote 0

Forum statistics

Threads
1,222,102
Messages
6,163,937
Members
451,866
Latest member
cradd64

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