+ Reply to Thread
Results 1 to 5 of 5

Macro returning string, while i want array

Hybrid View

  1. #1
    Registered User
    Join Date
    02-02-2010
    Location
    Providence, Rhode Island
    MS-Off Ver
    Excel 2003
    Posts
    2

    Macro returning string, while i want array

    Hi!

    My problem started when i was trying to get a VLookup to return mulitple values. For example, I wanted it to return every value that had the word "YES" beside it. But, as we all know, once a VLookup finds the lookup value, it stops searching.

    I then discovered a macro that will return multiple values based on a given criteria. It is:

    Public Function FindSeries(TRange As Range, MatchWith As String)
         
        For Each cell In TRange
            If cell.Value = MatchWith Then
                x = x & cell.Offset(0, 1).Value & ", "
            End If
        Next cell
         
        FindSeries = Left(x, (Len(x) - 2))
         
    End Function

    However, it is returning all the value as a string in the same cell separated by commas! I want it to return each value into a new cell (preferrably going vertically). I'm sure there must be an easy manipulation to this macro to make it return each value in its own cell, rather than as a string. Please help!!!


    Thank you so much
    Last edited by DanceFanatic707; 03-01-2010 at 02:30 PM.

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Macro returning string, while i want array

    Welcome to the forum.

    Please take a few minutes to read the forum rules, and then edit your post to add code tags.

    Thanks.please
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Macro returning string, while i want array

    Your post does not comply with Rule 8 of our Forum RULES. Cross-posting is when you post the same question in other forums on the web. You'll find people are disinclined to respond to cross-posts because they may be wasting their time solving a problem that has been solved elsewhere. We prefer that you not cross-post at all, but if you do (and it's unlikely to go unnoticed), you MUST provide a link (copy the url from the address bar in your browser)to the cross-post. Expect cross-posts without a link to be closed a message will be posted by the moderator explaining why. We are here to help so help us help you!

    Cross posted here
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  4. #4
    Registered User
    Join Date
    02-02-2010
    Location
    Providence, Rhode Island
    MS-Off Ver
    Excel 2003
    Posts
    2

    Re: Macro returning string, while i want array

    Sorry about the double posting, didn't realize it was frowned upon. Due to the time sensitive nature I figured I'd post in more than one place. The other place I posted was here:

    http://www.thecodecage.com/forumz/me...tml#post655925


    Again, I apologize

  5. #5
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Macro returning string, while i want array

    This version will return an array. If you are calling from a sheet, you need to select the range of cells where you want the value returned and enter =FindSeries(..) as an array formula, using Ctrl-Shift-Enter (Cmd+Return for Mac) to enter the formula.
    Public Function FindSeries(TRange As Range, MatchWith As String)
        Dim retArray as Variant
        For Each cell In TRange
            If cell.Value = MatchWith Then
                x = x & cell.Offset(0, 1).Value & ", "
            End If
        Next cell
         
        retArray = Split(Left(x, (Len(x) - 2)),", ")
        
        FindSeries = retArray
    End Function
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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