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
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