COUNT Formula VBA

chaddres

Board Regular
Joined
Jun 14, 2014
Messages
143
Office Version
  1. 365
Platform
  1. Windows
How do I use lastcol in my last line of code to count from B2 to lastcol2?

VBA Code:
    Range("B1").Select
    ActiveCell.FormulaR1C1 = "COUNT"
    Range("B2").Select
    Dim lastcol As Long
    lastcol = Cells(ActiveCell.Row, Columns.Count).End(xlToLeft).Column
    Cells(ActiveCell.Row, lastcol + 1).Select
    ActiveCell.FormulaR1C1 = "=COUNT(B2:??2)"
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Without changing anything in the current code:
VBA Code:
ActiveCell.FormulaR1C1 = "=COUNT(RC[" & (1 - lastcol) & "]:RC[-1])"

With a couple of changes to avoid Select actions:
VBA Code:
Dim lastcol As Long
    Range("B1").FormulaR1C1 = "COUNT"
    lastcol = Cells(ActiveCell.Row, Columns.Count).End(xlToLeft).Column
    Cells(2, lastcol + 1).FormulaR1C1 = "=COUNT(RC[" & (1 - lastcol) & "]:RC[-1])"
 
Upvote 0
Solution
Thank you again. I am stuck once again. I am trying to copy the formula (on line 4 of the code) down to the bottom of the range of data. The data starts in "lastcol3". My normal entry (for column B) would be "Range(B3:B & Range("A" & Rows.Count).End(xlUp).Row).FillDown" so I tried to use lastcol instead of the "B".

VBA Code:
    Dim lastcol As Long
    lastcol = Cells(ActiveCell.Row, Columns.Count).End(xlToLeft).Column
    Cells(ActiveCell.Row, lastcol + 1).Select
    ActiveCell.Value = "=COUNT(RC[" & (1 - lastcol) & "]:RC[-1])"
    Range(lastcol & "3:" & lastcol & Range("A" & Rows.Count).End(xlUp).Row).FillDown
 
Upvote 0
Unfortunately, I don't want to but I have to make assumptions. Please understand if there is no sample data structure in the question, then the helper can only make assumptions.

If your data range starts from A1, and you want to add an additional column to this range that contains the formula, then please try the following. By the way, I suggest stopping using ActiveCell and Select methods. You do not have to Select a cell to change its properties. I recommend trying to write codes by reference as much as possible.

The following code:
1- Finds the entire range that is starting from A1 - CurrentRegion method.
2- Finds the cell on the adjacent column, and on the second row - Offset(1, rng.Columns.Count).
3- Resizes the cell to include all rows on the column along with the data range - Resize method.
4- Set the formula for all cells at once.

VBA Code:
Dim rng As Range
    Set rng = Range("A1").CurrentRegion
    rng.Offset(1, rng.Columns.Count).Resize(rng.Rows.Count - 1, 1).FormulaR1C1 = "=COUNT(RC[" & (1 - rng.Columns.Count) & "]:RC[-1])"
 
Upvote 0

Forum statistics

Threads
1,214,947
Messages
6,122,413
Members
449,082
Latest member
tish101

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