Question

I am trying to perform a COUNTIF but when it comes to define the range, to use a row value found in a previous "Find" statement. Might be easier to explain by showing my code:

Public Sub Run_Count_Click()

'// Set Ranges
Dim Cr_1, CR1_range, _
Cr_2, CR2_range, _
Cr_3, CR3_range, _
Cr_4, CR4_range, _
Cr_5, CR5_range _
As Range

'// Set Integers
Dim CR1, V1, CR1_Result, _
CR2, V2, CR2_Result, _
CR3, V3, CR3_Result, _
CR4, V4, CR4_Result, _
CR5, V5, CR5_Result, _
total_result, _
total_result2, _
total_result3, _
total_result4, _
total_result5 _
As Integer

'Set Strings
Dim V_1, V_2, V_3, V_4, V_5 As String

Dim ws As Worksheet

Set ws = Worksheets("database")

Dim Date_Start, Date_End As Long

Date_Start = ws.Cells.Find(What:=Me.R_Start.Value, SearchOrder:=xlRows, _
    SearchDirection:=xlPrevious, LookIn:=xlValues).Row

Date_End = ws.Cells.Find(What:=Me.R_End.Value, SearchOrder:=xlRows, _
    SearchDirection:=xlPrevious, LookIn:=xlValues).Row

'// Get Criteria From Form And Search Database Headers
Set Cr_1 = ws.Cells.Find(What:=Me.Count_Criteria_1.Value, After:=ws.Cells(1, 1), MatchCase:=False)

If Not Cr_1 Is Nothing Then

CR1 = Cr_1.Column '//Set CR1 as the Column in which the Criteria Header was found

Else
    MsgBox "Criteria 1 Has Not Been Found In The Database. Report Has Failed To Generate"
    Exit Sub
End If

'// Get Variable Value From Form And Set Shortcode
V_1 = Me.Criteria_1_Variable.Value

Set CR1_range = ws.Range(ws.Cells(Date_Start, CR1), ws.Cells(Date_End, CR1))
CR1_Result = Application.CountIf(CR1_range, V_1)

If Me.Count_Criteria_2 = "Any" Then

Me.Count_Result.visible = True

Me.Count_Result.Value = "Based On Your Search Criteria Of:" & vbNewLine & _
"How many occurences of [" & Me.Criteria_1_Variable.Value & "] in the category [" & Me.Count_Criteria_1.Value & _
"] have occured between the dates..." & vbNewLine & vbNewLine & "The Results Are: " & CR1_Result

Exit Sub

Else 'More stuff after this that is not relevant

I get an error saying that the lines below require an object to be set:

Date_Start = ws.Cells.Find(What:=Me.R_Start.Value, SearchOrder:=xlRows, _
        SearchDirection:=xlPrevious, LookIn:=xlValues).Row

    Date_End = ws.Cells.Find(What:=Me.R_End.Value, SearchOrder:=xlRows, _
        SearchDirection:=xlPrevious, LookIn:=xlValues).Row

Why?

Was it helpful?

Solution 2

Ok, thanks for all your input, I managed to solve this myself in the end like this:

Public Sub Run_Count_Click()

'// Set Ranges
Dim Cr_1, CR1_range, _
Cr_2, CR2_range, _
Cr_3, CR3_range, _
Cr_4, CR4_range, _
Cr_5, CR5_range _
As Range

'// Set Integers
Dim CR1, V1, CR1_Result, _
CR2, V2, CR2_Result, _
CR3, V3, CR3_Result, _
CR4, V4, CR4_Result, _
CR5, V5, CR5_Result, _
total_result, _
total_result2, _
total_result3, _
total_result4, _
total_result5 _
As Integer

'Set Strings
Dim V_1, V_2, V_3, V_4, V_5 As String

Dim ws As Worksheet

Set ws = Worksheets("database")

Sheets("Settings").Range("Start_Date").Value = Format(Me.R_Start.Value, "mm/dd/yyyy")
Sheets("Settings").Range("End_Date").Value = Format(Me.R_End.Value, "mm/dd/yyyy")

'Collect Start & End Dates
Dim dStartDate As Long
Dim dEndDate As Long
dStartDate = Sheets("Settings").Range("Start_Date").Value
dEndDate = Sheets("Settings").Range("End_Date").Value

ws.Activate

'On Error GoTo error_Sdate:
Dim RowNum As Variant
    RowNum = Application.WorksheetFunction.Match(dStartDate, Range("B1:B60000"), 0)
     MsgBox "Found " & Format(dStartDate, "dd/mm/yyyy") & " at row : " & RowNum

'On Error GoTo error_Edate:
Dim RowNumEnd As Variant
    RowNumEnd = Application.WorksheetFunction.Match(dEndDate, Range("B1:B60000"), 1)
     MsgBox "Found " & Format(dEndDate, "dd/mm/yyyy") & " at row : " & RowNumEnd

GoTo J1

error_Sdate:

Dim msg As String

msg = "You entered " & Format(dStartDate, "dd/mm/yyyy") & " as your Start Date, but no referrals were made on that date"
msg = msg & vbCrLf & "Please enter a different date in the Start Date box"
MsgBox msg, , "Start Date Not Found"
Err.Clear
Exit Sub

error_Edate:
msg = "You entered " & Format(dEndDate, "dd/mm/yyyy") & " as your End Date, but no referrals were made on that date"
msg = msg & vbCrLf & "Please enter a different date in the End Date box"
MsgBox msg, , "End Date Not Found"
Err.Clear
Exit Sub


J1:


'// Get Criteria From Form And Search Database Headers
Set Cr_1 = ws.Cells.Find(What:=Me.Count_Criteria_1.Value, After:=ws.Cells(1, 1), MatchCase:=False)

If Not Cr_1 Is Nothing Then

CR1 = Cr_1.Column '//Set CR1 as the Column in which the Criteria Header was found

Else
    MsgBox "Criteria 1 Has Not Been Found In The Database. Report Has Failed To Generate"
    Exit Sub
End If

'// Get Variable Value From Form And Set Shortcode
V_1 = Me.Criteria_1_Variable.Value

Set CR1_range = ws.Range(ws.Cells(RowNum, CR1), ws.Cells(RowNumEnd, CR1))
CR1_Result = Application.CountIf(CR1_range, V_1)

If Me.Count_Criteria_2 = "Any" Then

Me.Count_Result.visible = True

Me.Count_Result.Value = "Based On Your Search Criteria Of:" & vbNewLine & vbNewLine & _
"- " & Me.Count_Criteria_1.Value & ": " & Me.Criteria_1_Variable.Value & vbNewLine & vbNewLine & _
"The Results Are: " & CR1_Result & " entries found between the dates " & Format(dStartDate, "dd/mm/yyyy") & _
" and " & Format(dEndDate, "dd/mm/yyyy")

end sub

The script sets dStartDate as the date entered in the settings page (which is written to from the form earlier in the script) and dEndDate as the end date. Then sets a criteria based on userform input and a variable based on the userform.

Finally a CountIf is done based on the criteria, variable and dates entered in the form...

Taken a long time to get it working, but now I have successfully managed to handle 10 variables with this form at the same time. Apart from VBA's messed up formatting for dates this works great now.

Thanks for all your help!

Edit: (The OnError's are commented out for testing, the MsgBox's that show row numbers should be commented out, but are not for testing)

OTHER TIPS

I don't know if this has anything to do with your problem, but I think you are incorrectly declaring your variables. In your set ranges segment, I assume you want all of those variables to be set to Type Range, but only CR5_Range is being declared as a range; the others are all declared as variants. Although you can place multiple declarations on a single line, each variable needs to be defined as a Type or, if Type is omitted, it will default to Variant Type. This may cause some useful error messages to be omitted. Same problem exists with your other declaration segments.

The problem could well be that the Find method is not Finding anything. That will give that error. Date's are sometimes tricky to "Find". You can check seeing if the result (without the row) is nothing. e.g: debug.print .find(.....) is nothing If that returns TRUE, your Find is failing.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top