How to loop thru an entire activesheet, search and remove a text and shift the remaining cells to the left

spidertech

New Member
Joined
Dec 20, 2012
Messages
9
Hi,

I need some help please. How do I search the entire active worksheet for a particular word, say "XYZ", remove/delete a text contains "XYZ" and be able to shift the remain of the values in that row one cell over to the left?
A1 A2 A3 A4 A5
XYZ B1 B2 B3 B4

Result:
A1 A2 A3 A4 A5
B1 B2 B3 B4
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
If your first row and Columns Have Data , Try this:
VBA Code:
Sub DeleteShiftLeft()
Dim i As Long, j As Long, Lr As Long, LC As Long
Dim Cell As Range, MyRange As Range
Lr = Cells(Rows.Count, 1).End(xlUp).Row
LC = Cells(1, Columns.Count).End(xlToLeft).Column
Set MyRange = Range(Cells(1, 1), Cells(Lr, LC))

For j = LC To 1 Step -1
For i = Lr To 1 Step -1
If Cells(i, j).Value = "XYZ" Then
Cells(i, j).Delete (xlShiftToLeft)
End If
Next i
Next j
End Sub
 
Upvote 0
If your first row and Columns Have Data , Try this:
VBA Code:
Sub DeleteShiftLeft()
Dim i As Long, j As Long, Lr As Long, LC As Long
Dim Cell As Range, MyRange As Range
Lr = Cells(Rows.Count, 1).End(xlUp).Row
LC = Cells(1, Columns.Count).End(xlToLeft).Column
Set MyRange = Range(Cells(1, 1), Cells(Lr, LC))

For j = LC To 1 Step -1
For i = Lr To 1 Step -1
If Cells(i, j).Value = "XYZ" Then
Cells(i, j).Delete (xlShiftToLeft)
End If
Next i
Next j
End Sub
Thank you maabadi. It works.
 
Upvote 0

Forum statistics

Threads
1,223,246
Messages
6,170,999
Members
452,373
Latest member
TimReeks

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