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:


A1B1C1D1E1F1G1H1I1J1K1
John SmithEnglish40%Math60%Science20%Art70%History80%

<tbody>
</tbody>




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

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
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,216,101
Messages
6,128,842
Members
449,471
Latest member
lachbee

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