Remove leading characters & delete adjacent cells

stuartgb100

Active Member
Joined
May 10, 2015
Messages
322
Office Version
  1. 2021
Platform
  1. Windows
Hi,
I have approx 5,000 rows of data that need attention.
Data is in col E and looks like this:
Folder PATH listing for volume DATA
Volume serial number is 349F-83B7
H:.
output.doc

\---NAS Archive (Music - Artists 1)
+---10cc
+---20th Century Masters The Millennium Collection Best Of 10CC
01_Life Is A Minestrone.flac
02_Channel Swimmer.flac
02_Channel Swimmer.lrc
03_I'm Not In Love.flac
03_I'm Not In Love.lrc

I'd like a vba script that will :
a) act on col E in single worksheet
b) if the data starts with \---, then remove those characters and delete the three cells to the left
c) if the data starts with +---, then remove those characters and delete the two cells to the left
d) if the data ends with .Irc, then delete the entire row

Any help much appreciated.
Many thanks
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
You actually want to delete cells, or just delete (clear) their contents? If you mean to delete then I think you have to specify which way you want the adjacent cells to shift.
 
Upvote 0
Assuming you only want to clear contents, maybe this:
VBA Code:
Sub formTest()
Dim lngRow As Long, i As Long
Dim rng As Range, rng2 As Range

On Error GoTo errHandler
lngRow = Cells.Find(What:="*", After:=Range("A1"), LookAt:=xlPart, LookIn:=xlFormulas, _
         SearchOrder:=xlByRows, SearchDirection:=xlPrevious, MatchCase:=False).Row

For i = 1 To lngRow
    Set rng = Range("E" & i)
    Select Case True
        Case InStr(rng, ".lrc") > 0
            rng.EntireRow.Delete
        Case InStr(rng, "\") > 0
            rng = Replace(rng, "\", "")
            Set rng2 = Range(rng.Address).Offset(0, -3)
            Range(rng2.Address & ":" & rng2.Offset(0, 2).Address).ClearContents
            'Sheet2.Range(r.Address & ":" & r.Offset(rowOffset:=8).Address).ClearContents
        Case InStr(rng, "+") > 0
            rng = Replace(rng, "+", "")
            Set rng2 = Range(rng.Address).Offset(0, -2)
            Range(rng2.Address & ":" & rng2.Offset(0, 1).Address).ClearContents
    End Select
Next

exitHere:
Set rng = Nothing
Application.EnableEvents = True
Exit Sub

errHandler:
MsgBox "Error " & Err.Number & ": " & Err.Description
Resume exitHere

End Sub
Before:
1720975703993.png


After
1720975738122.png
 
Upvote 0
Hi Micron,
Many thanks.
Apologies, I should have been clearer !
I'd like to delete the cells to the left.

Many thanks.
Regards.
 
Upvote 0
Then change the clearcontents lines to
Range(rng2.Address & ":" & rng2.Offset(0, 2).Address).Delete Shift:=xlToLeft
(watch out for the offset numbers - don't change them).
You might find this causes issues because data to the right will shift left. This might cause all sorts of problems, such as Spill errors, formatting errors, calculations errors, etc. as things move around. Also, E column needs to be formatted as text, which I suppose it already is. You'd best test this on a wb copy.
 
Upvote 0
Solution
Glad I could help, & thanks for the recognition.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,173
Members
453,021
Latest member
Justyna P

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