VBA to extract digits

Shadkng

Active Member
Joined
Oct 11, 2018
Messages
365
Hi, I have been searching to find a simple code to extract a set of digits from a numeric string. I would normally present some code but I haven't been able to formulate anything that is close to what I want.

My data will be always 12 digits (actually a UPC code). For example, 093427123456. I need to extract the 7th digit thru the 11th digit, so 12345. It would need to run through all the cells of one column, say col "G". The new data can replace the original data in the same cell.

Thanks for the help!

Steve
 
How about
VBA Code:
Sub Shadkng()
   With Range("A2", Range("A" & Rows.Count).End(xlUp))
      .Value = Evaluate("if({1},""'""&mid(" & .Address & ",7,5))")
   End With
End Sub
 
Upvote 0

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
How about
VBA Code:
Sub Shadkng()
   With Range("A2", Range("A" & Rows.Count).End(xlUp))
      .Value = Evaluate("if({1},""'""&mid(" & .Address & ",7,5))")
   End With
End Sub
This didn't work either. I formatted the column from "general" to "text" and ran the previous code and it worked fine. I need a line of code to format a column "text" if you can provide. Thanks
 
Upvote 0
How about
VBA Code:
Sub Shadkng()
   With Range("A2", Range("A" & Rows.Count).End(xlUp))
      .NumberFormat = "@"
      .Value = Evaluate("if({1},""'""&mid(" & .Address & ",7,5))")
   End With
End Sub
 
Upvote 0
How about
VBA Code:
Sub Shadkng()
   With Range("A2", Range("A" & Rows.Count).End(xlUp))
      .NumberFormat = "@"
      .Value = Evaluate("if({1},""'""&mid(" & .Address & ",7,5))")
   End With
End Sub
Thanks I'm good to go.
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,821
Messages
6,121,759
Members
449,048
Latest member
excelknuckles

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