+ Reply to Thread
Results 1 to 7 of 7

Needexplanation of a VBA code (Macro) regarding returning multiple values in a single cell

  1. #1
    Registered User
    Join Date
    01-14-2016
    Location
    Houston, Tx
    MS-Off Ver
    2010
    Posts
    15

    Needexplanation of a VBA code (Macro) regarding returning multiple values in a single cell

    I have a Macro for excel I found online to create a 'myvlookup' function which allows to lookup all instances of a number in a range and return a specified corresponding column; multiple instances if necessary. I've gotten the macro and respective myvlookup to work for my needs but I'd like an explanation of the logic/meanings behind how this code works. Code is below and any information would be helpful. P.S I'm code illiterate, so don't assume I know anything lol. Thanks!

    Function MYVLOOKUP(pValue As String, pWorkRng As Range, pIndex As Long)

    Dim rng As Range

    Dim xResult As String

    xResult = ""

    For Each rng In pWorkRng

    If rng = pValue Then
    xResult = xResult & " * " & rng.Offset(0, pIndex - 1)
    End If
    Next

    MYVLOOKUP = xResult

    End Function

  2. #2
    Forum Moderator zbor's Avatar
    Join Date
    02-10-2009
    Location
    Croatia
    MS-Off Ver
    365 ProPlus
    Posts
    15,405

    Re: Needexplanation of a VBA code (Macro) regarding returning multiple values in a single

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code.

    Posting code between [CODE]Please [url=https://www.excelforum.com/login.php]Login or Register [/url] to view this content.[/CODE] tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.

    Highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here



    (This thread should receive no further responses until this moderation request is fulfilled, as per Forum Rule 7)

  3. #3
    Registered User
    Join Date
    01-14-2016
    Location
    Houston, Tx
    MS-Off Ver
    2010
    Posts
    15

    Re: Needexplanation of a VBA code (Macro) regarding returning multiple values in a single

    Sorry. Lets try this again. My code is below.

    Please Login or Register  to view this content.
    thanks!

  4. #4
    Forum Moderator zbor's Avatar
    Join Date
    02-10-2009
    Location
    Croatia
    MS-Off Ver
    365 ProPlus
    Posts
    15,405

    Re: Needexplanation of a VBA code (Macro) regarding returning multiple values in a single

    Your function MYVLOOKUP use 3 variable:
    • pValue wich is String
    • pWorkRng is Range where you comparing your string
    • pIndex is n-th column

    So for each value in defined pWorkRng range
    compare is that value same as string

    and if it's same then add word from n-th column separated with *

    When you compare all words give output for example: Hope * this * was * explained * well

  5. #5
    Registered User
    Join Date
    01-14-2016
    Location
    Houston, Tx
    MS-Off Ver
    2010
    Posts
    15

    Re: Needexplanation of a VBA code (Macro) regarding returning multiple values in a single

    I think I understand the definitions of the variables but I think I'm lost in the below portion of the code. Again I'm not very familiar with code so dummy terms may be needed. Thanks for any help!

    Please Login or Register  to view this content.

  6. #6
    Forum Moderator zbor's Avatar
    Join Date
    02-10-2009
    Location
    Croatia
    MS-Off Ver
    365 ProPlus
    Posts
    15,405

    Re: Needexplanation of a VBA code (Macro) regarding returning multiple values in a single

    I'll try to put in parallel for easier comparation:


    Please Login or Register  to view this content.
    How xResult is added:
    Let say you have 3 words: A, B and C

    xResult = A

    Second time it add B:

    xResult = xResult * new word
    xResult = A * B

    Third time it add C

    xResult = xResult * new word
    xResult = A * B * C
    xResult =

  7. #7
    Registered User
    Join Date
    01-14-2016
    Location
    Houston, Tx
    MS-Off Ver
    2010
    Posts
    15

    Re: Needexplanation of a VBA code (Macro) regarding returning multiple values in a single

    That's a great explanation! Thank you.

    Can you also give a clear breakdown of the below portion of the formula please?

    rng.Offset(0, pIndex - 1)

    Sorry I'm not at my pic now and don't see the '#' for the code.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Returning Multiple Values Separated by Commas to a single cell
    By enragedpigeon in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 02-02-2015, 05:48 PM
  2. [SOLVED] Looking for a single text value and returning 1 or multiple text values in one cell
    By cramnij in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 11-07-2014, 04:59 AM
  3. Replies: 2
    Last Post: 04-30-2014, 05:59 AM
  4. [SOLVED] Excel - Returning multiple values in a single cell that match criteria
    By amazinz2006 in forum Excel General
    Replies: 13
    Last Post: 01-25-2014, 09:35 PM
  5. Checking for single value, returning Multiple values
    By English_Bloke82 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-11-2013, 01:31 PM
  6. Returning a value to a single cell based on multiple values elsewhere.
    By archieross in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 12-05-2012, 07:35 PM
  7. Returning ordered values from a string in a single cell
    By Dissonant in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 10-23-2010, 12:52 PM

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