Convert COUNTIFS formula to vba code so I can replicate

vbastarterstar

New Member
Joined
Aug 3, 2018
Messages
2
Hello,

I am trying to search for 4 certain phrases across multiple tabs, within certain date ranges

The formula I have used is

=countifs('Alex'!B20:B51,">1/1/18",'Alex'!B20:B51,"<8/1/18",'Alex'!C20:C51,"*TCM*")

B is my date column and C is my comment column

This formula works but I need to count how many times 'TCM', 'TCR', 'EMS' and 'EMR' appear in each of my tabs
I need a weekly count too

please can someone help me write the vba code I would need
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
Re: Please help convert COUNTIFS formula to vba code so I can replicate

Welcome to the board.

Insufficient detail to solve I'm afraid, but start by trying:
Code:
Sub Loopy()

    Dim arr()   As Variant
    Dim temp    As Variant
    Dim x       As Long
    Dim str     As String
    
    str = "=COUNTIFS('Alex'!$B$20:$B$51,"">1/1/8"",'Alex'!$B$20:$B$51,""<8/1/18"",'Alex'!$C$20:$C$51,@Term)"
    arr = Array("""*TCM*""", """*TCR*""", """*EMS*""", """*EMR*""")

    With Worksheets.Add
        For x = LBound(arr) To UBound(arr)
            .Cells(1 + x, 1).Formula = Replace(str, "@Term", arr(x))
        Next x
    End With

    Erase arr
End Sub
It will output COUNTIFs for all 4 terms within the specified data range to the given sheet and range into a new sheet, starting in cell A1
 
Last edited:
Upvote 0
Re: Please help convert COUNTIFS formula to vba code so I can replicate

Hi Jack,

Really appreciate that, thank you!

if I wanted to have the data count in a pre-made table,

how would I direct that?
 
Upvote 0
Re: Please help convert COUNTIFS formula to vba code so I can replicate

WorksheetFunction.SumIfs(

i found naming your ranges works easiest
 
Last edited:
Upvote 0
Re: Please help convert COUNTIFS formula to vba code so I can replicate

@vbastarerstar pre-made table? You'll need to be specific, provide sheet name, cell address of table and then be easier to figure out how to adapt the code to suit your needs
 
Upvote 0

Forum statistics

Threads
1,214,851
Messages
6,121,931
Members
449,056
Latest member
denissimo

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