VBA: Different requirements when coding

你。 提交于 2019-12-25 07:02:31

问题


This is a follow up question from a previous post.

So the company I work for has recently updated their Excel from 2003 to 2013. I am now having issues with some pretty basic VBA code. The line Cells.AutoFilter(x, y) in particular is giving me issues.


I wrote a very ugly program months ago which looks something like this:

...
ActiveSheet.ListObjects("Table1").Range.AutoFilter Field:=11, Criteria1:= _
    "0"
If wf.CountA(r) > 0 Then
ActiveSheet.ListObjects("Table1").Range.AutoFilter Field:=8, Criteria1:= _
    Array("baseunitprice", "burden", "MTLBURRATE", "PurPoint", "Vendornum"), Operator _
    :=xlFilterValues
Range("K2:K50000").SpecialCells(xlCellTypeVisible).Select
ActiveCell.FormulaR1C1 = "MACROUSE"
Range(Selection, Selection.End(xlDown)).Select
Selection.FillDown
ActiveSheet.ListObjects("Table1").Range.AutoFilter Field:=8
...

This is the first program I wrote and must be rewritten for obvious reasons.


In an attempt to mirror the above code in a more elegant, readable way, I created another Sub in the same module:

Sub ActualProgram()

Dim firstRow As Integer
Dim lastRow As Integer
Dim firstCol As Integer
Dim lastCol As Integer
Dim allRange As Range
Dim vRange As Range
Dim bRange As Range
Dim commentsCol As Integer
Dim commentsColRng As Range
Dim fieldNameCol As Integer
Dim userCol As Integer

If Cells(2, 1) <> "" Then

    DeleteEmptyRows

    firstCol = 1
    lastCol = Cells(1, Columns.Count).End(xlToLeft).Column
    firstRow = 2
    lastRow = Cells(Rows.Count, "A").End(xlUp).Row

    commentsCol = Rows(1).find("Comments").Column '11
    fieldNameCol = Rows(1).find("Field Name").Column '8
    userCol = Rows(1).find("User").Column '4

    Set allRange = Range(Cells(firstRow, firstCol), Cells(lastRow, lastCol))
    Set commentsColRng = Range(Cells(firstRow, commentsCol), Cells(lastRow, commentsCol))

    ActiveSheet.ListObjects("Table1").Range.AutoFilter 11, "0"     'WORKS
    Cells.AutoFilter commentsCol, "0"                              'FAILS

    Call MarkFieldNames(fieldNameCol, commentsColRng)

    Call MarkNonSMFields(commentsColRng)

    Call TargetFieldNames(fieldNameCol, commentsCol)

End If

End Sub

This sub is never called in the previous program, of course. I just wanted to have both codes together so I could refer to the previous one while writing the new one.

The line I'm having issues with in the new code is Cells.AutoFilter commentsCol, "0".

The line which I used in the old code is ActiveSheet.ListObjects("Table1").Range.AutoFilter Field:=11, Criteria1:="0"

The old code still works fine. The new one throws an AutoFilter method of Range class failed run-time error. In my eyes, these two lines do the exact same thing, and I've used the line Cells.AutoFilter(x, y) too many times to count without error on Excel 2013.


Is there a setting I need to change? I ask because I see in VBA > Tools > Options > Editor there are Code Settings options such as Require Variable Declaration, which leads me to belive that there may be a setting which disables the way I call the AutoFilter() method.


Thank you for your time.


回答1:


I am working with a table. Hence ActiveSheet.ListObjects("Table1")... Tables are treated differently than regular cells, and therefore, must be specifically targeted when autofiltering.



来源:https://stackoverflow.com/questions/31747522/vba-different-requirements-when-coding

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!