Countif in VBA

GordonLS

Board Regular
Joined
May 28, 2021
Messages
89
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Good afternoon. so I use the following to select the 1st empty cell in Column G via VBA.

Cells(Rows.Count, "G").End(xlUp).Offset(1, 0).Select - In this example G10 is selected

I now need to count with the following:

=sumproduct(1/countif(G2:G9,G2:G9) - however I never know what the 1st empty cell is so in other words I want count down from G2 to the last cell prior to my formula.

G
1 Order Number
2 WALM-15181
3 WALM-14830
4 WALM-14797
5 WALM-14788
6 WALM-14738
7 WALM-14578
8 WALM-14290
9 WALM-14290
=sumproduct(1/countif( ??????? )



 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
How about
VBA Code:
Range("G" & Rows.Count).End(xlUp).Offset(1).FormulaR1C1 = "=sumproduct(1/countif(r2c:r[-1]c,r2c:r[-1]c))"
 
Upvote 0
This works perfect. Can you guide me to where I can understand the following?

(r2c:r[-1]c,r2c:r[-1]c))"
 
Upvote 0
The R stands for row & the C for column. If the number is not in [] then it's absolute, otherwise it's relative, and if there is no number it means same column/row.
So r2c is row 2 this column & r[-1]c is one row above in this column
 
Upvote 0
Solution
Very much appreciated. I will be back again.

Gordon
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,567
Messages
6,120,268
Members
448,953
Latest member
Dutchie_1

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