how to pull exactly 6 continuous figures from string










0















Problem: how to pull exactly 6 continuous figures from string



Example:



f657674
576767g
tt454656y
465767yy
x1234567
1234567x
n645856g
s-5656-54654657657-6576-46567785-4354
pof-user-201734545435454
4545665
345678
f546576767g
rtryty


it should give



657674
576767
454656
465767
(blank value)
(blank value)
645856
(blank value)
(blank value)
(blank value)
345678
(blank value)
(blank value)


What I've tried: (A1 is the cell of the first string)



=IFERROR(LOOKUP(10^6;MID(A1;ROW(INDIRECT("1:"&LEN(A1)-5));6)+0);"")


Then I drag this formula for the other rows and it gives:



 657674 (right)
576767 (right)
454656 (right)
465767 (right)
(blank value) (right)
(blank value) (right)
645856 (right)
657457 (wrong)
435454 (wrong)
4545665 (wrong)
345678 (right)
546576767 (wrong)
(blank value) (right)


Version: Excel 2016










share|improve this question



















  • 1





    You may be better off here with VBA

    – urdearboy
    Nov 15 '18 at 19:17











  • unfortunately I do not know VBA.

    – graphene
    Nov 15 '18 at 19:18






  • 1





    So, you only want six numbers returned, where there's a letter on either side?

    – BruceWayne
    Nov 15 '18 at 19:19











  • yes, I want to pull only six CONTINUOUS numbers. so 565dft676 should give a blank value. But st645594uy should give 645594

    – graphene
    Nov 15 '18 at 19:22















0















Problem: how to pull exactly 6 continuous figures from string



Example:



f657674
576767g
tt454656y
465767yy
x1234567
1234567x
n645856g
s-5656-54654657657-6576-46567785-4354
pof-user-201734545435454
4545665
345678
f546576767g
rtryty


it should give



657674
576767
454656
465767
(blank value)
(blank value)
645856
(blank value)
(blank value)
(blank value)
345678
(blank value)
(blank value)


What I've tried: (A1 is the cell of the first string)



=IFERROR(LOOKUP(10^6;MID(A1;ROW(INDIRECT("1:"&LEN(A1)-5));6)+0);"")


Then I drag this formula for the other rows and it gives:



 657674 (right)
576767 (right)
454656 (right)
465767 (right)
(blank value) (right)
(blank value) (right)
645856 (right)
657457 (wrong)
435454 (wrong)
4545665 (wrong)
345678 (right)
546576767 (wrong)
(blank value) (right)


Version: Excel 2016










share|improve this question



















  • 1





    You may be better off here with VBA

    – urdearboy
    Nov 15 '18 at 19:17











  • unfortunately I do not know VBA.

    – graphene
    Nov 15 '18 at 19:18






  • 1





    So, you only want six numbers returned, where there's a letter on either side?

    – BruceWayne
    Nov 15 '18 at 19:19











  • yes, I want to pull only six CONTINUOUS numbers. so 565dft676 should give a blank value. But st645594uy should give 645594

    – graphene
    Nov 15 '18 at 19:22













0












0








0








Problem: how to pull exactly 6 continuous figures from string



Example:



f657674
576767g
tt454656y
465767yy
x1234567
1234567x
n645856g
s-5656-54654657657-6576-46567785-4354
pof-user-201734545435454
4545665
345678
f546576767g
rtryty


it should give



657674
576767
454656
465767
(blank value)
(blank value)
645856
(blank value)
(blank value)
(blank value)
345678
(blank value)
(blank value)


What I've tried: (A1 is the cell of the first string)



=IFERROR(LOOKUP(10^6;MID(A1;ROW(INDIRECT("1:"&LEN(A1)-5));6)+0);"")


Then I drag this formula for the other rows and it gives:



 657674 (right)
576767 (right)
454656 (right)
465767 (right)
(blank value) (right)
(blank value) (right)
645856 (right)
657457 (wrong)
435454 (wrong)
4545665 (wrong)
345678 (right)
546576767 (wrong)
(blank value) (right)


Version: Excel 2016










share|improve this question
















Problem: how to pull exactly 6 continuous figures from string



Example:



f657674
576767g
tt454656y
465767yy
x1234567
1234567x
n645856g
s-5656-54654657657-6576-46567785-4354
pof-user-201734545435454
4545665
345678
f546576767g
rtryty


it should give



657674
576767
454656
465767
(blank value)
(blank value)
645856
(blank value)
(blank value)
(blank value)
345678
(blank value)
(blank value)


What I've tried: (A1 is the cell of the first string)



=IFERROR(LOOKUP(10^6;MID(A1;ROW(INDIRECT("1:"&LEN(A1)-5));6)+0);"")


Then I drag this formula for the other rows and it gives:



 657674 (right)
576767 (right)
454656 (right)
465767 (right)
(blank value) (right)
(blank value) (right)
645856 (right)
657457 (wrong)
435454 (wrong)
4545665 (wrong)
345678 (right)
546576767 (wrong)
(blank value) (right)


Version: Excel 2016







excel






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 16 '18 at 2:04









chris neilsen

40.3k86096




40.3k86096










asked Nov 15 '18 at 19:08









graphenegraphene

676




676







  • 1





    You may be better off here with VBA

    – urdearboy
    Nov 15 '18 at 19:17











  • unfortunately I do not know VBA.

    – graphene
    Nov 15 '18 at 19:18






  • 1





    So, you only want six numbers returned, where there's a letter on either side?

    – BruceWayne
    Nov 15 '18 at 19:19











  • yes, I want to pull only six CONTINUOUS numbers. so 565dft676 should give a blank value. But st645594uy should give 645594

    – graphene
    Nov 15 '18 at 19:22












  • 1





    You may be better off here with VBA

    – urdearboy
    Nov 15 '18 at 19:17











  • unfortunately I do not know VBA.

    – graphene
    Nov 15 '18 at 19:18






  • 1





    So, you only want six numbers returned, where there's a letter on either side?

    – BruceWayne
    Nov 15 '18 at 19:19











  • yes, I want to pull only six CONTINUOUS numbers. so 565dft676 should give a blank value. But st645594uy should give 645594

    – graphene
    Nov 15 '18 at 19:22







1




1





You may be better off here with VBA

– urdearboy
Nov 15 '18 at 19:17





You may be better off here with VBA

– urdearboy
Nov 15 '18 at 19:17













unfortunately I do not know VBA.

– graphene
Nov 15 '18 at 19:18





unfortunately I do not know VBA.

– graphene
Nov 15 '18 at 19:18




1




1





So, you only want six numbers returned, where there's a letter on either side?

– BruceWayne
Nov 15 '18 at 19:19





So, you only want six numbers returned, where there's a letter on either side?

– BruceWayne
Nov 15 '18 at 19:19













yes, I want to pull only six CONTINUOUS numbers. so 565dft676 should give a blank value. But st645594uy should give 645594

– graphene
Nov 15 '18 at 19:22





yes, I want to pull only six CONTINUOUS numbers. so 565dft676 should give a blank value. But st645594uy should give 645594

– graphene
Nov 15 '18 at 19:22












3 Answers
3






active

oldest

votes


















2














Inspired by Bruce, but paired down to the minimum



Function ContainsSix(ByVal rng As Range) As String
Dim re As RegExp
Dim mc As MatchCollection
Dim CellValue As Variant

CellValue = rng.Cells(1, 1).Value2
Set re = New RegExp
With re
.Pattern = "(?:D|^)(d6)(?:D|$)"
.Global = True
.MultiLine = True
.IgnoreCase = True

If .Test(CellValue) Then
Set mc = .Execute(CellValue)
ContainsSix = mc(0).SubMatches(0)
End If
End With
Set re = Nothing
End Function


A description of the regular expression:



  • Match expression but don't capture it. [D|^]

    • Select from 2 alternatives

      • Any character that is not a digit

      • Beginning of line or string



  • A numbered capture group. [d6]

    • Any digit, exactly 6 repetitions


  • Match expression but don't capture it. [D|$]

    • Select from 2 alternatives

      • Any character that is not a digit

      • End of line or string







share|improve this answer
































    2














    You will likely want a UDF instead of a built-in function. This should work, but likely needs tweaking. Your example in your OP returns 345678 as a correct return, but there are no letters on either side. Therefore, I assume you want either: 6 consecutive numbers on their own, or 6 consecutive numbers with a letter on both sides.



    Add this to the workbook module, and you can call like =return_numbers(A1). You may or may not have to add the RegEx Reference to VBEditor.



    Function return_numbers(ByVal cel As Range) As String
    Dim strPattern As String
    Dim regEx As New RegExp

    strPattern = "[a-z]d6[a-z]"

    With regEx
    .Global = True
    .MultiLine = True
    .IgnoreCase = True
    .Pattern = strPattern
    End With

    Dim matches As Object
    Set matches = regEx.Execute(cel)

    If Len(cel) = 6 And IsNumeric(cel) Then
    return_numbers = cel.Value
    Set regEx = Nothing
    Exit Function
    End If

    If matches.Count <> 0 Then
    return_numbers = Mid(matches.Item(0), 2, Len(matches.Item(0)) - 2)
    ElseIf matches.Count = 0 Then
    strPattern = "[a-z]1d6$"
    regEx.Pattern = strPattern
    Set matches = regEx.Execute(cel)
    If matches.Count <> 0 Then
    return_numbers = Mid(matches.Item(0), 2, Len(matches.Item(0)) - 1)
    ElseIf matches.Count = 0 Then
    strPattern = "^d6[a-z]1"
    regEx.Pattern = strPattern
    Set matches = regEx.Execute(cel)
    If matches.Count <> 0 Then
    return_numbers = Mid(matches.Item(0), 1, Len(matches.Item(0)) - 1)
    End If
    End If
    End If

    Set regEx = Nothing

    End Function


    enter image description here



    If you're wanting to speed this up, I think if you switch the If/else statements, it might run a little quicker since it won't always run the Regex, if it finds 6 lonely digits



    Edit: This is rather clunky. I'm sure there's a better regex pattern, so please let me know.






    share|improve this answer

























    • "Therefore, I assume you want either: 6 consecutive numbers on their own, or 6 consecutive numbers with a letter on both sides." OR it can happen one letter in one of the sides, like f565679 it should give 565679. Or 454656g it should give 454656.

      – graphene
      Nov 15 '18 at 19:44






    • 1





      @graphene - What should it return for x1234567? or 1234567x?

      – BruceWayne
      Nov 15 '18 at 19:45











    • x1234567? or 1234567x? In both cases, a blank value.

      – graphene
      Nov 15 '18 at 19:46











    • @BruceWayne - The following strPattern: (?<!d)(d6)(?!d) would allow you to avoid the isnumberic test alltogether

      – cybernetic.nomad
      Nov 15 '18 at 19:55











    • it throws an error: user-defined type not defined... and points out to Dim regEx As New RegExp. I suppose I need to add the regfex refernce..

      – graphene
      Nov 15 '18 at 19:56



















    1














    =IFERROR(0+MID(A1,MATCH(8,MMULT(ABS(ISNUMBER(0+MID(MID("ζ"&A1&"ζ",ROW(INDEX(A:A,1):INDEX(A:A,LEN(A1)-5)),8),1,2,3,4,5,6,7,8,1))-1,0,0,0,0,0,0,1),1;1;1;1;1;1;1;1),0),6),"")



    Note that if you're not using an English-language-version of Excel then parts of the above may need amending.



    Regards






    share|improve this answer






















      Your Answer






      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "1"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













      draft saved

      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53326376%2fhow-to-pull-exactly-6-continuous-figures-from-string%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      2














      Inspired by Bruce, but paired down to the minimum



      Function ContainsSix(ByVal rng As Range) As String
      Dim re As RegExp
      Dim mc As MatchCollection
      Dim CellValue As Variant

      CellValue = rng.Cells(1, 1).Value2
      Set re = New RegExp
      With re
      .Pattern = "(?:D|^)(d6)(?:D|$)"
      .Global = True
      .MultiLine = True
      .IgnoreCase = True

      If .Test(CellValue) Then
      Set mc = .Execute(CellValue)
      ContainsSix = mc(0).SubMatches(0)
      End If
      End With
      Set re = Nothing
      End Function


      A description of the regular expression:



      • Match expression but don't capture it. [D|^]

        • Select from 2 alternatives

          • Any character that is not a digit

          • Beginning of line or string



      • A numbered capture group. [d6]

        • Any digit, exactly 6 repetitions


      • Match expression but don't capture it. [D|$]

        • Select from 2 alternatives

          • Any character that is not a digit

          • End of line or string







      share|improve this answer





























        2














        Inspired by Bruce, but paired down to the minimum



        Function ContainsSix(ByVal rng As Range) As String
        Dim re As RegExp
        Dim mc As MatchCollection
        Dim CellValue As Variant

        CellValue = rng.Cells(1, 1).Value2
        Set re = New RegExp
        With re
        .Pattern = "(?:D|^)(d6)(?:D|$)"
        .Global = True
        .MultiLine = True
        .IgnoreCase = True

        If .Test(CellValue) Then
        Set mc = .Execute(CellValue)
        ContainsSix = mc(0).SubMatches(0)
        End If
        End With
        Set re = Nothing
        End Function


        A description of the regular expression:



        • Match expression but don't capture it. [D|^]

          • Select from 2 alternatives

            • Any character that is not a digit

            • Beginning of line or string



        • A numbered capture group. [d6]

          • Any digit, exactly 6 repetitions


        • Match expression but don't capture it. [D|$]

          • Select from 2 alternatives

            • Any character that is not a digit

            • End of line or string







        share|improve this answer



























          2












          2








          2







          Inspired by Bruce, but paired down to the minimum



          Function ContainsSix(ByVal rng As Range) As String
          Dim re As RegExp
          Dim mc As MatchCollection
          Dim CellValue As Variant

          CellValue = rng.Cells(1, 1).Value2
          Set re = New RegExp
          With re
          .Pattern = "(?:D|^)(d6)(?:D|$)"
          .Global = True
          .MultiLine = True
          .IgnoreCase = True

          If .Test(CellValue) Then
          Set mc = .Execute(CellValue)
          ContainsSix = mc(0).SubMatches(0)
          End If
          End With
          Set re = Nothing
          End Function


          A description of the regular expression:



          • Match expression but don't capture it. [D|^]

            • Select from 2 alternatives

              • Any character that is not a digit

              • Beginning of line or string



          • A numbered capture group. [d6]

            • Any digit, exactly 6 repetitions


          • Match expression but don't capture it. [D|$]

            • Select from 2 alternatives

              • Any character that is not a digit

              • End of line or string







          share|improve this answer















          Inspired by Bruce, but paired down to the minimum



          Function ContainsSix(ByVal rng As Range) As String
          Dim re As RegExp
          Dim mc As MatchCollection
          Dim CellValue As Variant

          CellValue = rng.Cells(1, 1).Value2
          Set re = New RegExp
          With re
          .Pattern = "(?:D|^)(d6)(?:D|$)"
          .Global = True
          .MultiLine = True
          .IgnoreCase = True

          If .Test(CellValue) Then
          Set mc = .Execute(CellValue)
          ContainsSix = mc(0).SubMatches(0)
          End If
          End With
          Set re = Nothing
          End Function


          A description of the regular expression:



          • Match expression but don't capture it. [D|^]

            • Select from 2 alternatives

              • Any character that is not a digit

              • Beginning of line or string



          • A numbered capture group. [d6]

            • Any digit, exactly 6 repetitions


          • Match expression but don't capture it. [D|$]

            • Select from 2 alternatives

              • Any character that is not a digit

              • End of line or string








          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 15 '18 at 21:24

























          answered Nov 15 '18 at 21:13









          chris neilsenchris neilsen

          40.3k86096




          40.3k86096























              2














              You will likely want a UDF instead of a built-in function. This should work, but likely needs tweaking. Your example in your OP returns 345678 as a correct return, but there are no letters on either side. Therefore, I assume you want either: 6 consecutive numbers on their own, or 6 consecutive numbers with a letter on both sides.



              Add this to the workbook module, and you can call like =return_numbers(A1). You may or may not have to add the RegEx Reference to VBEditor.



              Function return_numbers(ByVal cel As Range) As String
              Dim strPattern As String
              Dim regEx As New RegExp

              strPattern = "[a-z]d6[a-z]"

              With regEx
              .Global = True
              .MultiLine = True
              .IgnoreCase = True
              .Pattern = strPattern
              End With

              Dim matches As Object
              Set matches = regEx.Execute(cel)

              If Len(cel) = 6 And IsNumeric(cel) Then
              return_numbers = cel.Value
              Set regEx = Nothing
              Exit Function
              End If

              If matches.Count <> 0 Then
              return_numbers = Mid(matches.Item(0), 2, Len(matches.Item(0)) - 2)
              ElseIf matches.Count = 0 Then
              strPattern = "[a-z]1d6$"
              regEx.Pattern = strPattern
              Set matches = regEx.Execute(cel)
              If matches.Count <> 0 Then
              return_numbers = Mid(matches.Item(0), 2, Len(matches.Item(0)) - 1)
              ElseIf matches.Count = 0 Then
              strPattern = "^d6[a-z]1"
              regEx.Pattern = strPattern
              Set matches = regEx.Execute(cel)
              If matches.Count <> 0 Then
              return_numbers = Mid(matches.Item(0), 1, Len(matches.Item(0)) - 1)
              End If
              End If
              End If

              Set regEx = Nothing

              End Function


              enter image description here



              If you're wanting to speed this up, I think if you switch the If/else statements, it might run a little quicker since it won't always run the Regex, if it finds 6 lonely digits



              Edit: This is rather clunky. I'm sure there's a better regex pattern, so please let me know.






              share|improve this answer

























              • "Therefore, I assume you want either: 6 consecutive numbers on their own, or 6 consecutive numbers with a letter on both sides." OR it can happen one letter in one of the sides, like f565679 it should give 565679. Or 454656g it should give 454656.

                – graphene
                Nov 15 '18 at 19:44






              • 1





                @graphene - What should it return for x1234567? or 1234567x?

                – BruceWayne
                Nov 15 '18 at 19:45











              • x1234567? or 1234567x? In both cases, a blank value.

                – graphene
                Nov 15 '18 at 19:46











              • @BruceWayne - The following strPattern: (?<!d)(d6)(?!d) would allow you to avoid the isnumberic test alltogether

                – cybernetic.nomad
                Nov 15 '18 at 19:55











              • it throws an error: user-defined type not defined... and points out to Dim regEx As New RegExp. I suppose I need to add the regfex refernce..

                – graphene
                Nov 15 '18 at 19:56
















              2














              You will likely want a UDF instead of a built-in function. This should work, but likely needs tweaking. Your example in your OP returns 345678 as a correct return, but there are no letters on either side. Therefore, I assume you want either: 6 consecutive numbers on their own, or 6 consecutive numbers with a letter on both sides.



              Add this to the workbook module, and you can call like =return_numbers(A1). You may or may not have to add the RegEx Reference to VBEditor.



              Function return_numbers(ByVal cel As Range) As String
              Dim strPattern As String
              Dim regEx As New RegExp

              strPattern = "[a-z]d6[a-z]"

              With regEx
              .Global = True
              .MultiLine = True
              .IgnoreCase = True
              .Pattern = strPattern
              End With

              Dim matches As Object
              Set matches = regEx.Execute(cel)

              If Len(cel) = 6 And IsNumeric(cel) Then
              return_numbers = cel.Value
              Set regEx = Nothing
              Exit Function
              End If

              If matches.Count <> 0 Then
              return_numbers = Mid(matches.Item(0), 2, Len(matches.Item(0)) - 2)
              ElseIf matches.Count = 0 Then
              strPattern = "[a-z]1d6$"
              regEx.Pattern = strPattern
              Set matches = regEx.Execute(cel)
              If matches.Count <> 0 Then
              return_numbers = Mid(matches.Item(0), 2, Len(matches.Item(0)) - 1)
              ElseIf matches.Count = 0 Then
              strPattern = "^d6[a-z]1"
              regEx.Pattern = strPattern
              Set matches = regEx.Execute(cel)
              If matches.Count <> 0 Then
              return_numbers = Mid(matches.Item(0), 1, Len(matches.Item(0)) - 1)
              End If
              End If
              End If

              Set regEx = Nothing

              End Function


              enter image description here



              If you're wanting to speed this up, I think if you switch the If/else statements, it might run a little quicker since it won't always run the Regex, if it finds 6 lonely digits



              Edit: This is rather clunky. I'm sure there's a better regex pattern, so please let me know.






              share|improve this answer

























              • "Therefore, I assume you want either: 6 consecutive numbers on their own, or 6 consecutive numbers with a letter on both sides." OR it can happen one letter in one of the sides, like f565679 it should give 565679. Or 454656g it should give 454656.

                – graphene
                Nov 15 '18 at 19:44






              • 1





                @graphene - What should it return for x1234567? or 1234567x?

                – BruceWayne
                Nov 15 '18 at 19:45











              • x1234567? or 1234567x? In both cases, a blank value.

                – graphene
                Nov 15 '18 at 19:46











              • @BruceWayne - The following strPattern: (?<!d)(d6)(?!d) would allow you to avoid the isnumberic test alltogether

                – cybernetic.nomad
                Nov 15 '18 at 19:55











              • it throws an error: user-defined type not defined... and points out to Dim regEx As New RegExp. I suppose I need to add the regfex refernce..

                – graphene
                Nov 15 '18 at 19:56














              2












              2








              2







              You will likely want a UDF instead of a built-in function. This should work, but likely needs tweaking. Your example in your OP returns 345678 as a correct return, but there are no letters on either side. Therefore, I assume you want either: 6 consecutive numbers on their own, or 6 consecutive numbers with a letter on both sides.



              Add this to the workbook module, and you can call like =return_numbers(A1). You may or may not have to add the RegEx Reference to VBEditor.



              Function return_numbers(ByVal cel As Range) As String
              Dim strPattern As String
              Dim regEx As New RegExp

              strPattern = "[a-z]d6[a-z]"

              With regEx
              .Global = True
              .MultiLine = True
              .IgnoreCase = True
              .Pattern = strPattern
              End With

              Dim matches As Object
              Set matches = regEx.Execute(cel)

              If Len(cel) = 6 And IsNumeric(cel) Then
              return_numbers = cel.Value
              Set regEx = Nothing
              Exit Function
              End If

              If matches.Count <> 0 Then
              return_numbers = Mid(matches.Item(0), 2, Len(matches.Item(0)) - 2)
              ElseIf matches.Count = 0 Then
              strPattern = "[a-z]1d6$"
              regEx.Pattern = strPattern
              Set matches = regEx.Execute(cel)
              If matches.Count <> 0 Then
              return_numbers = Mid(matches.Item(0), 2, Len(matches.Item(0)) - 1)
              ElseIf matches.Count = 0 Then
              strPattern = "^d6[a-z]1"
              regEx.Pattern = strPattern
              Set matches = regEx.Execute(cel)
              If matches.Count <> 0 Then
              return_numbers = Mid(matches.Item(0), 1, Len(matches.Item(0)) - 1)
              End If
              End If
              End If

              Set regEx = Nothing

              End Function


              enter image description here



              If you're wanting to speed this up, I think if you switch the If/else statements, it might run a little quicker since it won't always run the Regex, if it finds 6 lonely digits



              Edit: This is rather clunky. I'm sure there's a better regex pattern, so please let me know.






              share|improve this answer















              You will likely want a UDF instead of a built-in function. This should work, but likely needs tweaking. Your example in your OP returns 345678 as a correct return, but there are no letters on either side. Therefore, I assume you want either: 6 consecutive numbers on their own, or 6 consecutive numbers with a letter on both sides.



              Add this to the workbook module, and you can call like =return_numbers(A1). You may or may not have to add the RegEx Reference to VBEditor.



              Function return_numbers(ByVal cel As Range) As String
              Dim strPattern As String
              Dim regEx As New RegExp

              strPattern = "[a-z]d6[a-z]"

              With regEx
              .Global = True
              .MultiLine = True
              .IgnoreCase = True
              .Pattern = strPattern
              End With

              Dim matches As Object
              Set matches = regEx.Execute(cel)

              If Len(cel) = 6 And IsNumeric(cel) Then
              return_numbers = cel.Value
              Set regEx = Nothing
              Exit Function
              End If

              If matches.Count <> 0 Then
              return_numbers = Mid(matches.Item(0), 2, Len(matches.Item(0)) - 2)
              ElseIf matches.Count = 0 Then
              strPattern = "[a-z]1d6$"
              regEx.Pattern = strPattern
              Set matches = regEx.Execute(cel)
              If matches.Count <> 0 Then
              return_numbers = Mid(matches.Item(0), 2, Len(matches.Item(0)) - 1)
              ElseIf matches.Count = 0 Then
              strPattern = "^d6[a-z]1"
              regEx.Pattern = strPattern
              Set matches = regEx.Execute(cel)
              If matches.Count <> 0 Then
              return_numbers = Mid(matches.Item(0), 1, Len(matches.Item(0)) - 1)
              End If
              End If
              End If

              Set regEx = Nothing

              End Function


              enter image description here



              If you're wanting to speed this up, I think if you switch the If/else statements, it might run a little quicker since it won't always run the Regex, if it finds 6 lonely digits



              Edit: This is rather clunky. I'm sure there's a better regex pattern, so please let me know.







              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Nov 15 '18 at 20:55

























              answered Nov 15 '18 at 19:37









              BruceWayneBruceWayne

              18k113363




              18k113363












              • "Therefore, I assume you want either: 6 consecutive numbers on their own, or 6 consecutive numbers with a letter on both sides." OR it can happen one letter in one of the sides, like f565679 it should give 565679. Or 454656g it should give 454656.

                – graphene
                Nov 15 '18 at 19:44






              • 1





                @graphene - What should it return for x1234567? or 1234567x?

                – BruceWayne
                Nov 15 '18 at 19:45











              • x1234567? or 1234567x? In both cases, a blank value.

                – graphene
                Nov 15 '18 at 19:46











              • @BruceWayne - The following strPattern: (?<!d)(d6)(?!d) would allow you to avoid the isnumberic test alltogether

                – cybernetic.nomad
                Nov 15 '18 at 19:55











              • it throws an error: user-defined type not defined... and points out to Dim regEx As New RegExp. I suppose I need to add the regfex refernce..

                – graphene
                Nov 15 '18 at 19:56


















              • "Therefore, I assume you want either: 6 consecutive numbers on their own, or 6 consecutive numbers with a letter on both sides." OR it can happen one letter in one of the sides, like f565679 it should give 565679. Or 454656g it should give 454656.

                – graphene
                Nov 15 '18 at 19:44






              • 1





                @graphene - What should it return for x1234567? or 1234567x?

                – BruceWayne
                Nov 15 '18 at 19:45











              • x1234567? or 1234567x? In both cases, a blank value.

                – graphene
                Nov 15 '18 at 19:46











              • @BruceWayne - The following strPattern: (?<!d)(d6)(?!d) would allow you to avoid the isnumberic test alltogether

                – cybernetic.nomad
                Nov 15 '18 at 19:55











              • it throws an error: user-defined type not defined... and points out to Dim regEx As New RegExp. I suppose I need to add the regfex refernce..

                – graphene
                Nov 15 '18 at 19:56

















              "Therefore, I assume you want either: 6 consecutive numbers on their own, or 6 consecutive numbers with a letter on both sides." OR it can happen one letter in one of the sides, like f565679 it should give 565679. Or 454656g it should give 454656.

              – graphene
              Nov 15 '18 at 19:44





              "Therefore, I assume you want either: 6 consecutive numbers on their own, or 6 consecutive numbers with a letter on both sides." OR it can happen one letter in one of the sides, like f565679 it should give 565679. Or 454656g it should give 454656.

              – graphene
              Nov 15 '18 at 19:44




              1




              1





              @graphene - What should it return for x1234567? or 1234567x?

              – BruceWayne
              Nov 15 '18 at 19:45





              @graphene - What should it return for x1234567? or 1234567x?

              – BruceWayne
              Nov 15 '18 at 19:45













              x1234567? or 1234567x? In both cases, a blank value.

              – graphene
              Nov 15 '18 at 19:46





              x1234567? or 1234567x? In both cases, a blank value.

              – graphene
              Nov 15 '18 at 19:46













              @BruceWayne - The following strPattern: (?<!d)(d6)(?!d) would allow you to avoid the isnumberic test alltogether

              – cybernetic.nomad
              Nov 15 '18 at 19:55





              @BruceWayne - The following strPattern: (?<!d)(d6)(?!d) would allow you to avoid the isnumberic test alltogether

              – cybernetic.nomad
              Nov 15 '18 at 19:55













              it throws an error: user-defined type not defined... and points out to Dim regEx As New RegExp. I suppose I need to add the regfex refernce..

              – graphene
              Nov 15 '18 at 19:56






              it throws an error: user-defined type not defined... and points out to Dim regEx As New RegExp. I suppose I need to add the regfex refernce..

              – graphene
              Nov 15 '18 at 19:56












              1














              =IFERROR(0+MID(A1,MATCH(8,MMULT(ABS(ISNUMBER(0+MID(MID("ζ"&A1&"ζ",ROW(INDEX(A:A,1):INDEX(A:A,LEN(A1)-5)),8),1,2,3,4,5,6,7,8,1))-1,0,0,0,0,0,0,1),1;1;1;1;1;1;1;1),0),6),"")



              Note that if you're not using an English-language-version of Excel then parts of the above may need amending.



              Regards






              share|improve this answer



























                1














                =IFERROR(0+MID(A1,MATCH(8,MMULT(ABS(ISNUMBER(0+MID(MID("ζ"&A1&"ζ",ROW(INDEX(A:A,1):INDEX(A:A,LEN(A1)-5)),8),1,2,3,4,5,6,7,8,1))-1,0,0,0,0,0,0,1),1;1;1;1;1;1;1;1),0),6),"")



                Note that if you're not using an English-language-version of Excel then parts of the above may need amending.



                Regards






                share|improve this answer

























                  1












                  1








                  1







                  =IFERROR(0+MID(A1,MATCH(8,MMULT(ABS(ISNUMBER(0+MID(MID("ζ"&A1&"ζ",ROW(INDEX(A:A,1):INDEX(A:A,LEN(A1)-5)),8),1,2,3,4,5,6,7,8,1))-1,0,0,0,0,0,0,1),1;1;1;1;1;1;1;1),0),6),"")



                  Note that if you're not using an English-language-version of Excel then parts of the above may need amending.



                  Regards






                  share|improve this answer













                  =IFERROR(0+MID(A1,MATCH(8,MMULT(ABS(ISNUMBER(0+MID(MID("ζ"&A1&"ζ",ROW(INDEX(A:A,1):INDEX(A:A,LEN(A1)-5)),8),1,2,3,4,5,6,7,8,1))-1,0,0,0,0,0,0,1),1;1;1;1;1;1;1;1),0),6),"")



                  Note that if you're not using an English-language-version of Excel then parts of the above may need amending.



                  Regards







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 15 '18 at 22:18









                  XOR LXXOR LX

                  7,17211013




                  7,17211013



























                      draft saved

                      draft discarded
















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53326376%2fhow-to-pull-exactly-6-continuous-figures-from-string%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      這個網誌中的熱門文章

                      What does pagestruct do in Eviews?

                      Dutch intervention in Lombok and Karangasem

                      Channel Islands