Delete columns based on variable criteria applied to variable row values

Pigankle

New Member
Joined
Apr 18, 2011
Messages
32
My First Macro-enabled Worksheet/VBA Project:

I have a table with seven header rows, 500 data rows and 1 header column. There will be roughly 40 data columns, although that may vary slightly. The four header rows contain a year and various summary statistics for the data in the corresponding column.

I want a function that has three arguments:
1. a sheetname (because I actually have several tables like the one I described above)
2. A criteria that will be used on a single row (e.g. " < 1954" or maybe ">4.5 AND <6")
2 A string that codes to the header row I want to apply the criteria to.

Here's what I have so far:
Code:
Public Sub DeleteColumnsFromWithOn(sheetname, strCriteria, rowType)

Dim xlCalc As XlCalculation

    On Error Resume Next

 'Get myself on the correct worksheet

     Application.Goto Reference:=Worksheets(sheetname).Range("A:BM"), Scroll:=True

' Use the rowType argument to determine which row to apply criteria to.    
    If rowType = "ptile" Then   rRow = 2
    ElseIf rowType = "year" Then   rRow = 4
    ElseIf (rowType = "fcast") Then rRow = 7
    Else: Exit Sub
    End If
    
'Some of the header rows are percentile values for other header rows.
' since I want to filter against percentiles of the whole collection, 
' I want to turn off calculations as I go (If I delete the column with the '
' largest 99th percentile value, I don't want to recalculate percentiles and 
' get a new matching column.

    'Turn off events and screen updating
    With Application
        xlCalc = .Calculation
        .Calculation = xlCalculationManual
        .EnableEvents = False
        .ScreenUpdating = True
    End With


' The actual filter/deletion process begins
'I wanted to use autofilter here, since all of the row-deleting examples I 
' could find use it, but it seems to only work for filtering out rows based on 
' column values, not the other way around.  Instead:

'I choose a number greater than the number of columns I will ever have
For c = 50 To 2 Step -1
  '******I THINK THIS IS THE STEP NEEDING HELP********
  ' I want to test with my criteria -  What is the syntax?
  If Cells(rRow, c) '****??????????
    Then Cells(rRow, c).EntireColumn.Delete
Next

'Revert back to old behavior
With Application
        .Calculation = xlCalc
        .EnableEvents = True
'        .ScreenUpdating = True
    End With
   On Error GoTo 0
End Sub
Is this basically a reasonable approach? If so, can anyone help me with applying my criteria to the value in the appropriate row as I loop through the columns?

Thanks,

Adam
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
After poking around, I think that BuildCriteria (the Access function) would be very helpful. Is there an equivalent for excel?
 
Upvote 0
Hrmm...based on further reading, it sounds like this is a difficult issue. Perhaps it would be easier to transpose the whole spreadsheet, use autofilter, then transpose back. Is expression.transpose the most efficient method?
 
Upvote 0

Forum statistics

Threads
1,224,599
Messages
6,179,828
Members
452,946
Latest member
JoseDavid

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