VBA search and delete

Manolocs

Active Member
Joined
Mar 28, 2008
Messages
340
Hello,
I have a cell with a name on it,
Is there a possibility to look for in a table in other sheet
same workbook, find that name in the first column and then
clear a range of column in this row?
Is like a vlookup but clear the 5 columns in the line.
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.

xenou

MrExcel MVP
Joined
Mar 2, 2007
Messages
16,836
Office Version
  1. 2019
Platform
  1. Windows
One try:
Code:
Sub DeleteMatches()
Dim wsLookOnSheet As Worksheet
Dim ans As Integer
Dim msg As String
Dim c As Range

Set wsLookOnSheet = Worksheets("Sheet2") 'Change to actual sheet name as needed


msg = "Value to find: " & ActiveCell.Value & vbCrLf
msg = msg & "Look on sheet: " & wsLookOnSheet.Name & vbCrLf & vbCrLf
msg = msg & "Continue?"
ans = MsgBox(msg, vbYesNo)

If ans = vbYes Then
    With wsLookOnSheet
        Set c = .Cells.Find(What:=ActiveCell.Value)
            If Not c Is Nothing Then
                .Cells(c.Row, 1).Resize(1, 5).ClearContents
                MsgBox "Cells cleared in range " & .Cells(c.Row, 1).Resize(1, 5).Address
            Else
            MsgBox "No match was found."
            End If
    End With
End If

End Sub
 
Upvote 0

VoG

Legend
Joined
Jun 19, 2002
Messages
63,650
Try this. It assumes that the name to find is in A1 of Sheet1 and clears the contents of 5 columns on Sheet2.

Code:
Sub Clr()
Dim ToFind As String, Found As Range
ToFind = Sheets("Sheet1").Range("A1").Value
With Sheets("Sheet2")
    Set Found = .Cells.Find(what:=ToFind)
    If Not Found Is Nothing Then Range(Found, Found.Offset(0, 4)).ClearContents
End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,191,584
Messages
5,987,490
Members
440,097
Latest member
Wint

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
Top