VBA code failing to delete the right item

kelly mort

Well-known Member
Joined
Apr 10, 2017
Messages
2,169
Office Version
  1. 2016
Platform
  1. Windows
freg2 contains name eg. "THE NAME"
freg3 contains a string say "GROUP 1", "GROUP 2". "GROUP 3" , ...., - it is a combobox
freg25 also contains a string just as freg3 eg "YEAR 1", "YEAR 2", "YEAR 3" - it is a textbox but will contain each of the strings "YEAR *"
freg26 will contain string like "2021/2022", etc.

The data starts from column A row 4.

1KELLY MORTGROUP 1MYEAR 12020/2021
1KELLY MORTGROUP 1MYEAR 22020/2021

So from the above sample, when I run my code, my aim was to delete the record for the YEAR 1 (The first item) from the data above.
But code code keeps deleting the second item instead - except there is only one record (year 1 only) that it deletes the right item for me.

I tried adding a third item (year 3) to check if the code will delete it (thinking the code was pointing to the last row) but it deleted the year 2 again.
can someone help me with the best way to get the job done?



Code:
          Sub DeleteIt()
                Dim cel As Range, sh As Worksheet, lr&
                Set sh = Sheets("Data")
                lr = sh.Range("A" & Rows.Count).End(xlUp).Row
                If lr < 4 Then lr = 4
                For Each cel In sh.Range("Z4:Z" & lr)
                    If cel = freg26 And cel.Offset(, -1) = freg25 And cel.Offset(, -23) = freg3 And cel.Offset(, -24) = freg2 Then
                        Set findvalue = sh.Range("A4:A" & lr).Find(what:=freg1, LookIn:=xlValues, lookat:=xlWhole)
                        
                        findvalue.Resize(, 29).ClearContents
                        sh.Range("A4:AC" & lr).Sort key1:=sh.[B4], order1:=xlAscending, Header:=xlNo
                        sh.Range("A4:AC" & lr).Sort key1:=sh.[C4], order1:=xlAscending, Header:=xlNo
                        Exit For
                    End If
                Next cel
            End Sub
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
the code has started doing what it was doing before. It is deleting other row instead of the specified row
Can you explain in more detail?
 
Upvote 0
Can you explain in more deta
What I have observed is that the code deletes the row that is active.
I have tried to vary the active rows manually and it seemed to work.

So with the After argument, how do I use it in my case to specify the active cell?

Maybe that could save me.

Thanks again.
 
Upvote 0
Code:
          Sub DeleteIt()
                Dim cel As Range, sh As Worksheet, lr&
                Set sh = Sheets("Data")
                lr = sh.Range("A" & Rows.Count).End(xlUp).Row
                If lr < 4 Then lr = 4
                For Each cel In sh.Range("Z4:Z" & lr)
                    If cel = freg26 And cel.Offset(, -1) = freg25 And cel.Offset(, -23) = freg3 And cel.Offset(, -24) = freg2 Then
                        Set findvalue =  cel.Offset(, -25)
                        
                        findvalue.Resize(, 29).ClearContents
                        sh.Range("A4:AC" & lr).Sort key1:=sh.[B4], order1:=xlAscending, Header:=xlNo
                        sh.Range("A4:AC" & lr).Sort key1:=sh.[C4], order1:=xlAscending, Header:=xlNo
                        Exit For
                    End If
                Next cel
            End Sub

I finally fixed it
 
Upvote 0
Solution

Forum statistics

Threads
1,214,649
Messages
6,120,730
Members
448,987
Latest member
marion_davis

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