Consulting

Results 1 to 5 of 5

Thread: Activating multiple sheets with countifs function

  1. #1
    VBAX Regular
    Joined
    Jul 2014
    Posts
    15
    Location

    Activating multiple sheets with countifs function

    Hi all,

    I am trying to use a countifs function to get my data on multiple sheets. The code is as follows :
    ActiveSheet.Range("S1").Value = Evaluate("COUNTIFS(ActiveSheet!D:D,""P"",ActiveSheet!I:I,""1"")")
    ActiveSheet.Range("S2").Value = Evaluate("COUNTIFS(ActiveSheet!D:D,""I"",ActiveSheet!I:I,""1"")")
    ActiveSheet.Range("S3").Value = Evaluate("COUNTIFS(ActiveSheet!D:D,""O"",ActiveSheet!I:I,""1"")")
    ActiveSheet.Range("S4").Value = Evaluate("COUNTIFS(ActiveSheet!D:D,""C"",ActiveSheet!I:I,""1"")")
    ActiveSheet.Range("S5").Value = Evaluate("COUNTIFS(ActiveSheet!D:D,""L"",ActiveSheet!I:I,""1"")")


    As the sheet name varies, I'm not able to use the constant name Sheet1!. I tried changing (Sheet1!D,""P"",Sheet1!I:I,""1"") to (ActiveSheet!D,""P"",ActiveSheet!I:I,""1"") but it doesn't seem to work. Any idea how to do it? Any help will be appreciated. Thank you.

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Maybe this will work

    With ActiveSheet
    
    .Range("S1").Value = Evaluate("COUNTIFS('" & .Name & "'!D:D,""P"",'" & .Name & "'!D:D,""!I:I,""1"")") 
    .Range("S2").Value = Evaluate("COUNTIFS('" & .Name & "'!D:D,""!D:D,""I"",'" & .Name & "'!D:D,""!I:I,""1"")") 
    .Range("S3").Value = Evaluate("COUNTIFS('" & .Name & "'!D:D,""!D:D,""O"",'" & .Name & "'!D:D,""!I:I,""1"")") 
    .Range("S4").Value = Evaluate("COUNTIFS('" & .Name & "'!D:D,""!D:D,""C"",'" & .Name & "'!D:D,""!I:I,""1"")") 
    .Range("S5").Value = Evaluate("COUNTIFS('" & .Name & "'!D:D,""!D:D,""L"",'" & .Name & "'!D:D,""!I:I,""1"")") 
    End With
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    VBAX Tutor
    Joined
    Mar 2014
    Posts
    210
    Location
    You dont need the word ACTIVESHEET. Excel assumes your formula is for the activesheet so you dont need to put it in the formula. Just use the range.

  4. #4
    VBAX Regular
    Joined
    Jul 2014
    Posts
    15
    Location
    Quote Originally Posted by xld View Post
    Maybe this will work

    With ActiveSheet
    
    .Range("S1").Value = Evaluate("COUNTIFS('" & .Name & "'!D:D,""P"",'" & .Name & "'!D:D,""!I:I,""1"")") 
    .Range("S2").Value = Evaluate("COUNTIFS('" & .Name & "'!D:D,""!D:D,""I"",'" & .Name & "'!D:D,""!I:I,""1"")") 
    .Range("S3").Value = Evaluate("COUNTIFS('" & .Name & "'!D:D,""!D:D,""O"",'" & .Name & "'!D:D,""!I:I,""1"")") 
    .Range("S4").Value = Evaluate("COUNTIFS('" & .Name & "'!D:D,""!D:D,""C"",'" & .Name & "'!D:D,""!I:I,""1"")") 
    .Range("S5").Value = Evaluate("COUNTIFS('" & .Name & "'!D:D,""!D:D,""L"",'" & .Name & "'!D:D,""!I:I,""1"")") 
    End With
    Thank you very much (:

  5. #5
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by ranman256 View Post
    You dont need the word ACTIVESHEET. Excel assumes your formula is for the activesheet so you dont need to put it in the formula. Just use the range.
    It is true that you don't, but it is not a good practice to omit a sheet reference, including it shows that you have explicitly decided you want it to work on the activesheet, and it is much easier to change code that is explicit if things change.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •