+ Reply to Thread
Results 1 to 5 of 5

Code to loop through a column and change cell value based on list

Hybrid View

  1. #1
    Registered User
    Join Date
    05-05-2011
    Location
    Michigan
    MS-Off Ver
    Excel 2010
    Posts
    18

    Code to loop through a column and change cell value based on list

    See attachment. Column B has values that need to be updated based on a list of conversion data. I need code to loop through column B, search that value against a table on the conversion list tab, and update each cell to the new value. if the value in column B does not have a match in the list, return "-"

    Can be done?
    Attached Files Attached Files

  2. #2
    Registered User
    Join Date
    05-05-2011
    Location
    Michigan
    MS-Off Ver
    Excel 2010
    Posts
    18

    Re: Code to loop through a column and change cell value based on list

    Correction, If there is no matching value in the conversion table, it should leave the original value as-is

  3. #3
    Valued Forum Contributor MaczaQ's Avatar
    Join Date
    06-03-2011
    Location
    Poland
    MS-Off Ver
    Excel 2003 / XP
    Posts
    510

    Re: Code to loop through a column and change cell value based on list

    Hello,

    Please find working example in attached file.

    My solution is
    Private Sub CommandButton1_Click()
    'Author: MaczaQ
    'Thread: http://www.excelforum.com/excel-programming/792086-code-to-loop-through-a-column-and-change-cell-value-based-on-list.html
    
    Dim r As Integer
    r = 2
    
    
    Do Until Cells(r, "B").Value = ""
      Set f = Sheets("Conversion List").Range("A:A").Find(Cells(r, "B").Value)
      
      If Not f Is Nothing Then
          Cells(r, "B").Value = f.Cells(, 2).Value
      End If
      
      Set f = Nothing
      r = r + 1
    Loop
    End Sub
    Best Regards
    MaczaQ
    Attached Files Attached Files

  4. #4
    Valued Forum Contributor
    Join Date
    08-29-2011
    Location
    Mississauga, CANADA
    MS-Off Ver
    Excel 2010
    Posts
    503

    Re: Code to loop through a column and change cell value based on list

    try this and if so, mark the thread as SOLVED

    Sub ListUpdate()
    
    Dim oShtA As Worksheet
    Dim oShtB As Worksheet
    Dim i, n As Integer
    
    Set oShtA = ThisWorkbook.Worksheets("Values List")
    Set oShtB = ThisWorkbook.Worksheets("Conversion List")
    
    For i = 2 To oShtA.UsedRange.Rows.Count
    
        For n = 2 To oShtB.UsedRange.Rows.Count
            If oShtA.Range("B" & i) = oShtB.Range("A" & n) Then
                oShtA.Range("B" & i) = oShtB.Range("B" & n)
                Exit For
            End If
        Next
    Next
    
    End Sub

  5. #5
    Forum Expert
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2019
    Posts
    4,168

    Re: Code to loop through a column and change cell value based on list

    hi, davekippen, please check attachment, run code "test"
    Attached Files Attached Files

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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