+ Reply to Thread
Results 1 to 7 of 7

looping through columns to find a value then replace said value with column header

Hybrid View

Sybille looping through columns to... 10-22-2018, 09:45 AM
Mumps1 Re: looping through columns... 10-22-2018, 09:59 AM
Sybille Re: looping through columns... 10-22-2018, 10:14 AM
Mumps1 Re: looping through columns... 10-22-2018, 10:16 AM
davsth Re: looping through columns... 10-22-2018, 10:21 AM
Mumps1 Re: looping through columns... 10-22-2018, 10:38 AM
Sybille Re: looping through columns... 10-22-2018, 10:38 AM
  1. #1
    Forum Contributor
    Join Date
    10-09-2018
    Location
    Quebec, Canada
    MS-Off Ver
    Office 365
    Posts
    150

    looping through columns to find a value then replace said value with column header

    Hello Forum,

    I have a worksheet with values from column A to AK, and an undetermined number of rows yet.

    I need a code to do this : loop through columns B to AK, and, for each cell containing the value 1, then replace 1 with the header of the column in which the cell containing 1 is... (Headers are on the first row).

    I know how to do so but only with one column. Can you help me improve my code?

    Thank you so much in advance.

    Sub Loop_Column()
    
    Dim rngCell As Range
    
    For Each rngCell In ActiveSheet.Range("d:d")
        If rngCell.Value = 1 Then
             rngCell.Value = Range("d1").Value
        End If
        
      Next rngCell
    End Sub

  2. #2
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 365
    Posts
    8,049

    Re: looping through columns to find a value then replace said value with column header

    Try:
    Sub InsertHeader()
        Application.ScreenUpdating = False
        Dim x As Long
        For x = 2 To 37
            Columns(x).Replace "1", Cells(1, x)
        Next x
        Application.ScreenUpdating = True
    End Sub
    You can say "THANK YOU" for help received by clicking the Star symbol at the bottom left of the helper's post.
    Practice makes perfect. I'm very far from perfect so I'm still practising.

  3. #3
    Forum Contributor
    Join Date
    10-09-2018
    Location
    Quebec, Canada
    MS-Off Ver
    Office 365
    Posts
    150

    Re: looping through columns to find a value then replace said value with column header

    Awesome!

    Thank you Mumps1!

    Star symbol clicked!

  4. #4
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 365
    Posts
    8,049

    Re: looping through columns to find a value then replace said value with column header

    You are very welcome and thanks for the "Rep".

  5. #5
    Forum Expert
    Join Date
    10-10-2016
    Location
    Sheffield
    MS-Off Ver
    365 and rarely 2016
    Posts
    3,212

    Re: looping through columns to find a value then replace said value with column header

    I think to fully answer the question you may need the below amendment
    Sub InsertHeader()
        Application.ScreenUpdating = False
        Dim x As Long
        For x = 2 To 37
            Columns(x).Replace "1", Cells(1, x), xlWhole
        Next x
        Application.ScreenUpdating = True
    End Sub
    as 10 or 11 etc will be changed by the first bit of code

  6. #6
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 365
    Posts
    8,049

    Re: looping through columns to find a value then replace said value with column header

    @davsth: Thank you for your suggestion. I based the code on this line from the original post:
    If rngCell.Value = 1 Then
    which indicates that the value will always be "1".

  7. #7
    Forum Contributor
    Join Date
    10-09-2018
    Location
    Quebec, Canada
    MS-Off Ver
    Office 365
    Posts
    150

    Re: looping through columns to find a value then replace said value with column header

    Good point davsth, but I have only "1" in my spreadsheet. The other values are null or text.

    Thank you anyway for the hint.

+ 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. Find multiple column header, filter out other columns, and export data using a macro
    By trevtrev in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-18-2017, 06:24 PM
  2. Looping 'find' to find a value and replace a value in an adjacent column
    By Boltsie in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 09-12-2016, 06:54 PM
  3. Replies: 1
    Last Post: 04-30-2015, 10:01 AM
  4. Looping with find and replace
    By Monkihunta in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 03-23-2015, 04:50 PM
  5. looping Find and Replace over two columns
    By Monkihunta in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-21-2015, 04:52 PM
  6. Replies: 3
    Last Post: 11-25-2014, 06:08 AM
  7. [SOLVED] Search through columns and find specific text and replace that text with header column
    By adamzee in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 10-09-2014, 11:59 PM

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