Loop through Columns and Change Row Values to be of the respective Column

ShuStar

Board Regular
Joined
Sep 9, 2015
Messages
89
Office Version
  1. 2010
Platform
  1. Windows
Hi all,

The Ask: For each column, starting from D3 to n number of Columns, replace the values of Y in each row reflect that of the column header.
ie if D3 Column header is Banana and D4 has Y, then it should replace D4 to be Banana, but dynamically do this as the number of Columns and Rows always change (ie can sometimes be 4 or 10 columns, and rows can be 5-100+ rows)


Current view:

IDNameCountBananaApplesNectarine....to nColumn
12345John1Y
12345Sara4YYYY
12345Alex3YYY
...to nRow##################


Sub to amend to:

IDNameCountBananaApplesNectarine...to nColumn
12345John1Banana
12345Sara4BananaApplesNectarine...to nColumn
12345Alex4BananaApples...to nColumn
...to nRow##################



My code where I can't seem to get passed Range(startcolumn, "D"&lastrow).select as it keeps erroring out here.

Any help to resolve this would be greatly appreciated (ie how the code should dynamically scan each column, do a find and replace if the value contains Y to reflect that of the Column name, and then move onto the next column and do the same).

VBA Code:
Sub changeYtorespectiveColumnName()



Dim lastrow As Long

Dim lastcolumn As Long

Dim startcolumn As Variant

Dim startingRange As Range

Dim InbtColumn As Long

Dim selectrange As Long

Dim i As Integer



Dim sht As Worksheet



Set sht = Sheets("Matrix")



lastrow = sht.Cells.SpecialCells(xlCellTypeLastCell).Row - 1

lastcolumn = sht.Cells.SpecialCells(xlCellTypeLastCell).Column

startcolumn = Cells(3, 4).Range

Range(startcolumn, "D" & lastrow).Select

startingRange.Select



'For each column

For j = 1 To lastcolumn



'scan through each row and if the value equals Y, then replace it with the title of the column header

For i = 1 To lastrow





Next i



Next j







End Sub
 
Thanks for letting us know.
Next time I check a little closer and leave the not needed lines out!!!!!
As you might have noticed, the lr line is unnecessary.
 
Upvote 0

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
JLGWhiz - yours is working too. thanks for this.

To All - the final thing I need to do here is to concatenate the row into a new column. Any ideas on how i can achieve this?

Ultimately, for each row starting from D3 to nth Column, I want to insert a new column to my table after Column C and just concatenate that row.

There may be a better way to do it without changing Ys to Column Header name etc but the flow in my head is:
-after changing the Ys to that of the name of column header then
-insert a new column after column C, which becomes new Column D (purpose for holding concatenated values) and then concatenate the rows from Column E to nth Column
-should look like "Banana, Apples, Lemon" and not "Banana, Apples , , Lemon" as there may be blank cells from the start column to end column - example below)


Example:

Currently looks like
IDNameCountBananaApplesNectarineLemon...to nColumn
12345John1Banana
12345Sara4BananaApplesNectarine...to nColumn
12345Alex3BananaApplesLemon...to nColumn
...to nRow##################


Change to (note new Concatenated column added):
IDNameCountConcatenatedBananaApplesNectarineLemon...to nColumn
12345John1Banana, ApplesBanana
12345Sara4Banana, Apples, NectarineBananaApplesNectarine...to nColumn
12345Alex4Banana, Apples, LemonBananaApplesLemon...to nColumn
...to nRow##################



Thanks inadvance Gurus, appreciate the help.
 
Upvote 0
I believe that is outside the scope of the original post and forum guidlines indicate the need for a new thread to be started for the concatenate part.
Thanks for your feedback on this post.
regards, JLG
 
Upvote 0

Forum statistics

Threads
1,214,985
Messages
6,122,606
Members
449,089
Latest member
Motoracer88

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