countifs to last row

Godders199

Active Member
Joined
Mar 2, 2017
Messages
313
Office Version
  1. 2013
Hello, i have written the following code, which works fine for one row, i am trying to change it to go to the last row, with the criteria changing from A2 to a3. a4 etc,

Range("f2") = WorksheetFunction.CountIfs(ThisWorkbook.Sheets("Adviser data").Range("A:A"), ThisWorkbook.Sheets("advisers").Range("a2"), ThisWorkbook.Sheets("Adviser data").Range("d:d"), ">=" & Daterng, ThisWorkbook.Sheets("Adviser data").Range("d:d"), "<=" & Daterng1)

heres what i have done to try and get to last row
With Sheets("Advisers")
Dim rowlast As Long
rowlast = .Range("a" & Rows.Count).End(xlUp).Row

With .Range("f2& K & rowlast") = WorksheetFunction.CountIfs(ThisWorkbook.Sheets("Adviser data").Range("A:A"), ThisWorkbook.Sheets("advisers").Range("a2"), ThisWorkbook.Sheets("Adviser data").Range("d:d"), ">=" & Daterng, ThisWorkbook.Sheets("Adviser data").Range("d:d"), "<=" & Daterng1)
End With
End With
Can anyone say where i am gong wrong please
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Like this maybe?

Code:
Dim lastRow As Long
Dim thisRow As Long

With Sheets("Advisers")
    lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
    For thisRow = 2 To lastRow
        .Cells(thisRow, "F").Value = WorksheetFunction.CountIfs(ThisWorkbook.Sheets("Adviser data").Range("A:A"), .Cells(thisRow, "A").Value, ThisWorkbook.Sheets("Adviser data").Range("D:D"), ">=" & Daterng, ThisWorkbook.Sheets("Adviser data").Range("D:D"), "<=" & Daterng1)
    Next thisRow
End With

WBD
 
Upvote 0
Like this maybe?

Code:
Dim lastRow As Long
Dim thisRow As Long

With Sheets("Advisers")
    lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
    For thisRow = 2 To lastRow
        .Cells(thisRow, "F").Value = WorksheetFunction.CountIfs(ThisWorkbook.Sheets("Adviser data").Range("A:A"), .Cells(thisRow, "A").Value, ThisWorkbook.Sheets("Adviser data").Range("D:D"), ">=" & Daterng, ThisWorkbook.Sheets("Adviser data").Range("D:D"), "<=" & Daterng1)
    Next thisRow
End With

WBD

thanks had just worked i out with
Dim lastrowcolumnA As Long
lastrowcolumnA = Range("a" & Rows.Count).End(xlUp).Row
For i = 2 To lastrowcolumnA
Cells(i, 6) = WorksheetFunction.CountIfs(ThisWorkbook.Sheets("Adviser data").Range("A:A"), Cells(i, 1), ThisWorkbook.Sheets("Adviser data").Range("d:d"), ">=" & Daterng, ThisWorkbook.Sheets("Adviser data").Range("d:d"), "<=" & Daterng1)
Next
 
Upvote 0

Forum statistics

Threads
1,224,828
Messages
6,181,201
Members
453,022
Latest member
RobertV1609

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