Macro to extract 10 digit number from cell starting with 501

Status
Not open for further replies.

amandeep08

Board Regular
Joined
Mar 20, 2011
Messages
130
Office Version
  1. 365
Hi All,

I want a Macro that will extract 10 digit number from cell which contains number & alphabets and the number should start from 501. The macro will override that cell with the number and .xlsx

Can someone pl help me
 
IN othe case
VBA Code:
Sub test()
    Dim a As Variant
    a = Cells(1).CurrentRegion
    With CreateObject("VBScript.RegExp")
        .Global = True
        .Pattern = "[5][0][1]+\d+"
        For i = 1 To UBound(a)
            Set m = .Execute(a(i, 1))
            If m.Count = 1 Then
                a(i, 1) = m(0)
            Else
                For ii = 0 To m.Count - 1
                    tmp = IIf(tmp = "", "", tmp) & "," & m(ii)
                Next
                a(i, 1) = tmp
                tmp = ""
            End If
        Next
    End With
    Cells(1, 1).Resize(UBound(a)) = a
End Sub
Yes, this is working but can we have .xlsx also after the number
 
Upvote 0

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Replace with
VBA Code:
   tmp = tmp & IIf(tmp = "", "", ",") & m(ii) & ".xlsx"
 
Upvote 0
99% cases Number starting with 501 is not possible. If else if it is present and the total digits are 10, in that case, result should be 1st number.xlsx, 2nd number.xlsx
The number should be separated with comma but it will be in a single cell
Give this macro a try (it will handle the multiple 501's should there be any)...
VBA Code:
Sub Get501s()
  Dim R As Long, LastRow As Long, XLXS As String, V As Variant, Arr As Variant
  LastRow = Cells(Rows.Count, "A").End(xlUp).Row
  For R = 1 To LastRow
    Arr = Split(Cells(R, "A").Value & " ", 501)
    XLXS = ""
    For Each V In Arr
      If V Like "#######[!0-9]*" Then XLXS = XLXS & ", 501" & Left(V, 7) & ".xlxs"
    Next
    Cells(R, "B").Value = Mid(XLXS, 3)
  Next
End Sub
 
Upvote 0
T
Give this macro a try (it will handle the multiple 501's should there be any)...
VBA Code:
Sub Get501s()
  Dim R As Long, LastRow As Long, XLXS As String, V As Variant, Arr As Variant
  LastRow = Cells(Rows.Count, "A").End(xlUp).Row
  For R = 1 To LastRow
    Arr = Split(Cells(R, "A").Value & " ", 501)
    XLXS = ""
    For Each V In Arr
      If V Like "#######[!0-9]*" Then XLXS = XLXS & ", 501" & Left(V, 7) & ".xlxs"
    Next
    Cells(R, "B").Value = Mid(XLXS, 3)
  Next
End Sub
This is also working but it is taking data to next column instead of replacing the same column
 
Upvote 0
This is also working but it is taking data to next column instead of replacing the same column
Replace
Cells(R, "A").Value = Mid(XLXS, 3)
 
Upvote 0
This is also working but it is taking data to next column instead of replacing the same column
Whoops! I missed that you wanted the values to replace the existing values. The fix is simple, we just need to change the last line inside the outer For loop. mohadin post the correction; here is the complete code...
Rich (BB code):
Sub Get501s()
  Dim R As Long, LastRow As Long, XLXS As String, V As Variant, Arr As Variant
  LastRow = Cells(Rows.Count, "A").End(xlUp).Row
  For R = 1 To LastRow
    Arr = Split(Cells(R, "A").Value & " ", 501)
    XLXS = ""
    For Each V In Arr
      If V Like "#######[!0-9]*" Then XLXS = XLXS & ", 501" & Left(V, 7) & ".xlxs"
    Next
    Cells(R, "A").Value = Mid(XLXS, 3)
  Next
End Sub
 
Upvote 0
You are very welcome
And thank you for the feedback
Be happy
 
Upvote 0
@Rick Rothstein could you give a variant for this

Hi,
I'm looking for an Excel that is able to extract all consecutive 5-digit numbers from each cell, separating the results with a comma.
Note while extracting, exclude numbers less than 5 digits, exclude numbers more than 5 digits
 

Attachments

  • prs 2021-02-16_8-41-24.jpg
    prs 2021-02-16_8-41-24.jpg
    125.9 KB · Views: 8
Upvote 0
@Rick Rothstein could you give a variant for this

Hi,
I'm looking for an Excel that is able to extract all consecutive 5-digit numbers from each cell, separating the results with a comma.
Note while extracting, exclude numbers less than 5 digits, exclude numbers more than 5 digits
Give this UDF a try...
VBA Code:
Function FiveDigits(ByVal S As String) As String
  Dim X As Long, Arr As Variant
  For X = 1 To Len(S)
    If Mid(S, X, 1) Like "[!0-9]" Then Mid(S, X) = " "
  Next
  Arr = Split(Application.Trim(S))
  For X = 0 To UBound(Arr)
    If Len(Arr(X)) <> 5 Then Arr(X) = ""
  Next
  FiveDigits = Replace(Application.Trim(Join(Arr)), " ", ", ")
End Function
 
Upvote 0
Status
Not open for further replies.

Forum statistics

Threads
1,215,219
Messages
6,123,689
Members
449,117
Latest member
Aaagu

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