VBA Macro to Fill in Formulas Referencing Separate Sheet

rschappe

New Member
Joined
Jun 4, 2014
Messages
6
Hello All,

Thank in advance for taking look.

My code is very simple. I creates a summary table of important stats I need to know about data in Sheet1.

Code:
     shtName = ActiveSheet.Name
    strName = InputBox(Prompt:="Enter AOI Name, please.", _
    Title:="Generate Summary", Default:="")
    Sheets.Add After:=ActiveSheet

' add formulas
    Range("B2").Formula = "=COUNTIF('" & shtName & "'!G:G, 0)"
    Range("B3").Formula = "=COUNTIF('" & shtName & "'!G:G, <101)"
    Range("B5").Formula = "=MAX('" & shtName & "'!Q:Q)"
    Range("B6").Formula = "=MAX('" & shtName & "'!R:R)"
    Range("B7").Formula = "=AVERAGE('" & shtName & "'!Q:Q)"
    Range("B8").Formula = "=AVERAGE('" & shtName & "'!R:R)"
    Range("B9").Formula = "=MIN('" & shtName & "'!S:S)"
    Range("B10").Formula = "=MAX('" & shtName & "'!S:S)"
    Range("B11").Forumla = "=MIN('" & shtName & "'!T:T)"
    Range("B12").Formula = "=MAX('" & shtName & "'!T:T)"
    Range("B13").Forumla = "=MIN('" & shtName & "'!W:W)"
    Range("B14").Formula = "=MAX('" & shtName & "'!W:W)"
    Range("B15").Formula = "=AVERAGE('" & shtName & "'!W:W)"
    Range("B16").Formula = "=MIN('" & shtName & "'!X:X)"
    Range("B17").Formula = "=MAX('" & shtName & "'!X:X)"
    Range("B18").Formula = "=MIN('" & shtName & "'!Y:Y)"
    Range("B19").Formula = "=MAX('" & shtName & "'!Y:Y)"

Code is failing in two places. The formula for range ("B3") doesn't function as I cant figure out the correct escape character to input "<101" into the second argument of the COUNTIF statement.

Secondly, and most oddly, only the first MIN statement functions. The rest return an error. Seems very strange since they are syntactically the same.


Thanks for taking a look.

Robert
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Hello All,

Thank in advance for taking look.

My code is very simple. I creates a summary table of important stats I need to know about data in Sheet1.

Code:
     shtName = ActiveSheet.Name
    strName = InputBox(Prompt:="Enter AOI Name, please.", _
    Title:="Generate Summary", Default:="")
    Sheets.Add After:=ActiveSheet

' add formulas
    Range("B2").Formula = "=COUNTIF('" & shtName & "'!G:G, 0)"
    Range("B3").Formula = "=COUNTIF('" & shtName & "'!G:G, <101)"
    Range("B5").Formula = "=MAX('" & shtName & "'!Q:Q)"
    Range("B6").Formula = "=MAX('" & shtName & "'!R:R)"
    Range("B7").Formula = "=AVERAGE('" & shtName & "'!Q:Q)"
    Range("B8").Formula = "=AVERAGE('" & shtName & "'!R:R)"
    Range("B9").Formula = "=MIN('" & shtName & "'!S:S)"
    Range("B10").Formula = "=MAX('" & shtName & "'!S:S)"
    Range("B11").Forumla = "=MIN('" & shtName & "'!T:T)"
    Range("B12").Formula = "=MAX('" & shtName & "'!T:T)"
    Range("B13").Forumla = "=MIN('" & shtName & "'!W:W)"
    Range("B14").Formula = "=MAX('" & shtName & "'!W:W)"
    Range("B15").Formula = "=AVERAGE('" & shtName & "'!W:W)"
    Range("B16").Formula = "=MIN('" & shtName & "'!X:X)"
    Range("B17").Formula = "=MAX('" & shtName & "'!X:X)"
    Range("B18").Formula = "=MIN('" & shtName & "'!Y:Y)"
    Range("B19").Formula = "=MAX('" & shtName & "'!Y:Y)"

Code is failing in two places. The formula for range ("B3") doesn't function as I cant figure out the correct escape character to input "<101" into the second argument of the COUNTIF statement.

Secondly, and most oddly, only the first MIN statement functions. The rest return an error. Seems very strange since they are syntactically the same.


Thanks for taking a look.

Robert
Enclose <101 in double quotation marks
Code:
Range("B3").Formula = "=COUNTIF('" & shtName & "'!G:G, ""<101"")"
and correct the spelling of Formula for Range("B11") and ("B13").
 
Upvote 0
I suppose my brain was just auto-correcting it every time that I read it.

Thanks for catching that!

Everything works!
 
Upvote 0

Forum statistics

Threads
1,216,077
Messages
6,128,684
Members
449,463
Latest member
Jojomen56

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