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
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Sub t()
Dim rng As Range, c As Range
Set rng = Range("D1", Cells(Columns.Count).End(xlToLeft))
For Each c In rng
Columns(c.Column).Replace "Y", c.Value
Next
End Sub
 
Upvote 0
Thank you for your reply but this hasn't worked. It just cleared everything in Column D and did not move onto Column E.

I did also change D1 to D2 in your code (of which Col D2 is the Column Header in my sheet) but again, this just cleared all the rows in Col D and Col E remained as Ys
 
Upvote 0
Ahh but when I change your code .EndxltoLeft to .EndxlRight, then it the loop is working - but it replace Y to blank, rather than the value of the Column Name.

To Clarify
My Headers are in Row 2
The first column that it should begin this looping find&replace is Col D onwards

Example D2 column name is Banana, if D3 is marked as Y, then this Y should become Banana, and do this find&replace until the end of that column. Then go on to Column E and do the same. thereafter Column F through to nColumn
 
Upvote 0
Anyone know how i can fix this code so it can change to below (provided by JLGWhiz - thank you!!)? I think it's 90% there, just instead of the VBA removing the Y and replacing it with blanks, I'd like to amend to code to instead replace the Y with the respective Column Header. It is looping through the right amount columns correctly though - just places blanks each time unfortunately.

VBA Code:
Sub t()
Dim Rng As Range, c As Range
Set Rng = Range("D2", Cells(Columns.Count).End(xlToRight))
For Each c In Rng
Columns(c.Column).Replace "Y", c.Value


Next
End Sub





My Headers are in Row 2
The first column that it should begin this looping find&replace is Col D onwards

Example D2 column name is Banana, if D3 is marked as Y, then this Y should become Banana, and do this find&replace until the end of that column. Then go on to Column E and do the same. thereafter Column F through to nColumn

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##################
 
Upvote 0
I swear You Can do this with a Vlookup or a hlookup or even a countif.you don't need a macro. But I'm lying in my bed. Work in 6 hours.

& What's wrong with just =if(isblank(b$2),"",b$2) .. etc for each row/ cell in your table range ??.
 
Last edited:
Upvote 0
I meant , (on a new sheet, sheet2) do =if(isblank(Sheet1!d4),"",sheet1!d$2) .. Drag down & across.
& Then Copy them over when your ready ?

You could fill your entire table that way , and it would update when if change y's .

I should be asleap.
 
Upvote 0
Try
Code:
Sub Maybe()
Dim lr As Long, lc As Long, i As Long
lr = Cells.Find("*", , , , xlByRows, xlPrevious).Row
lc = Cells.Find("*", , , , xlByColumns, xlPrevious).Column
    For i = 4 To lc
        Columns(i).Replace "Y", Cells(2, i), xlWhole
    Next i
End Sub
 
Upvote 0
@jolivanes has a more universal code but for clarification, here is a mod to my original with the typo fixed.

VBA Code:
Sub t()
Dim rng As Range, c As Range
Set rng = Range("D2", Cells(2, Columns.Count).End(xlToLeft))
    For Each c In rng
        Columns(c.Column).Replace "Y", c.Value
    Next
End Sub
 
Upvote 0
Thank you all. @jolivanes - yours has worked perfectly!! It's exactly the way I was thinking it but couldn't piece it together, brilliant, thank you.

Ahah Spyros13, your comment made me laugh....Re using formula/lookup - dont want the sheet to be formula intensive or creating another tab (along with the many existing tabs) - good shout though.

Thank you all for your support
 
Upvote 0

Forum statistics

Threads
1,215,493
Messages
6,125,119
Members
449,206
Latest member
burgsrus

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