VBA to look down column and over write cell with text from list

pete4monc

Board Regular
Joined
Jan 12, 2014
Messages
88
Office Version
  1. 365
Platform
  1. Windows
Hi all

I hope you could help a beginner.

I need the vba to look down column B on sheet1.
Find the material in the first cell that matches the material in the list on sheet2 and change (over write) the material in that first cell for the corresponding name in column B of sheet 2.
Sheet1 will change daily via copy and pasting the full sheet so I can not use vlookups.

Hope this all makes sense?

I have some code which works but it is very long winded and seems to take forever to run the longer the code gets.
I was wondering if the vba could lookup from a list on sheet two?
 

Attachments

  • Screenshot 2024-10-07 192620.png
    Screenshot 2024-10-07 192620.png
    29.9 KB · Views: 7
  • Screenshot 2024-10-07 192759.png
    Screenshot 2024-10-07 192759.png
    13.8 KB · Views: 7
  • Screenshot 2024-10-07 193013.png
    Screenshot 2024-10-07 193013.png
    66.3 KB · Views: 7

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
VBA Code:
Sub Change_All()
    Dim cell As Range
    Dim Found As Range
    With Sheets("Sheet2")
        For Each cell In .Range("A2", .Range("A" & Rows.Count).End(xlUp))
            Set Found = Sheets("Sheet1").Range("B:B").Find(cell.Value, Sheets("Sheet1").Range("B1"), xlValues, xlWhole, 1, 1, 0)
            If Not Found Is Nothing Then Found.Value = cell.Offset(0, 1).Value
        Next cell
    End With
    MsgBox "Changes complete.", vbInformation, "Change All"
End Sub
 
Last edited:
Upvote 0
VBA Code:
Sub Change_All()
    Dim cell As Range
    Dim Found As Range
    With Sheets("Sheet2")
        For Each cell In .Range("A2", .Range("A" & Rows.Count).End(xlUp))
            Set Found = Sheets("Sheet1").Range("B:B").Find(cell.Value, Sheets("Sheet1").Range("B1"), xlValues, xlWhole, 1, 1, 0)
            If Not Found Is Nothing Then Found.Value = cell.Offset(0, 1).Value
        Next cell
    End With
    MsgBox "Changes complete.", vbInformation, "Change All"
End Sub
Hi AlphaFrog.

Thanks for your help.
All seems to work but it will only change the first instance of the text and does not run down the whole B column on Sheet1.
 

Attachments

  • 1.png
    1.png
    23.2 KB · Views: 0
Upvote 0
Try this with a copy of your data

VBA Code:
Sub Replace_Values()
  Dim c As Range
  
  Application.ScreenUpdating = False
  For Each c In Sheets("Sheet2").Range("A1", Sheets("Sheet2").Range("A" & Rows.Count).End(xlUp))
    Sheets("Sheet1").Columns("B").Replace What:="*" & c.Value & "*", Replacement:=c.Offset(, 1).Value
  Next c
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution
Try this with a copy of your data

VBA Code:
Sub Replace_Values()
  Dim c As Range
 
  Application.ScreenUpdating = False
  For Each c In Sheets("Sheet2").Range("A1", Sheets("Sheet2").Range("A" & Rows.Count).End(xlUp))
    Sheets("Sheet1").Columns("B").Replace What:="*" & c.Value & "*", Replacement:=c.Offset(, 1).Value
  Next c
  Application.ScreenUpdating = True
End Sub
Thank you Peter_SSs.

This works fantastic.
 
Upvote 0
You're welcome. Thanks for the follow-up. :)
 
Upvote 0

Forum statistics

Threads
1,222,567
Messages
6,166,835
Members
452,077
Latest member
hufflefry

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