VBA code to find a value in a column then clear contents of certain cell

Mazkot1

New Member
Joined
Jun 14, 2023
Messages
2
Office Version
  1. 2019
Platform
  1. Windows
I'm looking for help on creating a vba code to look up a value based on a cell on another sheet then clear certain cells from that row. Below is an example.

I would like to type a value in cell (A2) in the 'Find" worksheet. Then run VBA code that would search column A of the "Yards" worksheet for that value and then clear the contents of cells A through E only of that row. Each value in column A would be unique.

Many thanks.
 

Attachments

  • find.jpg
    find.jpg
    17.5 KB · Views: 24
  • Yards.jpg
    Yards.jpg
    70 KB · Views: 22

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Copy and paste this macro into the worksheet code module. Do the following: right click the tab name for your "Find" sheet and click 'View Code'. Paste the macro into the empty code window that opens up. Close the code window to return to your sheet. Enter a value in cell A2 and press the ENTER key.

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.CountLarge > 1 Then Exit Sub
    If Target.Address(0, 0) <> "A2" Then Exit Sub
    Application.ScreenUpdating = False
    Dim fnd As Range
    Set fnd = Sheets("Yard").Range("A:A").Find(Target.Value, LookIn:=xlValues, lookat:=xlWhole)
    If Not fnd Is Nothing Then
        Sheets("Yard").Range("A" & fnd.Row).Resize(, 5).ClearContents
    End If
    Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,906
Members
452,366
Latest member
TePunaBloke

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