Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
Solved

CONCATENATE multiple cell ranges delimeted by comma.

Posted on 2014-04-21
Medium Priority
360 Views
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
Question by:jchongers71
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 3
• 3

LVL 43

Expert Comment

ID: 40012925
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

ID: 40012981

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

ID: 40013074
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
``````
0

Author Comment

ID: 40013160
Please check cell c17 as the result is a 0 when nothing is selected.

Populate-codes.xlsm
0

LVL 43

Accepted Solution

Saqib Husain, Syed earned 2000 total points
ID: 40013171
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

ID: 40013178
Awesome. Thanks so much. I have been wracking my brain on this one for a bit.

This works flawlessly.
0

Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
This article describes how you can use Custom Document Properties to store settings and other information in your workbook so that they will be available the next time you open the workbook.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Suggested Courses
Course of the Month11 days, 21 hours left to enroll