Solved

Excel VBA concatenate table header txt for row/col intersections marked with "X"

Posted on 2011-03-13
6
853 Views
Last Modified: 2012-05-11
I have a dynamic table with "i" rows and "j" columns.
The top row will have header labels.
The left-most column cells will be enumerated 1, 2, 3...
The next column will be populated by VBA
Besides these columns 1 & 2, the only content in the body of the table are "X" or blank.

I need help with the code that will enter into each cell of col 2 the concatenated text of the header label corresponding to each cell with an "X".

Table will be appox 150rows x 30col so looping will probably be slow and should be avoided if possible.


Thanks, --Andres
0
Comment
Question by:AndresHernando
  • 4
  • 2
6 Comments
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 35126030
Do you want the headers all joined up, or separated with a delimiter such as a space or comma?
0
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 35127087
One other question: this could be done using a subroutine to populate the column with hardcoded values, or you could use a VBA UDF located in each cell, so the formulas would be live. Any preference?
0
 

Author Comment

by:AndresHernando
ID: 35127242
rorya, thanks for your attention.  In answer to your q's:
1. Separated by coma and a space
2. No preference on whether live or hard-coded.  Whichever is "lighter" on resources.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 85

Expert Comment

by:Rory Archibald
ID: 35127307
Here's a function version:
Public Function ConcatIf(rngCrit As Range, varCriterion, rngConcat As Range, Optional strDelimiter As String = ", ")
   Dim varData, varCrit
   Dim lngrow As Long, lngCol As Long
   If rngCrit.Rows.Count <> rngConcat.Rows.Count Or rngCrit.Columns.Count <> rngConcat.Columns.Count Then
      ConcatIf = CVErr(xlErrRef)
   Else
      varCrit = rngCrit.value
      varData = rngConcat.value
      For lngrow = LBound(varCrit, 1) To UBound(varCrit, 1)
         For lngCol = LBound(varCrit, 2) To UBound(varCrit, 2)
            If StrComp(varCrit(lngrow, lngCol), varCriterion, vbTextCompare) = 0 Then
               ConcatIf = ConcatIf & strDelimiter & varData(lngrow, lngCol)
            End If
         Next lngCol
      Next lngrow
      If Len(ConcatIf) > 0 Then ConcatIf = Mid$(ConcatIf, Len(strDelimiter) + 1)
   End If
End Function

Open in new window


in a cell you would use it like:
=concatif(B2:I2,"x",$B$1:$I$1)
to concatenate values in B1:I1 where B2:I2 contains 'X'
0
 
LVL 85

Accepted Solution

by:
Rory Archibald earned 500 total points
ID: 35127341
Small tweak to return a null string if there are no matches:
Public Function ConcatIf(rngCrit As Range, varCriterion, rngConcat As Range, Optional strDelimiter As String = ", ")
   Dim varData, varCrit
   Dim lngrow As Long, lngCol As Long
   If rngCrit.Rows.Count <> rngConcat.Rows.Count Or rngCrit.Columns.Count <> rngConcat.Columns.Count Then
      ConcatIf = CVErr(xlErrRef)
   Else
      varCrit = rngCrit.value
      varData = rngConcat.value
      For lngrow = LBound(varCrit, 1) To UBound(varCrit, 1)
         For lngCol = LBound(varCrit, 2) To UBound(varCrit, 2)
            If StrComp(varCrit(lngrow, lngCol), varCriterion, vbTextCompare) = 0 Then
               ConcatIf = ConcatIf & strDelimiter & varData(lngrow, lngCol)
            End If
         Next lngCol
      Next lngrow
      If Len(ConcatIf) > 0 Then
         ConcatIf = Mid$(ConcatIf, Len(strDelimiter) + 1)
      Else
         ConcatIf = ""
      End If
   End If
End Function

Open in new window

0
 

Author Closing Comment

by:AndresHernando
ID: 35127925
Beautiful, rorya!!!!

Thanks, --Andres
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Sparklines have been introduced with Excel 2010 and are a useful tool for creating small in-cell charts, used for example in dashboards. Excel 2010 offers three different types of Sparklines: Line, Column and Win/Loss. What it does not offer is a…
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

920 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

16 Experts available now in Live!

Get 1:1 Help Now