VBA autofill question

gd6noob

Board Regular
Joined
Oct 20, 2017
Messages
170
Office Version
  1. 2016
Platform
  1. Windows
I have a question about VBA autofill. Below is my table of shift pattern, it calculates the number of people per shift On Premise vs Scheduled.
YUL2 Site HR tool v2.2.xlsb
ABCDEFGHIJKLMNOPQRSTUVWXYZ
1INBOUNDICQAOUTBOUND
2DayOn PremiseScheduled% on 7/29NightOn PremiseScheduled% on 7/29DayOn PremiseScheduled% on 7/29NightOn PremiseScheduled% on 7/29DayOn PremiseScheduled% on 7/29NightOn PremiseScheduled% on 7/29
3DA5C0700450.8NA5C1800050DA5C0700551NA5C1800551DA5C073016180.88888889NA5C183016180.88888889
4DA6C0700340.75NA6C1800140.25DB3C0700551NB3C1800551DB3C073011111NB3C183011111
5DB2C0700441NB2C1800040000000DC7C0730080080
6DB3C0700560.83333333NB3C1800060000000DN4C073001100110
7DL4C0700030NL4C1800130.33333333000000000000
8DN4C0700050050000000000000
Calculations
Cell Formulas
RangeFormula
D2,Z2,V2,Q2,M2,H2D2="% on "&TEXT(Dashboard!$I$3,"m/d")
B3:B8B3=COUNTIFS('Shift Punches'!$A:$A,$A$1,'Shift Punches'!$E:$E,$A3)
G3:G8,C3:C8C3=COUNTIFS(Roster!$E:$E,$A$1,Roster!$G:$G,$A3)
Z3:Z8,V3:V8,Q3:Q8,H3:H8,D3:D8D3=IF(C3,B3/C3,0)
F3:F8F3=COUNTIFS('Shift Punches'!$A:$A,$A$1,'Shift Punches'!$E:$E,$E3)
O3:O8,K3:K8K3=COUNTIFS('Shift Punches'!$A:$A,$J$1,'Shift Punches'!$E:$E,$J3)
P3:P8,L3:L8L3=COUNTIFS(Roster!$E:$E,$J$1,Roster!$G:$G,$J3)
M3:M8M3=IF(L3,L3/L3,0)
X3:X8,T3:T8T3=COUNTIFS('Shift Punches'!$A:$A,$S$1,'Shift Punches'!$E:$E,$S3)
Y3:Y8,U3:U8U3=COUNTIFS(Roster!$E:$E,$S$1,Roster!$G:$G,$S3)


The code Im using in VBA:
VBA Code:
LastRow1 = ActiveSheet.Cells(Rows.Count, 1).End(3).Row

'IB day
    Range("B3").Select
    Range("B3").Value2 = "=COUNTIFS('Shift Punches'!$A:$A,$A$1,'Shift Punches'!$E:$E,$A3)"
    
    Range("C3").Select
    Range("C3").Value2 = "=COUNTIFS(Roster!$E:$E,$A$1,Roster!$G:$G,$A3)"
    
    Range("D3").Select
    Range("D3").Value2 = "=IF(C3,B3/C3,0)"
    
    Range("B3:D3").Select
    If LastRow1 > 2 Then
    Selection.AutoFill Destination:=Range("B3:D" & LastRow1)
    End If
'IB nights
LastRow2 = ActiveSheet.Cells(Rows.Count, 1).End(3).Row
    Range("F3").Select
    Range("F3").Value2 = "=COUNTIFS('Shift Punches'!$A:$A,$A$1,'Shift Punches'!$E:$E,$E3)"
    
    Range("G3").Select
    Range("G3").Value2 = "=COUNTIFS(Roster!$E:$E,$A$1,Roster!$G:$G,$A3)"
    
    Range("H3").Select
    Range("H3").Value2 = "=IF(G3,F3/G3,0)"
    
    Range("F3:H3").Select
    If LastRow2 > 2 Then
    Selection.AutoFill Destination:=Range("F3:H" & LastRow2)
    End If
As you can see, cells F to H autofills to row 8 but that row should not be autofilled. It is also like this for all the other sections where autofill ends at wherever column A ends with data, which is not what I want.
The autofill for the other section is pretty much the same, but adjusting the LastRow#
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
I only want the autofill to fill in data until end of data.

i.e. Column E, data ends at E7 but autofill code ends at E8. Same with columns K-M, should end at row 4.
 
Upvote 0
In that case Lastrow2 should be looking for the last row in col E, not in col A.
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,897
Messages
6,122,148
Members
449,066
Latest member
Andyg666

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