Last Row and Last column

vmjan02

Well-known Member
Joined
Aug 15, 2012
Messages
1,057
Office Version
  1. 365
  2. 2021
  3. 2019
  4. 2016
  5. 2013
I have this code and idea is clear to apply formula till the last row and last column, i am not sure as what is wrong with this code.

VBA Code:
Sub Countdata()
    Dim dERow As Double
    Dim lECol As Long
    
    dERow = Cells(10000, 4).End(xlUp).Row
    lECol = Cells(10000, 5).End(xlUp).Row
                
    Cells(4, 5).Formula = "=COUNTIFS('Actual DailyData'!$B:$B,$D4,'Actual DailyData'!$A:$A,E$3)"
    Cells(4, 5).AutoFill Destination:=Range(Cells(4, 5), Cells(4, 5 + lECol - 2)) '- 1))
    Range(Cells(4, 5), Cells(4, 5 + lECol - 2)).AutoFill Destination:=Range(Cells(5, 5), Cells(dERow, 5 + lECol - 2))
End Sub

the range of columns and rows are dynamic
DateEmp1Emp2
09-02-2022​
6​
8​
13-02-2022​
13-02-2022​
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Are you using lECol variable to get total number of coloumns?

Then it should be

VBA Code:
lECol = Cells(1, columns.count).End(xltoleft).column
 
Upvote 0
@Saurabhj

Have modified the script

VBA Code:
Sub Countdata()
    Dim dERow As Double
    Dim lECol As Long
    
    dERow = Cells(10000, 4).End(xlUp).Row
    'lECol = Cells(10000, 5).End(xlUp).Row
    lECol = Cells(1, Columns.Count).End(xlToLeft).Column
            
    Cells(4, 5).Formula = "=COUNTIFS('Actual DailyData'!$B:$B,$D4,'Actual DailyData'!$A:$A,E$3)"
    Cells(4, 5).AutoFill Destination:=Range(Cells(4, 5), Cells(4, 5 + lECol))  '- 1))
    
    On Error Resume Next
    Range(Cells(4, 5), Cells(4, 5 + lECol)).AutoFill Destination:=Range(Cells(4, 5), Cells(dERow, 5 + lECol))
End Sub

Its is not working for a new column entry, it is only working on 2 cloumns

eg:- function is applied to only Emp 1 and Emp 2 (column) and not applied to Emp 3
 
Upvote 0
Excel Formula:
Its is not working for a new column entry, it is only working on 2 columns

Are you saying that after entering a new data if you run the code it's not auto filling the formula for new data.
 
Upvote 0
yes, row wise it is working but not column wise
 
Upvote 0
Kindly share your data using XL2BB tool. It would help to understand the issue.
 
Upvote 0
Have fond the solution.

VBA Code:
Sub Countdata()
Dim last_row, last_col As Long

last_row = Cells(Rows.Count, 4).End(xlUp).Row
last_col = Cells(3, Columns.Count).End(xlToLeft).Column
Range(Cells(4, 5), Cells(last_row, last_col)).Formula = "=COUNTIFS('Actual DailyData'!$B:$B,$D4,'Actual DailyData'!$A:$A,E$3)"

End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,214,583
Messages
6,120,375
Members
448,955
Latest member
BatCoder

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