Excel AutoFilter range set by combo boxes?

2019-07-14 02:14发布

I am currently working on a bear of a worksheet that is designed to show the change of individual parameters over the same time interval using the Backdoor technique detailed at http://peltiertech.com/excel/chartshowto/panelunevenscales.html.

Basically, I have it set up as a template where 5 columns of data (which vary in length of rows) can be pasted into columns A-F starting at row 12. The time column that I would like to filter is in column A and I basically want to make it so that 2 combo boxes (ComboBox1 and ComboBox2) can be used to set an upper and lower time limit and filter out any other data. I'm hoping I can turn this into a dynamic chart that will resize the x-axis to show only the selected time period.

I'm still relatively new to using VBA and I did a good bit of searching and the only thing I could find that seemed related was the question answered at http://www.ozgrid.com/forum/showthread.php?t=54376. I tried the following code and kept getting errors:

Sub TimeFilter()

Crit1 = Format(ComboBox1, "0")

Crit2 = Format(ComboBox2, "0")

.AutoFilterMode = False

.Range("A12:L10000").AutoFilter

.Range("A12:L10000").AutoFilter Field:=1, Criteria:=">=" & crit1, Operator:=xlAnd, Criteria2:="<=" & crit2

End Sub

Any ideas on why it might not be working? Any help or guidance is appreciated!

****EDIT****

Here is the updated FULL code for the spreadsheet I'm using, and with the TimeFilter script I get a "Run-time error '1004', AutoFilter method of Range class failed": **The time that I am filtering by is in 0.2 second intervals with the format MM:SS.X.

Sub CreateComboBoxes()

Dim Cell As Range
Dim sht As Worksheet

Set sht = ThisWorkbook.Worksheets("Sheet1")

    Set Cell = Range("I8")  
    With Cell
        sht.DropDowns.Add(.Left, .Top, .Width, .Height).Name = "ComboBox1"
    End With

    Set Cell = Range("K8")    
    With Cell
        sht.DropDowns.Add(.Left, .Top, .Width, .Height).Name = "ComboBox2"
    End With

End Sub

Sub TimeFilter()

Crit1 = Format(ComboBox1, "0")
Crit2 = Format(ComboBox2, "0")

Worksheets("Sheet1").AutoFilterMode = False
Worksheets("Sheet1").Range("A12:L10000").AutoFilter
Worksheets("Sheet1").Range("A12:L10000").AutoFilter Field:=1,Criteria1:=">=" & Crit1, Operator:=xlAnd, _
Criteria2:="<=" & Crit2

End Sub

1条回答
可以哭但决不认输i
2楼-- · 2019-07-14 03:01

I preface this by stating that this is not an answer but an explanation in the use of With, End With statement. The comment section does not offer the formatting tools required to do so properly

The With, End With statements save you on typing the same bits over and over.

    ThisWorkbook.Worksheets("Sheet1").UsedRange.Font.ColorIndex = 3
    ThisWorkbook.Worksheets("Sheet1").UsedRange.Font.Bold = True
    ThisWorkbook.Worksheets("Sheet1").UsedRange.Font.Size = 15
    ThisWorkbook.Worksheets("Sheet1").UsedRange.Font.Name = "Times new Roman"

Can also be written as

    With ThisWorkbook.Worksheets("Sheet1").UsedRange.Font
        .ColorIndex = 3
        .Bold = True
        .Size = 15
        .Name = "Times new Roman"
    End With

You'll notice the "." in front of the lines in between the with end with statement, this means it should be read as a continuation of what is set by the With statement.

查看更多
登录 后发表回答