Please help me on creating a macro which search multiple sheets in a workbook with a particular column name and display the count of values in that column and display in a new sheet with sheet name and the count.
Here is my code,
Sub Click()
Dim sh As Worksheet, ws As Worksheet, LstRw As Long, x, s As String
Dim rng1 As Range
Set ws = Sheets.Add
ws.Name = "Report"
s = "A"
For Each sh In Sheets
If sh.Name <> ws.Name Then
With sh
x = Application.WorksheetFunction.CountA(.Range("A2:A100"))
With ws
LstRw = .Cells(.Rows.Count, "A").End(xlUp).Row + 1
.Cells(LstRw, 1) = sh.Name
.Cells(LstRw, 2) = x
End With
End With
End If
Next sh
End Sub
But it doesn't workout. I have 3 sheets A, B & C and i want to search only the column name "XYZ". I have the data as below,
Sheet A Sheet B Sheet C
[TABLE="width: 320"]
<colgroup><col width="64" span="5" style="width:48pt"> </colgroup><tbody>[TR]
[TD="class: xl63, width: 64"]XYZ[/TD]
[TD="width: 64"][/TD]
[TD="class: xl63, width: 64"]XYZ[/TD]
[TD="width: 64"][/TD]
[TD="class: xl63, width: 64"]XYZ[/TD]
[/TR]
[TR]
[TD="class: xl64"]1418780[/TD]
[TD][/TD]
[TD="class: xl64"]1418780[/TD]
[TD][/TD]
[TD="class: xl64"]1418780[/TD]
[/TR]
[TR]
[TD="class: xl64"]1438095[/TD]
[TD][/TD]
[TD="class: xl64"] [/TD]
[TD][/TD]
[TD="class: xl64"]1438095[/TD]
[/TR]
[TR]
[TD="class: xl64"]1643542[/TD]
[TD][/TD]
[TD="class: xl64"]1643542[/TD]
[TD][/TD]
[TD="class: xl64"] [/TD]
[/TR]
[TR]
[TD="class: xl64"]1309082[/TD]
[TD][/TD]
[TD="class: xl64"]1309082[/TD]
[TD][/TD]
[TD="class: xl64"] [/TD]
[/TR]
</tbody>[/TABLE]
and my result should look like,
[TABLE="width: 128"]
<colgroup><col width="64" span="2" style="width:48pt"> </colgroup><tbody>[TR]
[TD="class: xl65, width: 64"]Sheet Name[/TD]
[TD="class: xl65, width: 64"]Count[/TD]
[/TR]
[TR]
[TD="class: xl65"]A[/TD]
[TD="class: xl65"]4[/TD]
[/TR]
[TR]
[TD="class: xl65"]B[/TD]
[TD="class: xl65"]3[/TD]
[/TR]
[TR]
[TD="class: xl65"]C[/TD]
[TD="class: xl65"]2
[/TD]
[/TR]
</tbody>[/TABLE]
Could anyone help on this.
Here is my code,
Sub Click()
Dim sh As Worksheet, ws As Worksheet, LstRw As Long, x, s As String
Dim rng1 As Range
Set ws = Sheets.Add
ws.Name = "Report"
s = "A"
For Each sh In Sheets
If sh.Name <> ws.Name Then
With sh
x = Application.WorksheetFunction.CountA(.Range("A2:A100"))
With ws
LstRw = .Cells(.Rows.Count, "A").End(xlUp).Row + 1
.Cells(LstRw, 1) = sh.Name
.Cells(LstRw, 2) = x
End With
End With
End If
Next sh
End Sub
But it doesn't workout. I have 3 sheets A, B & C and i want to search only the column name "XYZ". I have the data as below,
Sheet A Sheet B Sheet C
[TABLE="width: 320"]
<colgroup><col width="64" span="5" style="width:48pt"> </colgroup><tbody>[TR]
[TD="class: xl63, width: 64"]XYZ[/TD]
[TD="width: 64"][/TD]
[TD="class: xl63, width: 64"]XYZ[/TD]
[TD="width: 64"][/TD]
[TD="class: xl63, width: 64"]XYZ[/TD]
[/TR]
[TR]
[TD="class: xl64"]1418780[/TD]
[TD][/TD]
[TD="class: xl64"]1418780[/TD]
[TD][/TD]
[TD="class: xl64"]1418780[/TD]
[/TR]
[TR]
[TD="class: xl64"]1438095[/TD]
[TD][/TD]
[TD="class: xl64"] [/TD]
[TD][/TD]
[TD="class: xl64"]1438095[/TD]
[/TR]
[TR]
[TD="class: xl64"]1643542[/TD]
[TD][/TD]
[TD="class: xl64"]1643542[/TD]
[TD][/TD]
[TD="class: xl64"] [/TD]
[/TR]
[TR]
[TD="class: xl64"]1309082[/TD]
[TD][/TD]
[TD="class: xl64"]1309082[/TD]
[TD][/TD]
[TD="class: xl64"] [/TD]
[/TR]
</tbody>[/TABLE]
and my result should look like,
[TABLE="width: 128"]
<colgroup><col width="64" span="2" style="width:48pt"> </colgroup><tbody>[TR]
[TD="class: xl65, width: 64"]Sheet Name[/TD]
[TD="class: xl65, width: 64"]Count[/TD]
[/TR]
[TR]
[TD="class: xl65"]A[/TD]
[TD="class: xl65"]4[/TD]
[/TR]
[TR]
[TD="class: xl65"]B[/TD]
[TD="class: xl65"]3[/TD]
[/TR]
[TR]
[TD="class: xl65"]C[/TD]
[TD="class: xl65"]2
[/TD]
[/TR]
</tbody>[/TABLE]
Could anyone help on this.