VBA to find/replace information from one sheet to another

dominikapabis

New Member
Joined
Feb 9, 2016
Messages
4
Hello,

I am looking for help on writing a VBA that will replace information in one sheet using a legend that is in another sheet.

I need two things to happen.
1. replace cells in column C in Sheet1 with data from column C in Sheet2 if column C in Sheet1 = column A in Sheet2.
2. also insert the adjacent cell (Item Number) that corresponds with the new description.

Here is a simple sample of the spreadsheet.

Sheet 1 BEFORE Code:
1
ABC
2EQUIPMENT TYPEITEM NUMBERDESCRIPTION
3Actives NC400
4Full Spectrum EQ- Bank 1T2&4 CS12
5T2&4 CS09
6Full Spectrum EQ - Bank 2T8 CS09
7T8 CS06

<tbody>
</tbody>


Sheet 2 (KEY)
1
ABC
2Original DescriptionItem NumberNew Description
3
NC400​
5543
Full Features 2x2
4
T2&4 CS12​
5568Cable Simulator 12db
5
T2&4 CS09​
9956Cable Simulator 9db
6
T8 CS09​
5547Return Path 9db
7
T8 CS06​
6689Return Path 6db

<tbody>
</tbody>


Sheet 1 AFTER CODE:
1
ABC
2EQUIPMENT TYPEITEM NUMBERDESCRIPTION
3Actives 5543Full Features 2x2
4Full Spectrum EQ- Bank 15568Cable Simulator 12db
59956
Cable Simulator 9db
6Full Spectrum EQ - Bank 25547Return Path 9db
76689Return Path 6db

<tbody>
</tbody>


I know that I will have to use a series of loops, I would appreciate any help.

Thanks!`
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Hi, dominikapabis
You can try this

Code:
Sub TRY()
    Dim rr As Long
    Dim rs As Long
    Dim r As Range
    Dim Rng1 As Range
    Dim Rng2 As Range
    Dim aCell As Range
    Dim ws1 As Worksheet
    Dim ws2 As Worksheet

    Application.ScreenUpdating = False
    
    Set ws1 = Sheets("sheet1")
    Set ws2 = Sheets("sheet2")
    
    rr = ws1.Range("C" & Rows.count).End(xlUp).row
    rs = ws2.Range("A" & Rows.count).End(xlUp).row
 
    
    Set Rng1 = ws1.Range("C2:C" & rr)
    Set Rng2 = ws2.Range("A2:A" & rs)
    For Each r In Rng1
    
    Set aCell = Rng2.Find(What:=r, LookIn:=xlValues, _
                lookat:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
                MatchCase:=True, SearchFormat:=False)
    
    If Not aCell Is Nothing Then
        r.Value = aCell.Offset(0, 2).Value
        r.Offset(0, -1).Value = aCell.Offset(0, 1).Value
            
    End If
    Next
    
    Application.ScreenUpdating = True

End Sub
 
Upvote 0
dominikapabis,

Welcome to the MrExcel forum.

1. What version of Excel, and, Windows are you using?

2. Are you using a PC or a Mac?


Here is another macro solution for you to consider.

Sample raw data worksheets:


Excel 2007
ABC
1Original DescriptionItem NumberNew Description
2NC4005543Full Features 2x2
3T2&4 CS125568Cable Simulator 12db
4T2&4 CS099956Cable Simulator 9db
5T8 CS095547Return Path 9db
6T8 CS066689Return Path 6db
7
Sheet2



Excel 2007
ABC
1EQUIPMENT TYPEITEM NUMBERDESCRIPTION
2ActivesNC400
3Full Spectrum EQ- Bank 1T2&4 CS12
4T2&4 CS09
5Full Spectrum EQ - Bank 2T8 CS09
6T8 CS06
7
Sheet1


And, after the macro:


Excel 2007
ABC
1EQUIPMENT TYPEITEM NUMBERDESCRIPTION
2Actives5543Full Features 2x2
3Full Spectrum EQ- Bank 15568Cable Simulator 12db
49956Cable Simulator 9db
5Full Spectrum EQ - Bank 25547Return Path 9db
66689Return Path 6db
7
Sheet1


Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

1. Copy the below code
2. Open your NEW workbook
3. Press the keys ALT + F11 to open the Visual Basic Editor
4. Press the keys ALT + I to activate the Insert menu
5. Press M to insert a Standard Module
6. Where the cursor is flashing, paste the code
7. Press the keys ALT + Q to exit the Editor, and return to Excel
8. To run the macro from Excel press ALT + F8 to display the Run Macro Dialog. Double Click the macro's name to Run it.

Code:
Sub dominikapabis()
' hiker95, 02/09/2016, ME920278
Dim w1 As Worksheet, w2 As Worksheet
Dim r As Range, a As Range
Application.ScreenUpdating = False
Set w1 = Sheets("Sheet1")
Set w2 = Sheets("Sheet2")
With w1
  For Each r In .Range("C2", .Range("C" & Rows.Count).End(xlUp))
    Set a = w2.Columns(1).Find(r.Value, LookAt:=xlWhole)
    If Not a Is Nothing Then
      r.Value = w2.Cells(a.Row, 3).Value
      r.Offset(, -1).Value = w2.Cells(a.Row, 2).Value
    End If
  Next r
  .UsedRange.Columns.AutoFit
End With
Application.ScreenUpdating = True
End Sub

Before you use the macro with Excel 2007 or newer, save your workbook, Save As, a macro enabled workbook with the file extension .xlsm, and, answer the "do you want to enable macros" question as "yes" or "OK" (depending on the button label for your version of Excel) the next time you open your workbook.

Then run the dominikapabis macro.
 
Upvote 0
dominikapabis,

Thanks for the feedback.

You are very welcome. Glad I could help.

And, come back anytime.
 
Upvote 0

Forum statistics

Threads
1,214,834
Messages
6,121,876
Members
449,056
Latest member
ruhulaminappu

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