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
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Maybe something like if 093427123456 is text, not 0,93427123456):
(test in a throw away worksheet, backup before doing any modification)
VBA Code:
Sub test()
Dim lRow As Long
With ActiveSheet
    For lRow = 1 To .UsedRange.Rows.Count
        .Cells(lRow, 7) = Mid(ActiveSheet.Cells(lRow, 7), 6, 5)
    Next lRow
End With
End Sub
 
Upvote 0
Solution
You can do that with a formula.
Excel Formula:
=Mid(G2,7,5)

If you need vba then
VBA Code:
sub t()
Dim i As Long
With ActiveSheet
    For i = 2 To .Cells(Rows.Count, "G").End(xlUp).Row
        Sheets(2).Cells(i, 1) = .Application.Mid(.Cells(i, 7),7,5)
    Next
End With
End Sub
 
Upvote 0
You can do that with a formula.
Excel Formula:
=Mid(G2,7,5)

If you need vba then
VBA Code:
sub t()
Dim i As Long
With ActiveSheet
    For i = 2 To .Cells(Rows.Count, "G").End(xlUp).Row
        Sheets(2).Cells(i, 1) = .Application.Mid(.Cells(i, 7),7,5)
    Next
End With
End Sub
Hi Eduzs, your code worked fine. JLGWhiz, I got a debug on your code...could it have something to do with the "Sheets(2)" part? Am I supposed to change that?

Thank you both for your quick replies!
 
Upvote 0
Hi Eduzs, your code worked fine. JLGWhiz, I got a debug on your code...could it have something to do with the "Sheets(2)" part? Am I supposed to change that?

Thank you both for your quick replies!
The sheets(2) is the destination for the results. The error was because there is a period in front of 'Application' that should not be there.
 
Upvote 0
Maybe something like if 093427123456 is text, not 0,93427123456):
(test in a throw away worksheet, backup before doing any modification)
VBA Code:
Sub test()
Dim lRow As Long
With ActiveSheet
    For lRow = 1 To .UsedRange.Rows.Count
        .Cells(lRow, 7) = Mid(ActiveSheet.Cells(lRow, 7), 6, 5)
    Next lRow
End With
End Sub
Hi Eduzs,

I noticed that the "usedrange' is not including the last couple of lines in the range and I'm not sure why. Is there an alternate way to find the last row? Or I can set a range to line 500 which I will never exceed. Thanks again. Steve
 
Upvote 0
Try (last used row column "G"):
VBA Code:
For lRow = 1 To .Cells(.Count, "G").End(xlUp).Row
 
Upvote 0
Try (last used row column "G"):
VBA Code:
For lRow = 1 To .Cells(.Count, "G").End(xlUp).Row
Still having trouble. Here is my current code - I had changed it to work on Column A instead of G, and to begin on row 5. Thanks

VBA Code:
Sub Trim_UPC()
Dim lRow As Long
With ActiveSheet
    'For lRow = 5 To .UsedRange.Rows.Count  
     For lRow = 1 To .Cells(.Count, "A").End(xlUp).Row
    .Cells(lRow, 1) = Mid(ActiveSheet.Cells(lRow, 1), 7, 5)
    Next lRow
End With
End Sub
 
Upvote 0
How about
VBA Code:
Sub Shadkng()
   With Range("A5", Range("A" & Rows.Count).End(xlUp))
      .Value = Evaluate("if({1},mid(" & .Address & ",7,5))")
   End With
End Sub
 
Upvote 0
Fluff, this works fine. One small glitch which happened on the previous versions as well. On the below sequence it's returning "3201" instead of "03201", I guess because of the "0".
93427103201
Thanks
 
Upvote 0

Forum statistics

Threads
1,215,402
Messages
6,124,708
Members
449,182
Latest member
mrlanc20

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