Populate field with formula results

RedWizard75

New Member
Joined
Sep 23, 2014
Messages
2
Hi,

I'm using the following 'stolen' code. To get the name of the sheets (which are the names of servers), which include patch information.
The 'server sheets' have varing rows which relate to the number of patches that are required to patch the server.

Initial VBA instructions are:
Code:
Sub IDX()
Dim ws As Worksheet, i As Integer, NumPatch As String, CountRange As String
Worksheets.Add(before:=Worksheets(1)).Name = "Summary"
i = 0
For Each ws In ThisWorkbook.Worksheets
    If ws.Name <> "Summary" Then
        i = i + 1
        Sheets("Summary").Range("A" & i).Value = ws.Name
        Sheets("Summary").Hyperlinks.Add Anchor:=Range("A" & i), Address:="", SubAddress:="'" & ws.Name & "'!A1", TextToDisplay:=ws.Name
    End If
Next ws
Sheets("Summary").Columns("A").AutoFit
End Sub

This creates a 'Summary' sheet at the front of all the worksheets. I places the name of the server in the 'A' rows as it works through the name of the sheets.
It then adds a Hyperlink to that sheet in the 'Summary' sheet.

I am trying to expand the code as such:
Code:
Sub IDX()
Dim ws As Worksheet, i As Integer, NumPatch As String, CountRange As String
Worksheets.Add(before:=Worksheets(1)).Name = "Summary"
Sheets("Summary").Range("A1").Value = "Server Name"
Sheets("Summary").Range("B1").Value = "Patches to Apply"
i = 1
For Each ws In ThisWorkbook.Worksheets
    If ws.Name <> "Summary" Then
        i = i + 1
        Sheets("Summary").Range("A" & i).Value = ws.Name
        Sheets("Summary").Hyperlinks.Add Anchor:=Range("A" & i), Address:="", SubAddress:="'" & ws.Name & "'!A1", TextToDisplay:=ws.Name
        Sheets("Summary").Range("B" & i).Value = "=COUNTIF(INDIRECT(""'""&A &i &""'""!A:A"")," <> ")"
    End If
Next ws
Sheets("Summary").Columns("A").AutoFit
Sheets("Summary").Columns("B").AutoFit
End Sub

The 'A' range gets the correct name of the 'sheet' and creates the Hyperlink correctly.
However, I'm trying to get the number of rows in the 'sheet' and display that in the 'B' range.

With this code, the 'B' range returns 'TRUE', rather than a number!!!
I can manually put the following formula into the 'B' range
Code:
 =COUNTIF(INDIRECT("'"&A2&"'!A:A"),"<>")
NOTE: A2, increases by 1 as I paste down the page, that is, A3, A4, etc...

And I get the number of records in the sheet of 'A#'.

I did see something that says 'INDIRECT' is unrecognisable to VBA. However, If I could get the string populating the 'B' range. I can remove the 'string' quote and get the answer ...

So in short, how can I place this
Code:
 =COUNTIF(INDIRECT("'"& A & i &"'!A:A"),"<>")
into a string?

Thank you in advance.
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Perhaps

Code:
Sub IDX()
Dim ws As Worksheet, i As Integer, NumPatch As String, CountRange As String
Worksheets.Add(before:=Worksheets(1)).Name = "Summary"
Sheets("Summary").Range("A1").Value = "Server Name"
Sheets("Summary").Range("B1").Value = "Patches to Apply"
i = 1
For Each ws In ThisWorkbook.Worksheets
    If ws.Name <> "Summary" Then
        i = i + 1
        Sheets("Summary").Range("A" & i).Value = ws.Name
        Sheets("Summary").Hyperlinks.Add Anchor:=Range("A" & i), Address:="", SubAddress:="'" & ws.Name & "'!A1", TextToDisplay:=ws.Name
        Sheets("Summary").Range("B" & i).Value = ws.UsedRange.Rows.Count
    End If
Next ws
Sheets("Summary").Columns("A:B").AutoFit
End Sub
 
Upvote 0
Perhaps

Code:
Sub IDX()
Dim ws As Worksheet, i As Integer, NumPatch As String, CountRange As String
Worksheets.Add(before:=Worksheets(1)).Name = "Summary"
Sheets("Summary").Range("A1").Value = "Server Name"
Sheets("Summary").Range("B1").Value = "Patches to Apply"
i = 1
For Each ws In ThisWorkbook.Worksheets
    If ws.Name <> "Summary" Then
        i = i + 1
        Sheets("Summary").Range("A" & i).Value = ws.Name
        Sheets("Summary").Hyperlinks.Add Anchor:=Range("A" & i), Address:="", SubAddress:="'" & ws.Name & "'!A1", TextToDisplay:=ws.Name
        Sheets("Summary").Range("B" & i).Value = ws.UsedRange.Rows.Count
    End If
Next ws
Sheets("Summary").Columns("A:B").AutoFit
End Sub


Worked an absolute treat!!!

Thank you VERY VERY much.

Cheers.
 
Upvote 0

Forum statistics

Threads
1,223,231
Messages
6,170,884
Members
452,364
Latest member
springate

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