+ Reply to Thread
Results 1 to 5 of 5

Loop through cells and create string

Hybrid View

  1. #1
    Registered User
    Join Date
    04-24-2012
    Location
    San Francisco, USA
    MS-Off Ver
    Excel 2007
    Posts
    60

    Loop through cells and create string

    Hi there

    I would love some help

    lets say you have a two column table with kids names in column A and their ages in column B. Some kids are older than ten years old, some are younger.

    I want to write a macro that loops through and scoops up all the names of kids that are aged 10 or under. I then want to put all these names into a string like the following:

    "Kids aged ten or under are: bob, robert, dan, mike, martha"

    This string should then be placed in a cell in column A, 2 rows below the last row of the table.

    If you could give me the code it would be much appreciated.

    Many thanks
    Last edited by pablowilks; 04-24-2012 at 08:44 PM. Reason: poor formatting

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Loop through cells and create string

    How about the UDF approach? Instead of a macro, put this UDF code into a standard code module to give your workbook a new function you can use in a cell like any formula:

    Option Explicit
    
    Function AGELIST(MyRNG As Range, Age As Long, ListDir As String) As String
    'To get ages above a threshold, used in a cell like:   =AGELIST(A1:B10, 10, "OVER")
    'To get ages below a threshold, used in a cell like:   =AGELIST(A1:B10, 10, "UNDER")
    Dim cell As Range, Buf As String
    
    If MyRNG.Columns.Count <> 2 Then
        AGELIST = "1: Use a two-column range"
        Exit Function
    ElseIf Not Age > 0 Then
        AGELIST = "2: Use a whole number for the age"
        Exit Function
    ElseIf InStr(1, "OVERUNDER", ListDir) = 0 Then
        AGELIST = "3: OVER and UNDER only"
        Exit Function
    End If
    
        Select Case ListDir
            Case "OVER"
                For Each cell In MyRNG
                    If IsNumeric(cell) And cell >= Age Then Buf = Buf & ", " & cell.Offset(, -1)
                Next cell
            Case "UNDER"
                For Each cell In MyRNG
                    If IsNumeric(cell) And cell <= Age Then Buf = Buf & ", " & cell.Offset(, -1)
                Next cell
        End Select
        If Len(Buf) = 0 Then
            AGELIST = "none found"
        Else
            AGELIST = "Kids aged " & Age & " and " & ListDir & " are:" & Mid(Buf, 3, Len(Buf))
        End If
    End Function


    Used in a cell like:

    =AGELIST(A1:B10, 10, "OVER")

    The first parameter is your two-column source range
    The second parameter is the age threshold
    The third parameter is OVER or UNDER, sets the direction the threshold works
    Attached Files Attached Files
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

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

    Re: Loop through cells and create string

    UDF

    =MyReport(A2:B9,"<10")
    or
    =MyReport(A2:B9,"=>10")

    If you want to substitute "10" by cell reference
    =MyReport(A2:B9,"<"&C1)
    where C1 houses 10
    Function MyReport(rng As Range, criteria As String) As String
        Dim r As Range
        For Each r In rng.Columns(2).Cells
            If Evaluate(r.Value & criteria) Then
                MyReport = MyReport & ", " & r(, 0).Value
            End If
        Next
        If Len(MyReport) Then
            MyReport = Mid$(MyReport, 3)
        End If
    End Function

  4. #4
    Registered User
    Join Date
    04-24-2012
    Location
    San Francisco, USA
    MS-Off Ver
    Excel 2007
    Posts
    60

    Re: Loop through cells and create string

    great, much appreciated thanks

  5. #5
    Registered User
    Join Date
    04-24-2012
    Location
    San Francisco, USA
    MS-Off Ver
    Excel 2007
    Posts
    60

    Re: Loop through cells and create string

    thanks, thats great

+ 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