VBA Code Creation in Excel

Sivas

New Member
Joined
Aug 29, 2018
Messages
20
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
XYZXYZXYZ
141878014187801418780
1438095 1438095
16435421643542
13090821309082

<colgroup><col width="64" span="5" style="width:48pt"> </colgroup><tbody>
</tbody>


and my result should look like,

Sheet NameCount
A4
B3
C2

<colgroup><col width="64" span="2" style="width:48pt"> </colgroup><tbody>
</tbody>


Could anyone help on this.
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Code:
[color=darkblue]Sub[/color] Click()
    [color=darkblue]Dim[/color] sh [color=darkblue]As[/color] Worksheet, ws [color=darkblue]As[/color] Worksheet, x [color=darkblue]As[/color] [color=darkblue]Long[/color], i [color=darkblue]As[/color] Long
    
    Sheets.Add(After:=Sheets(Sheets.Count)).Name = "Report"
    [color=darkblue]Set[/color] ws = ActiveSheet
    i = 2
    ws.Range("A1:B1").Value = Array("Name", "Count")
    
    [color=darkblue]For[/color] [color=darkblue]Each[/color] sh [color=darkblue]In[/color] Sheets
        [color=darkblue]If[/color] [color=darkblue]Not[/color] sh [color=darkblue]Is[/color] ws [color=darkblue]Then[/color]
            x = Application.WorksheetFunction.CountA(sh.Range("A2:A100"))
            sh.Cells(Rows.Count, "A").End(xlUp).Offset(1).Resize(, 2).Value = Array(sh.Name, x)
            ws.Rows(i).Range("A1:B1").Value = Array(sh.Name, x)
            i = i + 1
        [color=darkblue]End[/color] [color=darkblue]If[/color]
    [color=darkblue]Next[/color] sh
    
[color=darkblue]End[/color] [color=darkblue]Sub[/color]
 
Upvote 0
Thank you for your valuable answer. But it's not searching for particular column name.
 
Upvote 0
Code:
[color=darkblue]Sub[/color] Click()
    [color=darkblue]Dim[/color] sh [color=darkblue]As[/color] Worksheet, ws [color=darkblue]As[/color] Worksheet, x [color=darkblue]As[/color] [color=darkblue]Long[/color], i [color=darkblue]As[/color] Long, rngXYZ [color=darkblue]As[/color] Range
    
    Sheets.Add(After:=Sheets(Sheets.Count)).Name = "Report"
    [color=darkblue]Set[/color] ws = ActiveSheet
    i = 2
    ws.Range("A1:B1").Value = Array("Name", "Count")
    
    [color=darkblue]For[/color] [color=darkblue]Each[/color] sh [color=darkblue]In[/color] Sheets
        [color=darkblue]If[/color] [color=darkblue]Not[/color] sh [color=darkblue]Is[/color] ws [color=darkblue]Then[/color]
            [color=darkblue]Set[/color] rngXYZ = sh.Rows(1).Find("XYZ", , xlValues, xlWhole, 1, 1, 0)
            [color=darkblue]If[/color] [color=darkblue]Not[/color] rngXYZ [color=darkblue]Is[/color] [color=darkblue]Nothing[/color] [color=darkblue]Then[/color]
                x = Application.CountA(rngXYZ.EntireColumn) - 1
                sh.Cells(Rows.Count, rngXYZ.Column).End(xlUp).Offset(1).Resize(, 2).Value = Array(sh.Name, x)
                ws.Rows(i).Range("A1:B1").Value = Array(sh.Name, x)
                i = i + 1
            [color=darkblue]End[/color] [color=darkblue]If[/color]
        [color=darkblue]End[/color] [color=darkblue]If[/color]
    [color=darkblue]Next[/color] sh
    
[color=darkblue]End[/color] [color=darkblue]Sub[/color]
 
Upvote 0
Thank you very much for your answer and help. I have one more additional query in this.

How to highlight the sheet with color which has the value more than 0.
 
Upvote 0

Forum statistics

Threads
1,214,424
Messages
6,119,404
Members
448,893
Latest member
AtariBaby

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