Finding row adress and passing it to countif function

LopezGG

New Member
Joined
Apr 12, 2011
Messages
1
I have the following code which basically runs through an excel sheet and gives me the number of zeros in each column . I want to modify the code

I also want to get the number of non-zeros in the first column and zeros in the second column. In other words,

I have to filter zeros for column 2 and find find out the number of non zero rows in column1
similarly, I have to filter zeros for column 3 and find out the number of non zeros in column 1 and so on .. the column 1 stays same all the time.

Could you please help me modify the code

Code:
Option Explicit
Public Sub TallyZerosOnActiveSheet()

   Dim SourceSh As Worksheet
   Dim CurrentCountRange As Range
   Dim ETOHCountRange As Range
   Dim ZeroCount As Integer
   Dim ETOHcount As Integer
   Dim NextRow As Long
   Dim iRow As Long
   Dim ColWithHdr
   
    ' CONFIG HERE !!
   Const analysisSh As String = "Analysis"
   Const SrcHdrRow As Integer = 1
   
   
   'Set Source Sheet
   Set SourceSh = ActiveSheet
   
   'Make sure Anlaysis Sheet Exist
   If Not SheetExists(analysisSh) Then
      Sheets.Copy after:=ActiveSheet
      ActiveSheet.Name = analysisSh
   End If
   
   With SourceSh
       ' Cycle thru all columns that have header
      For Each ColWithHdr In .Rows(SrcHdrRow & ":" & SrcHdrRow).SpecialCells(xlCellTypeConstants, 2)
         
         ' Set range to count
         Set CurrentCountRange = .Cells(1, ColWithHdr.Column).EntireColumn
         Set ETOHCountRange = .Cells(1, 1).EntireColumn
            
         ' Count zeros
         ZeroCount = Application.WorksheetFunction.CountIf(CurrentCountRange, 0)

                
         ' Log Zeros
         With Sheets(analysisSh)
            NextRow = .Cells(.Rows.Count, 1).End(xlUp).Row + 1
            .Cells(NextRow, 1).Value = ColWithHdr.Value
            .Cells(NextRow, 2).Value = ZeroCount
            .Cells(NextRow, 3).Value = ETOHcount
         End With
         
      Next ColWithHdr
   End With
End Sub


Private Function SheetExists(sname) As Boolean
'   Returns TRUE if sheet exists in the active workbook
    Dim x As Object
    On Error Resume Next
    Set x = ActiveWorkbook.Sheets(sname)
    If Err = 0 Then SheetExists = True _
        Else SheetExists = False
End Function
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!

Forum statistics

Threads
1,215,584
Messages
6,125,678
Members
449,248
Latest member
wayneho98

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