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
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Would you submit few examples of your data, please
 
Upvote 0
DataResult
sfwge5012256827
5012256827.xlsx​
dfhfjdsfs5018563057hhlhlll
5018563057.xlsx​
5017456295kbbgghjgjfj
5017456295.xlsx​
fgkkgk5017783572fkfkkj
5017783572.xlsx​
yfkgk5016456785gkkglhl50264974526
5016456785.xlsx​

But the result shown in B Column will override A Column
 
Last edited:
Upvote 0
Is it possible for the number 501 to appear more than once within a single cell?
 
Upvote 0
Is it possible for the number 501 to appear more than once within a single cell?
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
 
Upvote 0
Try
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)
       a(i, 1) = .Execute(a(i, 1))(0)
        Next
    End With
    Cells(1, 1).Resize(UBound(a)) = a
End Sub
 
Upvote 0
Its not working. Error No. 5 - Invalid procedure call or argument
Try
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)
       a(i, 1) = .Execute(a(i, 1))(0)
        Next
    End With
    Cells(1, 1).Resize(UBound(a)) = a
End Sub
 
Upvote 0
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
 
Upvote 0
Status
Not open for further replies.

Forum statistics

Threads
1,215,043
Messages
6,122,812
Members
449,095
Latest member
m_smith_solihull

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