logical OR 2 columns and conctenate reesult in VBA

stravo

New Member
Joined
Mar 12, 2014
Messages
3
[TABLE="class: MsoNormalTable, width: 2"]
<tbody>[TR]
[TD="width: 80, bgcolor: transparent"] xxx<o:p></o:p>
[/TD]
[TD="width: 80, bgcolor: transparent"]
1<o:p></o:p>
[/TD]
[TD="width: 80, bgcolor: transparent"]
<!--[if gte vml 1]><v:shapetype id="_x0000_t202" coordsize="21600,21600" o:spt="202" path="m,l,21600r21600,l21600,xe"> <v:stroke joinstyle="miter"/> <v:path gradientshapeok="t" o:connecttype="rect"/> </v:shapetype><v:shape id="Text_x0020_Box_x0020_2" o:spid="_x0000_s1026" type="#_x0000_t202" style='position:absolute;left:0;text-align:left; margin-left:-3.35pt;margin-top:-53.9pt;width:46.8pt;height:49.8pt;z-index:251661312; visibility:visible;mso-wrap-style:square;mso-width-percent:0; mso-height-percent:0;mso-wrap-distance-left:9pt;mso-wrap-distance-top:0; mso-wrap-distance-right:9pt;mso-wrap-distance-bottom:0; mso-position-horizontal:absolute;mso-position-horizontal-relative:text; mso-position-vertical:absolute;mso-position-vertical-relative:text; mso-width-percent:0;mso-height-percent:0;mso-width-relative:margin; mso-height-relative:margin;v-text-anchor:top' o:gfxdata="UEsDBBQABgAIAAAAIQC75UiUBQEAAB4CAAATAAAAW0NvbnRlbnRfVHlwZXNdLnhtbKSRvU7DMBSFdyTewfKKEqcMCKEmHfgZgaE8wMW+SSwc27JvS/v23KTJgkoXFsu+P+c7Ol5vDoMTe0zZBl/LVVlJgV4HY31Xy4/tS3EvRSbwBlzwWMsjZrlprq/W22PELHjb51r2RPFBqax7HCCXIaLnThvSAMTP1KkI+gs6VLdVdad08ISeCho1ZLN+whZ2jsTzgcsnJwldluLxNDiyagkxOquB2Knae/OLUsyEkjenmdzbmG/YhlRnCWPnb8C898bRJGtQvEOiVxjYhtLOxs8AySiT4JuDystlVV4WPeM6tK3VaILeDZxIOSsuti/jidNGNZ3/J08yC1dNv9v8AAAA//8DAFBLAwQUAAY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HwAAAAAAAAAAAAAAAAAgAgAAY2xpcGJvYXJkL2RyYXdpbmdzL2RyYXdpbmcxLnhtbFBLAQItABQABgAIAAAAIQCcTl4h4gYAADocAAAaAAAAAAAAAAAAAAAAAEIFAABjbGlwYm9hcmQvdGhlbWUvdGhlbWUxLnhtbFBLAQItABQABgAIAAAAIQCcZkZBuwAAACQBAAAqAAAAAAAAAAAAAAAAAFwMAABjbGlwYm9hcmQvZHJhd2luZ3MvX3JlbHMvZHJhd2luZzEueG1sLnJlbHNQSwUGAAAAAAUABQBnAQAAXw0AAAAA"/><![endif]--><!--[if !vml]-->​
<tbody> [TD="width: 85, bgcolor: white"]<!--[endif]--><!--[if !mso]-->
<tbody> [TD="bgcolor: transparent"]<!--[endif]-->
After Logical OR<o:p></o:p>
<!--[if !mso]--> [/TD]
</tbody>
<!--[endif]--><!--[if !mso & !vml]--> <!--[endif]--><!--[if !vml]--> [/TD]
</tbody>
<!--[endif]-->xxx<o:p></o:p>
[/TD]
[/TR]
[TR]
[TD="width: 80, bgcolor: transparent"] xx<o:p></o:p>
[/TD]
[TD="width: 80, bgcolor: transparent"]
<o:p></o:p>
[/TD]
[TD="width: 80, bgcolor: transparent"]
<o:p> </o:p>
[/TD]
[/TR]
[TR]
[TD="width: 80, bgcolor: transparent"] x<o:p></o:p>
[/TD]
[TD="width: 80, bgcolor: transparent"]
1<o:p></o:p>
[/TD]
[TD="width: 80, bgcolor: transparent"]
x<o:p></o:p>
[/TD]
[/TR]
[TR]
[TD="width: 80, bgcolor: transparent"] <o:p></o:p>
[/TD]
[TD="width: 80, bgcolor: transparent"]
<o:p></o:p>
[/TD]
[TD="width: 80, bgcolor: transparent"]
<o:p> </o:p>
[/TD]
[/TR]
[TR]
[TD="width: 80, bgcolor: transparent"] xx<o:p></o:p>
[/TD]
[TD="width: 80, bgcolor: transparent"]
1<o:p></o:p>
[/TD]
[TD="width: 80, bgcolor: transparent"]
xx<o:p></o:p>
[/TD]
[/TR]
</tbody>[/TABLE]
Col A col B colC<o:p></o:p>
Col A and Col B can be variable length columns (but they arethe same size)<o:p></o:p>
I need to perform a logical OR on col A & B (whichgenerates Col C). I want to take the output of the Logical OR (col C) andconcatenate all the non-blank cells separated by a delimiter (a space for thisexample). So the final result is a single cell with the contents “xxx x xx”which I’ll call the ANSWER<o:p></o:p>
Since this is part of a user form I don’t want to generateCol C in the spreadsheet. I want it to be virtual with only a single cell result(hence it either needs to be a cell formula or a VBA function). I can do thiswith a cell formula using the concatenate function but for large columns, thecell formula won’t fit as it takes too many characters. I have a routine thatcan take a range of variable type data that will remove the blank cells andgenerate the ANSWER. Here it is: <o:p></o:p>
<o:p></o:p>
Function ConCat(Delimiter As Variant, ParamArrayCellRanges() As Variant) As String<o:p></o:p>
Dim CellAs Range, Area As Variant<o:p></o:p>
IfIsMissing(Delimiter) Then Delimiter = ""<o:p></o:p>
<o:p> </o:p>
For EachArea In CellRanges<o:p></o:p>
IfTypeName(Area) = "Range" Then<o:p></o:p>
For Each Cell In Area<o:p></o:p>
If Len(Cell.Value) Then ConCat = ConCat & Delimiter & Cell.Value<o:p></o:p>
Next<o:p></o:p>
Else<o:p></o:p>
ConCat = ConCat & Delimiter& Area<o:p></o:p>
EndIf<o:p></o:p>
Next<o:p></o:p>
ConCat =Mid(ConCat, Len(Delimiter) + 1)<o:p></o:p>
End Function<o:p></o:p>
<o:p></o:p>
I can generate Col C with this formula {=IF((B1:B5=1),IF((A1:A5)="","",(A1:A5)),""))}<o:p></o:p>
If I use the ConCat function on Col C, {=concat(“ “,C1:C5)}results in the ANSWER “xxx x xx”<o:p></o:p>
If I type the values of ColC into the ConCat function{=concat(“ “,”xxx”,””,”x”,””,”xx”) I get the ANSWER<o:p></o:p>
However if I combine the two to do this in 1-step:<o:p></o:p>
{=concat(" ",IF((B1:B5=1),IF((A1:A5)="","",(A1:A5)),""))}I get #VALUE! Error<o:p></o:p>
Looking at the debugger, the resultant of the above IF statementsthat passes the values to ConCat is exactly {=concat(“ “,”xxx”,””,”x”,””,”xx”)which then evaluates to the #VALUE! Error. What is happening? Why won’t itwork?<o:p></o:p>
Some clues from spending a lot of debugging time. TheCaonCat function bombs at the step in yellow when it is trying to concatenate thevariant variable AREA (which TypeName (AREA) says is a string. Apparently theIF statements when parsed into the input to ConCat are placed into AREA as amulti-dimensional Array and it is not compatible with concatenate. The othercalls , {=concat(“ “,C1:C5)} and {=concat(“ “,”xxx”,””,”x”,””,”xx”) are parsedas individual cells and loop through one at a time. The combined function istrying to do it all in a single pass and not one cell at a time. <o:p></o:p>
Anyone have any ideas on how to fix this?<o:p></o:p>
Thanks<o:p></o:p>
mark<o:p></o:p>
<o:p> </o:p>
 
What are your inputs in columns A and B and the desire out puts.

OR is a logical function that acts on boolean values, none of the entries that you show are boolean.

The formula that you posted seems to be an array version of =REPT(A1, (B1=1))

In words, what do you want ConCat to do?
 
Upvote 0
Perhaps what you want is this UDF. The arguments of ConcatIf mirror those of SumIf.

If you want to concatenate the values in column A, whenever column B is 1, with a space delimiter, the formula =ConcatIf(B:B, 1, A:A, " ") should work.

Code:
Function ConcatIf(ByVal compareRange As Range, ByVal xCriteria As Variant, Optional ByVal stringsRange As Range, _
                    Optional Delimiter As String, Optional NoDuplicates As Boolean) As String
                    
    Rem the first three argumens of ConcatIf mirror those of SUMIF
    Rem the Delimiter and NoDuplicates arguments are optional (default "" and False)
    Dim i As Long, j As Long
    
    With compareRange.Parent
        Set compareRange = Application.Intersect(compareRange, Range(.UsedRange, .Range("a1")))
    End With
    
    If compareRange Is Nothing Then Exit Function
    If stringsRange Is Nothing Then Set stringsRange = compareRange
    Set stringsRange = compareRange.Offset(stringsRange.Row - compareRange.Row, _
                                            stringsRange.Column - compareRange.Column)
    
    For i = 1 To compareRange.Rows.Count
        For j = 1 To compareRange.Columns.Count
            If (Application.CountIf(compareRange.Cells(i, j), xCriteria) = 1) Then
                If InStr(ConcatIf, Delimiter & CStr(stringsRange.Cells(i, j))) <> 0 Imp Not (NoDuplicates) Then
                    ConcatIf = ConcatIf & Delimiter & CStr(stringsRange.Cells(i, j))
                End If
            End If
        Next j
    Next i
    ConcatIf = Mid(ConcatIf, Len(Delimiter) + 1)
End Function
 
Upvote 0
hi mike, thanks I'll try it. What i'm doing is taking a changing (due to new formulations, test results, regulations, etc.) list of fungicides for my vineyard that have different effectiveness for each of several or so fungi that I need to protect against (e.g. black rot, phomopsis, powdery mildew, downey mildew, botrytis, anthracnose, etc) that are active at particular times and conditions during the growing season. The relative effectiveness is given for each fungus as either a "blank", "x", "xx", "xxx" where blank means no control and "xxx" means excellent control. I have a list of 40 or so fungicides that I can use. So I set up a spreadsheet that lists all the chemicals down column 1 with dates across the first row. So after determining what I need to spray for, I pick several fungicides to put in my sprayer. I indicate this by putting a "1" in the cell for the fungicide and date. I leave the first six rows for calculating the overall "spray effectiveness" for each fungus for what I am planning to spray. This is the output of the subroutine that I want so at a glance I can see what I am protecting my vines against. Having "xxx" in a box means I have essentially provided protection for that disease for the next 2 weeks. Too many "xxx" in one disease means I'm wasting money and having "x" or blank means I am taking a risk for protecting the vines (I may not want to protect against a disease if it isn't active so that is desirable too). There are other factors that I need to track such as re-entry times, pre-harvest intervals, maximum rates per acre per the manuf label, compatibility with mixing fungicides together, and several other factors. At the end of the season I have a full spray matrix of what I used and when which I can quickly figure out my inventories at the end of each year so I can put my order in for the season in the spring each year. I had put together this spreadsheet just using the concatenate function but it was a lot of work to add new fungicides, and other aspects of my grand plan. I was looking for a better (and easier way to do this). I will copy your code into the spreadsheet and try it out later today. I appreciate your help. I was stymied by this for a while.
 
Upvote 0

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top