Solved

CONCATENATE multiple cell ranges delimeted by comma.

Posted on 2014-04-21
6
347 Views
Last Modified: 2014-04-21
I have a spreadsheet (macro enabled) that has several different locations on the sheet that I would like to evaluate (list box) for a Yes or No selection. If the selection is Yes, then the corresponding code will populate a cell along with all the other codes separated by a comma.  If the list box is set to NO or no value, the code is not placed in the cell.

I have tried using nested IF statements but can only nest 7 or 8 if statements before I get an error. I have more than 8 conditions to test and at different locations on the worksheet.

I have tried several nested if statements and they do not work. Ex.

=IF(D4="Yes",B4,"" &"," & IF(D5="Yes", B5,"" &"," & IF(D6="Yes",B6,"")))

with this: if d4:d6 = Yes, the code should be 01,02,03, the logic above yields only 01

=IF(D4="Yes",B4 &"," & IF(D5="Yes", B5 & "," & IF(D6="Yes",B6,"")))

with this: if d4:d6 = Yes the code is correct. if one or all of the selections is No or blank then it does not yeild the correct result.


I have included the workbook.

Any help would be greatly appreciated.


Thanks,Populate-codes.xlsm
0
Comment
Question by:jchongers71
  • 3
  • 3
6 Comments
 
LVL 43

Expert Comment

by:Saqib Husain, Syed
Comment Utility
Try this file with the UDF

Function yescodes(rng As Range)
    For Each cel In rng
        If cel.Value = "Yes" Then
            yescodes = yescodes & IIf(yescodes = "", "", ", ") & cel.Offset(, -2)
        End If
    Next cel
End Function
Populate-codes.xlsm
0
 

Author Comment

by:jchongers71
Comment Utility
Thanks for the speedy reply.

To get the result I am looking for I need to call the function for each section I have and place the results in a temporary cell. Those results that I get from calling the function I then have to use the concatenate function to tie it all together. is there a way to have this function work on multiple ranges?

I tried this and the result is #VALUE!

=yescodes(D4,D5,D6,D12,D13,D14,I4)

=yescodes(D4:D6,D12:D14,I4:I20)


Is there any way to have the function not return 0 when No or no option is selected? When I concatenate the results of the three functions when nothing is selected the cell is populated with 0,0,0


Thanks,
0
 
LVL 43

Expert Comment

by:Saqib Husain, Syed
Comment Utility
Try this code instead. You should not get the 0,0,0. If you do then upload a sample file showing this

Function yescodes(rng As Range, _
                        Optional rng1 As Range, _
                        Optional rng2 As Range, _
                        Optional rng3 As Range, _
                        Optional rng4 As Range)
If Not rng Is Nothing Then getcodes rng, yescodes
If Not rng1 Is Nothing Then getcodes rng1, yescodes
If Not rng2 Is Nothing Then getcodes rng2, yescodes
If Not rng3 Is Nothing Then getcodes rng3, yescodes
If Not rng4 Is Nothing Then getcodes rng4, yescodes

End Function

Sub getcodes(rng, yescodes)
    For Each cel In rng
        If cel.Value = "Yes" Then
            yescodes = yescodes & IIf(yescodes = "", "", ", ") & cel.Offset(, -2)
        End If
    Next cel
End Sub

Open in new window

0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:jchongers71
Comment Utility
Please check cell c17 as the result is a 0 when nothing is selected.

Populate-codes.xlsm
0
 
LVL 43

Accepted Solution

by:
Saqib Husain, Syed earned 500 total points
Comment Utility
Function yescodes(rng As Range, _
                        Optional rng1 As Range, _
                        Optional rng2 As Range, _
                        Optional rng3 As Range, _
                        Optional rng4 As Range)
yescodes = ""
If Not rng Is Nothing Then getcodes rng, yescodes
If Not rng1 Is Nothing Then getcodes rng1, yescodes
If Not rng2 Is Nothing Then getcodes rng2, yescodes
If Not rng3 Is Nothing Then getcodes rng3, yescodes
If Not rng4 Is Nothing Then getcodes rng4, yescodes

End Function

Sub getcodes(rng, yescodes)
    For Each cel In rng
        If cel.Value = "Yes" Then
            yescodes = yescodes & IIf(yescodes = "", "", ", ") & cel.Offset(, -2)
        End If
    Next cel
End Sub
0
 

Author Comment

by:jchongers71
Comment Utility
Awesome. Thanks so much. I have been wracking my brain on this one for a bit.

This works flawlessly.
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now