?
Solved

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

Posted on 2011-03-13
6
Medium Priority
?
881 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
[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
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 2000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
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…

770 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