Create an Excel COUNTIFS Formula from VBA InputBox Values
Question details
Create a COUNTIFS formula in VBA using a start date, end date, and industry collected through InputBox prompts.

- Product
- Microsoft Excel VBA
- Device & OS
- not provided
- Scenario
- A macro should count rows on the Employment worksheet that match a date range and an industry.
- Observed behavior
- The desired formula should use the entered values as criteria and count matching rows.
Confirm that the Employment sheet contains dates in C2:C507 and industries in G2:G507. Choose a worksheet where C30:E30 and C31 can be used for the inputs and result, and save the workbook as an Excel macro-enabled workbook (.xlsm).
Use InputBox values in an Excel VBA COUNTIFS formula
Use this macro to collect the criteria, store them in cells, and insert a COUNTIFS formula that references those cells.
The formula concatenates each comparison operator with a cell reference: ">=" with the start date and "<=" with the end date. Store the inputs and source values as actual Excel dates rather than text.
The date comparison is inclusive when source cells contain dates without time values.
In Excel, select Developer > Visual Basic, then choose Insert > Module.
Paste this code into the module: Sub CountEmployment() Dim ws As Worksheet Dim startInput As Variant, endInput As Variant, industryInput As Variant Dim startDate As Date, endDate As Date Set ws = ActiveSheet startInput = Application.InputBox("Enter the start date:", Type:=2) If VarType(startInput) = vbBoolean Then Exit Sub endInput = Application.InputBox("Enter the end date:", Type:=2) If VarType(endInput) = vbBoolean Then Exit Sub industryInput = Application.InputBox("Enter the industry:", Type:=2) If VarType(industryInput) = vbBoolean Then Exit Sub If Not IsDate(startInput) Or Not IsDate(endInput) Then MsgBox "Enter valid dates." Exit Sub End If startDate = DateValue(CDate(startInput)) endDate = DateValue(CDate(endInput)) If startDate > endDate Then MsgBox "The start date must not be after the end date." Exit Sub End If ws.Range("C30").Value = startDate ws.Range("D30").Value = endDate ws.Range("C30:D30").NumberFormat = "yyyy-mm-dd" ws.Range("E30").Value = CStr(industryInput) ws.Range("C31").FormulaR1C1 = "=COUNTIFS(Employment!R2C3:R507C3,"">=""&R30C3,Employment!R2C3:R507C3,""<=""&R30C4,Employment!R2C7:R507C7,R30C5)" End Sub
Return to the worksheet that should hold the criteria, select Developer > Macros, choose CountEmployment, and select Run. Enter the start date, end date, and industry when prompted.
Review C30:E30 for the entered criteria and C31 for the count. Adjust the Employment ranges in the code if your data occupies different rows or columns.

Count matching dates and industries in WPS Spreadsheet
WPS Office is a free, lightweight Office alternative with a familiar spreadsheet interface. Open compatible Excel files and use COUNTIFS directly for this task; it does not fix Excel VBA issues, and macro compatibility may vary.
- 1. Open the workbook: Open the Excel workbook in WPS Spreadsheet and select a worksheet for the criteria.
- 2. Enter the criteria: Enter the start date in C30, the end date in D30, and the industry in E30. Ensure the dates are recognized as date values.
- 3. Enter COUNTIFS: In C31, enter =COUNTIFS(Employment!C2:C507,">="&C30,Employment!C2:C507,"<="&D30,Employment!G2:G507,E30) and press Enter.

Frequently Asked Questions
Why does the formula use an ampersand after the comparison operator?
In the formula entered through FormulaR1C1, ">="&R30C3 combines the comparison operator with the start-date value in C30. The same pattern applies to the end date in D30.
Why does COUNTIFS return zero when matching dates appear in the sheet?
Check that Employment!C2:C507 and the input cells contain actual Excel date values rather than text. Also check that the industry in E30 matches the values in Employment!G2:G507.
How can I include all times on the end date?
If the source dates include times, replace the end-date criterion "<="&R30C4 with "<"&(R30C4+1). This counts timestamps before midnight on the day after the end date.
Where should I change the data ranges?
In the VBA editor, edit the FormulaR1C1 line. Change Employment!R2C3:R507C3 for the date range and Employment!R2C7:R507C7 for the industry range, keeping both ranges the same size.




