row count for visible cells only

ε祈祈猫儿з 提交于 2019-12-12 05:39:43

问题


I want to create a macro that will fill in blank cells with a formula. The formula will reference the cell above, but only after I apply Subtotal to my data, as well as only on the rows with a Total (see below Sample screenshot):

So far my macro will apply the subtotal, then filter on the column with the totals and filter for anything with Total in the cell. Afterwards it counts all the invisible rows and subtracts 2 (I do not want to count the header and grand total). It takes the count and loops the formula application.

That said, it works the majority of the time, but every so often it miscounts by 2 and I can't figure out why.

The data is formatted the same and the same columns are used each time.

I hope I explained my problem clearly and the sample and code snippet is enough:

'filling in empty cells on subtotal line
Cells.Select
    Selection.AutoFilter
    LastRow = Range("G" & Rows.Count).End(xlUp).Row
        x = LastRow
    ActiveSheet.Range("G1:G" & x).AutoFilter Field:=7, Criteria1:="=*total*", Operator:=xlAnd, Criteria2:="<>Grand Total", Operator:=xlAnd

Set rng = ActiveSheet.AutoFilter.Range
'-2 is to NOT count the header or Grand Total for my loop count
RowCount = rng.Columns(2).SpecialCells(xlCellTypeVisible).Count - 2
    r = RowCount

回答1:


The Problem is, that your range is not continues (because of the filtered cells). This stops rows.count to work properly. try this:

Number_of_Rows= rng.Resize(, 1).SpecialCells(xlCellTypeVisible).Count



回答2:


i noticed something in your code here that isnt right, i dont know if its a typo on this post or if its actually in your code..

ActiveSheet.Range("G1:G" & x).AutoFilter Field:=7, Criteria1:="=*total*", Operator:=xlAnd, Criteria2:="<>Grand Total", Operator:=xlAnd

i would imagine if this is running its likely going to freak out at some point. its got 2 'Operator:=xlAnd' which is strange i would expect an error.

This miscount could be a couple of things, its likely its just something simple that has been overlooked if its not caused by this bad filter part,

What is Rows.Count? could this possibly put the active cell(cursor if you like) in a position that when you use .End(xlUp) it might skip a couple of blank cells within the table? if this was the case it might filter but ignore a couple of rows giving extra rows remaining visible that shouldnt be.......possibly

RowCount = rng.Columns(2).SpecialCells(xlCellTypeVisible).Count - 2

this is definitely of interest because your problem is 2 rows sometimes miscounted and your explicitly taking away 2 rows here (-2), but i doubt this is the cause although its likely the reason its always 2, so it must be in the code above........

You also need to check the data in your table when this happens, and post it if possible, its really not easy to pinpoint whats happening here without the real data.

just post back if you can recreate the problem or post some real data, it will be a great help.




回答3:


Thanks Doktor but I looked around some more and found this solution that worked for my problem, it always counts the correct number to loop.

    Dim LastRow as interger, x as integer

    'add autofilter
    Cells.Select
    Selection.AutoFilter
    'filter for Total
    ActiveSheet.Range("G1:G" & x).AutoFilter Field:=7, Criteria1:="=*total*", Operator:=xlAnd, Criteria2:="<>Grand Total", Operator:=xlAnd

    'find last row
    LastRow = Range("G" & Rows.Count).End(xlUp).Row
       x = LastRow

    'count visible rows
    RowCount = Range("G1:G" & x).Rows.SpecialCells(xlCellTypeVisible).Count - 2
       r = RowCount


来源:https://stackoverflow.com/questions/36357806/row-count-for-visible-cells-only

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