+ Reply to Thread
Results 1 to 8 of 8

Number count and sort macro

Hybrid View

  1. #1
    Valued Forum Contributor
    Join Date
    03-03-2009
    Location
    UK
    MS-Off Ver
    MS365 Subscription Excel for Mac
    Posts
    1,017

    Number count and sort macro

    In column A and column C for the first 10 rows only I have data in a cell followed by a number (possibly two numbers sometimes separated by a comma) in the same cell. I want a macro to tell me the lowest number that it finds and the word it was found beside. I have attached a sample workbook showing a before and after.

    samplesheet.xlsm

  2. #2
    Valued Forum Contributor
    Join Date
    03-03-2009
    Location
    UK
    MS-Off Ver
    MS365 Subscription Excel for Mac
    Posts
    1,017

    Re: Number count and sort macro

    Any ideas? Perhaps something with Text to Columns might help but I still haven't found a good way yet.

  3. #3
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Number count and sort macro

    Try this
    Sub test()
        Dim rng As Range, r As Range, n As Long, m As Object
        Dim temp As String, myStr As String
        Set rng = Columns("a:c").SpecialCells(2)
        If rng Is Nothing Then Exit Sub
        With CreateObject("VBScript.RegExp")
            .Global = True
            For Each r In rng
                .Pattern = "(\D+) (\d+)(.*)"
                If .test(r.Value) Then
                    myStr = .Replace(r.Value, "$1")
                    n = n + 1
                    Cells(n, "m").Resize(, 2).Value = _
                    Split(.Replace(r.Value, "$1 $2"))
                    temp = .Replace(r.Value, "$3")
                    If temp <> "" Then
                        .Pattern = "\d+"
                        For Each m In .Execute(temp)
                            n = n + 1
                            Cells(n, "m").Resize(, 2).Value = _
                            Array(myStr, m.Value)
                        Next
                    End If
                End If
            Next
        End With
        Range("n1").CurrentRegion.Value = Range("n1").CurrentRegion.Value
    End Sub

  4. #4
    Valued Forum Contributor
    Join Date
    03-03-2009
    Location
    UK
    MS-Off Ver
    MS365 Subscription Excel for Mac
    Posts
    1,017

    Re: Number count and sort macro

    Thanks Jindon. Works perfect if there is only one word. However at times the cell will be "Test Test 12" or a different number or perhaps "Test Test Test 12". Is there a way to deal with those possible differences in the macro? Thanks!

  5. #5
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Number count and sort macro

    Need to see variety of data and the desired result from there.

  6. #6
    Valued Forum Contributor
    Join Date
    03-03-2009
    Location
    UK
    MS-Off Ver
    MS365 Subscription Excel for Mac
    Posts
    1,017

    Re: Number count and sort macro

    Here you go.

    Thanks again!

    samplesheetNEW.xlsm

  7. #7
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Number count and sort macro

    OK
    Sub test()
        Dim rng As Range, r As Range, n As Long, m As Object
        Dim temp As String, myStr As String
        Set rng = Columns("a:c").SpecialCells(2)
        If rng Is Nothing Then Exit Sub
        With CreateObject("VBScript.RegExp")
            .Global = True
            For Each r In rng
                .Pattern = "(\D+\S) (\d+)(.*)"
                If .test(r.Value) Then
                    myStr = .Replace(r.Value, "$1")
                    n = n + 1
                    Cells(n, "m").Resize(, 2).Value = _
                    Split(.Replace(r.Value, "$1" & Chr(2) & "$2"), Chr(2))
                    temp = .Replace(r.Value, "$3")
                    If temp <> "" Then
                        .Pattern = "\d+"
                        For Each m In .Execute(temp)
                            n = n + 1
                            Cells(n, "m").Resize(, 2).Value = _
                            Array(myStr, m.Value)
                        Next
                    End If
                End If
            Next
        End With
        Range("n1").CurrentRegion.Value = Range("n1").CurrentRegion.Value
    End Sub

  8. #8
    Valued Forum Contributor
    Join Date
    03-03-2009
    Location
    UK
    MS-Off Ver
    MS365 Subscription Excel for Mac
    Posts
    1,017

    Re: Number count and sort macro

    Awesome! Thankks!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1