VBA help on row count in if/then statement

ItalianPlatinum

Well-known Member
Joined
Mar 23, 2017
Messages
886
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Hello - my current vba will evaluate if column H is > 2 if it is then it proceeds if not then it will stop. For some reason if i select the cell H (myself) the count is 2
1680021932834.png
but my VBA is evaluating something greater than 2. Am i using the wrong code for this?

VBA Code:
If WsDIST.Range("H" & rows.count).End(xlUp).row > 2 Then

1680022019224.png
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
ok thanks something like this you think suffices?

VBA Code:
If WsDIST.Application.WorksheetFunction.CountA("H:H") > 1 Then
 
Upvote 0
The way VBA uses ranges is a little different (as you have written it, it sees "H:H" as literal text, not a range).
I usually build the range as a range variable, and then use it like this:

VBA Code:
    Dim rng As Range
    Set rng = WsDIST.Range("H:H")
   
    If Application.WorksheetFunction.CountA(rng) > 1 Then

Note if you want to know if there are at least 2 records, then you will need to check for something like:
VBA Code:
>3
or
VBA Code:
>=3

(2 records plus 1 header record)
 
Upvote 0
Solution
application.worksheetfunction.CountA(sheets("Sheet2").range("H:H"))
works fine for me (at least in immediate window)?
 
Upvote 0
application.worksheetfunction.CountA(sheets("Sheet2").range("H:H"))
works fine for me (at least in immediate window)?
Yep, that should work too, putting it all explicitly between the quotes, as long as you have the complete sheet and range references.
I just like the cleaner look of the using variable, but that's just me.
 
Upvote 0

Forum statistics

Threads
1,225,225
Messages
6,183,686
Members
453,180
Latest member
Systemize

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