VBA code to search for a range of percentages and delete corresponding cells

butternaut

New Member
Joined
Nov 20, 2016
Messages
3
Hello,


I have an Excel sheet for students' marks. I need to remove the class(es) and mark(s) that are passing (mark of 50-100%). Here is how my Excel sheet is set up:


[TABLE="width: 500"]
<tbody>[TR]
[TD]A1[/TD]
[TD]B1[/TD]
[TD]C1[/TD]
[TD]D1[/TD]
[TD]E1[/TD]
[TD]F1[/TD]
[TD]G1[/TD]
[TD]H1[/TD]
[TD]I1[/TD]
[TD]J1[/TD]
[TD]K1[/TD]
[/TR]
[TR]
[TD]John Smith[/TD]
[TD]English[/TD]
[TD]40%[/TD]
[TD]Math[/TD]
[TD]60%[/TD]
[TD]Science[/TD]
[TD]20%[/TD]
[TD]Art[/TD]
[TD]70%[/TD]
[TD]History[/TD]
[TD]80%[/TD]
[/TR]
</tbody>[/TABLE]




John is failing 2 classes (mark of 49% or less). I want to be able to remove the other 3 classes he is passing (along with the grade for that class) and just leave the failing class's names and respective failing marks.


Ideally, I would also like to be able to remove the entire row for any students who are not failing anything, but I can live without it.


If anyone has any thoughts, I'd greatly appreciate it.


Thanks very much in advance!
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
Assuming you have headers in row 1 in all used columns and your data starts in row 2, try this macro:
Code:
Sub butternaut()
    Application.ScreenUpdating = False
    Dim LastRow As Long
    LastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    Dim lColumn As Long
    lColumn = ActiveSheet.Cells(1, Columns.Count).End(xlToLeft).Column
    Dim counter As Boolean
    counter = True
    Dim x As Long
    Dim y As Long
    Dim rng As Range
    For x = LastRow To 2 Step -1
        For y = 3 To lColumn Step 2
            If Cells(x, y) < 0.5 Then
                Range(Cells(x, y - 1), Cells(x, y)).ClearContents
            End If
        Next y
    Next x
    For x = LastRow To 2 Step -1
        For y = 3 To lColumn Step 2
            If Cells(x, y) < 0.5 Then
                counter = False
                Exit For
            End If
        Next y
        If counter = True Then
            Rows(x).EntireRow.Delete
        End If
    Next x
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Assuming you have headers in row 1 in all used columns and your data starts in row 2, try this macro:
Code:
Sub butternaut()
    Application.ScreenUpdating = False
    Dim LastRow As Long
    LastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    Dim lColumn As Long
    lColumn = ActiveSheet.Cells(1, Columns.Count).End(xlToLeft).Column
    Dim counter As Boolean
    counter = True
    Dim x As Long
    Dim y As Long
    Dim rng As Range
    For x = LastRow To 2 Step -1
        For y = 3 To lColumn Step 2
            If Cells(x, y) < 0.5 Then
                Range(Cells(x, y - 1), Cells(x, y)).ClearContents
            End If
        Next y
    Next x
    For x = LastRow To 2 Step -1
        For y = 3 To lColumn Step 2
            If Cells(x, y) < 0.5 Then
                counter = False
                Exit For
            End If
        Next y
        If counter = True Then
            Rows(x).EntireRow.Delete
        End If
    Next x
    Application.ScreenUpdating = True
End Sub

Hi mumps,

I really appreciate you helping me out, but unfortunately, it's backwards... it removes the failing classes, and I need it to remove the passing classes. Could you please let me know what I should change?

Thanks!!
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,848
Members
452,361
Latest member
d3ad3y3

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