+ Reply to Thread
Results 1 to 11 of 11

To concatenate column headers if value in rows below is non-blank

  1. #1
    Registered User
    Join Date
    09-17-2015
    Location
    Singapore
    MS-Off Ver
    Microsoft Excel 2013
    Posts
    7

    To concatenate column headers if value in rows below is non-blank

    Hi all,

    As I'm pretty new to VBA it's still tough to amend codes that I've found. Would like to use Macros to perform the following task:

    I've the followng table, just imagine with many many more columns and the outcome I want is in the following

    OK NO OK OK
    Outcome Header 1 Header 2 Header 3 Header 4
    Header 1 5,000 5,000
    Header 3, Header 4 6,000 6,000

    So essentially, if the first row = "OK" and if there are values in the columns below (aka non-blank), to concatenate the header names with commas separating them.

    Using formulas is possible but a nightmare given how many columns there are. So I'm wondering if there is a more efficient way to perform this by using a VBA function. But I'm very weak with amending scripts I found so far so thought it might be easier to approach experts online. Would appreciate if you could explain the script as well if you have time. Would greatly appreciate any help. Thanks!

  2. #2
    Registered User
    Join Date
    09-15-2015
    Location
    France
    MS-Off Ver
    MS Office 2010
    Posts
    5

    Re: To concatenate column headers if value in rows below is non-blank

    Hi,

    Could you post a before and an after table ? Because I didn't get what you want to do exactly

    Then I could help you better

    Clémence.

  3. #3
    Forum Expert
    Join Date
    11-26-2013
    Location
    Colac, Victoria, Australia
    MS-Off Ver
    Excel 2016
    Posts
    1,309

    Re: To concatenate column headers if value in rows below is non-blank

    Here is a simple VBA solution which simply loops through your data and provides the Outcome you want in Col A.

    I hope you can follow without further explanation.

    The only thing you need to know is that since I have no idea how big your model is, I used CONST to provide a range.

    I hope this helps, please let me know!

    Regards,

    David

    Please remember, your request is clearer if you attach a sample workbook.
    - Click on Go Advanced and click on the Paper Clip.

    If this has been of assistance, please advise.
    - Please click on the *Add Reputation button at the bottom of helpful responses.

    Please mark your thread as SOLVED:
    - Click Thread Tools above your first post, select "Mark your thread as Solved".

    Attached Files Attached Files

  4. #4
    Registered User
    Join Date
    09-17-2015
    Location
    Singapore
    MS-Off Ver
    Microsoft Excel 2013
    Posts
    7

    Re: To concatenate column headers if value in rows below is non-blank

    Hello!

    Have given names to the rows and headers so it's clearer
    Fruits: Apple Banana Orange Pineapple
    Status: OK Rotten OK OK
    Company A: 5,000 4,000
    Company B: 6,000 6,000

    So Row 1 are the fruits. Row 2 is the status of the fruits. Row 3 and 4 is the no. of fruits owned by Company A and B respectively.

    So what I want to do is: For each Company, If the fruit is "OK" under row 2 AND If the Company owns that particular fruit (aka there is an amount in the cell and it's NOT blank), to concatenate the fruit names. So as a result, I'll have an additional column as follows titled "Fruits owned" with the formula to concatenate the fruit names based on the above criteria.

    Fruits: Apple Banana Orange Pineapple Fruits owned
    Status: OK Rotten OK OK
    Company A: 5,000 4,000 Apple
    Company B: 6,000 6,000 Orange, Pineapple

    What I can manually do is the following formula (assuming the topleft most call is A1):
    =CONCATENATE(IF(AND(B3<>"",B2="OK"),B1&",",""), IF(AND(C3<>"",C2="OK"),C1&",",""), IF(AND(D3<>"",D2="OK"),D1&",",""), IF(AND(E3<>"",E2="OK"),E1,""))

    But once i have many columns of data, it's abit challenging and time consuming to use this formula. So hope you can help . Thanks!

  5. #5
    Registered User
    Join Date
    09-17-2015
    Location
    Singapore
    MS-Off Ver
    Microsoft Excel 2013
    Posts
    7

    Re: To concatenate column headers if value in rows below is non-blank

    Oh! Thanks David. That's the outcome i'm looking for! But would it be possible to put it as a function instead of a button? so maybe if I key in the cell the following formula "=[Functionname]([Range for fruits],[Range for status],[Range for company row])", it'll read the function and give the data. That's what I was initially trying to achieve but failed miserably when I was trying to combine functions together.

  6. #6
    Forum Expert
    Join Date
    11-26-2013
    Location
    Colac, Victoria, Australia
    MS-Off Ver
    Excel 2016
    Posts
    1,309

    Re: To concatenate column headers if value in rows below is non-blank

    Easy enough! But not without the workbook because you have changed everything in your latest post.

    DAC

  7. #7
    Registered User
    Join Date
    09-17-2015
    Location
    Singapore
    MS-Off Ver
    Microsoft Excel 2013
    Posts
    7

    Re: To concatenate column headers if value in rows below is non-blank

    Also to give some perspective, my excel sheet range from col A to BO but my data is only from col F to BO. And there are around 253 companies from row 5 to 257. The formula I plan to put it for the cells under col B. Not sure if these helps.

  8. #8
    Registered User
    Join Date
    09-17-2015
    Location
    Singapore
    MS-Off Ver
    Microsoft Excel 2013
    Posts
    7

    Re: To concatenate column headers if value in rows below is non-blank

    Okay let me do up a sample workbook taking away the confidential data!

  9. #9
    Registered User
    Join Date
    09-17-2015
    Location
    Singapore
    MS-Off Ver
    Microsoft Excel 2013
    Posts
    7

    Re: To concatenate column headers if value in rows below is non-blank

    Hi, I've done up the workbook which simulates what I've. There were other variables like there are headers with duplicated data which I initially wanted it to concatenate only the unique values if there were duplicates such as in row 7 on the file. But not sure if that will make things too complicated since my macro knowledge is kinda below basic.

    So row 4 is like my "Fruits" row. Row 1 is my "Status" row where I want to only concatenate them if it's "S" in this case.

    SAMPLE.xlsm

  10. #10
    Registered User
    Join Date
    09-15-2015
    Location
    France
    MS-Off Ver
    MS Office 2010
    Posts
    5

    Re: To concatenate column headers if value in rows below is non-blank

    Can you clarify this to me:
    Is it possible that Apples coming from Company A are OK but apples coming from Company B aren't ?

    Because it isn't possible in your chart at this moment.

    Clémence
    --
    It's not bragging if it's true.

  11. #11
    Registered User
    Join Date
    09-17-2015
    Location
    Singapore
    MS-Off Ver
    Microsoft Excel 2013
    Posts
    7

    Re: To concatenate column headers if value in rows below is non-blank

    Yup it's not possible. But refering to row 4, the duplicated values could be one S one IS or both S. So if i don't just concatenate the unique values and it's both "S" I'll see repeated values being concatenate. But if it's very difficult to address, I can kinda live with it.

    Edit:
    Oops Clemence realised I misread your question. But yes, it's not possible. The data will definitely fall in each column and there won't be cases where the one column splits in two.
    Last edited by chibidee; 09-17-2015 at 12:46 PM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Concatenate Multiple Rows And Switch Columns If Blank
    By Knawl in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 11-17-2014, 05:11 AM
  2. [SOLVED] Remove blank cells form range due to Column headers.
    By Nitefox in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-11-2014, 09:49 PM
  3. [SOLVED] Add column headers for each 25 rows
    By Pavan.Sada.PS in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 11-25-2013, 02:32 PM
  4. Returning non-blank cell column/row headers to new array
    By jjkellog in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 08-19-2013, 10:45 AM
  5. deleting blank rows in a concatenate formula
    By jettakay in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 04-20-2013, 04:56 PM
  6. Rank between headers or blank rows
    By beat in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-10-2012, 05:35 AM
  7. Count Number of Non-Blank Rows for Colums with Certain Headers
    By alfykunable in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-17-2012, 01:56 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1