简体   繁体   中英

Loop using CountIf for text then moving to next column

I have one worksheet (Daily) with data in columns and a header. The data is dynamic based on the number of days in a month and it has been conditionally formatted to icons (Incomplete = T, Complete = R). I'm trying to create a chart on another worksheet (WS2) to show the percent complete and incomplete for each item (header).

I have a table on a 3rd worksheet (WS3) to count all non-empty cells in each column, countif "T", and countif "R". (eg. =COUNTIF(Daily!D5:D29,"R")). The chart on WS1 selects data from WS3 for each item. Sometimes I have to add or delete a column on the Daily page. I'm trying to write a sub to loop through each column on Daily using CountIF and CountA and show the results on WS2.

Sub LoopforCt()

Dim ws As Worksheet
Dim Daily As Worksheet
Dim Test As Worksheet
Dim AllData As Variant
Dim pcv As Range

Set Daily = Sheets("Daily")
Set Test = Sheets("Test")
Set AllData = Daily.Range("B5:B100")
Set pcv = Test.Range("A22")

With AllData
 pcv = "=COUNTIF(Daily!C5:C29,""R"")"
End With

End Sub

I've been able to get the countif to work for the first column, but I'm lost on how to put this in a loop. I want to continue to CountIf for the next column's results in Test.Range("B22"), etc.

Thank you!

You don't need a loop. You can write a formula to a Range in one line. You're using relative references, so the C5:C29 will become D5:D29 , E5:E29 as you progress right.

Test.Range("A22:X22").Formula = "=COUNTIF(Daily!C5:C29,""R"")" ' replace X with whatever column you need.

Or if you prefer to keep pcv ,

Set pcv = Test.Range("A22:X22")
pcv.Formula = "=COUNTIF(Daily!C5:C29,""R"")"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM