Add a formula in 28th row

bdt

Board Regular
Joined
Oct 3, 2024
Messages
53
Office Version
  1. 2019
Platform
  1. Windows
Hi guys.
Don't know if this is possible. I have a code that I'd like to add formula to. Presently, when running the code, 7 rows are added to sheet "OVERTIME" which are dates and days, sunday to saturday, along with where required a number and a letter "A" and a border added to the bottom of the last row.
What I would like is once 4 weeks have been added, 28 rows, a simple sum formula is added in columns N to X in the 28th row to add the numbers in columns C to M for the last 4 weeks. Also ideally like a heavy border line to be added to the bottom of row 28 to differentiate each 4 week period.
Below is a mini sheet of "OVERTIME"
LATHE MAGIC ABACUS v10.xlsm
ABCDEFGHIJKLMNOPQRSTUVWXY
25706/09/24FriA44
25807/09/24Sat45204056363204800
25908/09/24Sun4AA4
26009/09/24Mon4A44A4
26110/09/24Tues4A44A4
26211/09/24WedsA444A4
26312/09/24Thur44A
26413/09/24Fri44A
26514/09/24Sat
26615/09/24Sun4A
26716/09/24MonAAA444
26817/09/24TuesAA
26918/09/24Weds4AA
27019/09/24Thur4A4
27120/09/24FriAA4
27221/09/24Sat
27322/09/24SunA
27423/09/24MonA
27524/09/24TuesAA
27625/09/24WedsA
27726/09/24Thur4A4
27827/09/24Fri4A4
27928/09/24Sat
28029/09/24Sun4
28130/09/24MonAA4
28201/10/24TuesAA
28302/10/24WedsAA
28403/10/24ThurAA
28504/10/24FriAA
28605/10/24Sat2808028241202800
28706/10/24SunAA
OVERTIME
Cell Formulas
RangeFormula
N286:X286,N258:X258N258=SUM(C231:C258)
Cells with Conditional Formatting
CellConditionCell FormatStop If True
C1:M154,C155:F155,H155:J155,C156:J159,K155:M159,C160:M1048576Cell Value=0textNO
C167:M735Cell Valuecontains "A"textNO


The code, with the great effort from you guys, is

VBA Code:
Public Sub CopySheetAndRenamePredefined()


    Application.ScreenUpdating = False
    Dim ws As Worksheet
    Dim response As String
    For Each ws In Sheets
        If ws.Range("B2") <> "" And ws.Range("C2") = "" Then
        Do
            response = InputBox("Input date in format **/**/**")
            If response <> "" Then
            ws.Range("C2") = response
            Exit Do
            ElseIf response = "" Then
            MsgBox ("You must enter date in format **/**/**")

        Else: Exit Do
        End If
    Loop
    
    End If
    Next ws
    Application.ScreenUpdating = True
    
 Dim arr1 As Variant
    Dim arr2 As Variant
    Dim i As Long
    Dim dte As Date
    Dim writeCel As Range

' determine where to start
With Sheets("OVERTIME")
    Set writeCel = .Range("A" & .Rows.Count).End(xlUp).Offset(1)
End With

With ActiveSheet
'-----SUNDAY-----
'add overtime and absence sunday
    dte = .Range("M2").Value
    arr1 = .Range("AI21:AI32").Value
    arr2 = .Range("CY21:CY32").Value
    For i = LBound(arr1) To UBound(arr1)
        If arr2(i, 1) = "A" Then
            arr1(i, 1) = arr2(i, 1)
        End If
    Next i
    ' write to sheet and clear arrays
    writeCel = dte
    writeCel.Offset(0, 1).Resize(, UBound(arr1)).Value = Application.Transpose(arr1)
    Set arr1 = Nothing
    Set arr2 = Nothing

'-----MONDAY-----
'add overtime and absence monday
    dte = .Range("AC2").Value
    arr1 = .Range("AK21:AK32").Value
    arr2 = .Range("DA21:DA32").Value
    For i = LBound(arr1) To UBound(arr1)
        If arr2(i, 1) = "A" Then
            arr1(i, 1) = arr2(i, 1)
        End If
    Next i
    ' write to sheet and clear arrays
    writeCel.Offset(1) = dte
    writeCel.Offset(1, 1).Resize(, UBound(arr1)).Value = Application.Transpose(arr1)
    Set arr1 = Nothing
    Set arr2 = Nothing
    
'-----TUESDAY-----
'add overtime and absence tuesday
    dte = .Range("AS2").Value
    arr1 = .Range("AM21:AM32").Value
    arr2 = .Range("DC21:DC32").Value
    For i = LBound(arr1) To UBound(arr1)
        If arr2(i, 1) = "A" Then
            arr1(i, 1) = arr2(i, 1)
        End If
    Next i
    ' write to sheet and clear arrays
    writeCel.Offset(2) = dte
    writeCel.Offset(2, 1).Resize(, UBound(arr1)).Value = Application.Transpose(arr1)
    Set arr1 = Nothing
    Set arr2 = Nothing
    
'-----WEDNESDAY-----
'add overtime and absence wednesday
    dte = .Range("BI2").Value
    arr1 = .Range("AO21:AO32").Value
    arr2 = .Range("DE21:DE32").Value
    For i = LBound(arr1) To UBound(arr1)
        If arr2(i, 1) = "A" Then
            arr1(i, 1) = arr2(i, 1)
        End If
    Next i
    ' write to sheet and clear arrays
    writeCel.Offset(3) = dte
    writeCel.Offset(3, 1).Resize(, UBound(arr1)).Value = Application.Transpose(arr1)
    Set arr1 = Nothing
    Set arr2 = Nothing

'-----THURSDAY-----
'add overtime and absence thursday
    dte = .Range("BY2").Value
    arr1 = .Range("AQ21:AQ32").Value
    arr2 = .Range("DG21:DG32").Value
    For i = LBound(arr1) To UBound(arr1)
        If arr2(i, 1) = "A" Then
            arr1(i, 1) = arr2(i, 1)
        End If
    Next i
    ' write to sheet and clear arrays
    writeCel.Offset(4) = dte
    writeCel.Offset(4, 1).Resize(, UBound(arr1)).Value = Application.Transpose(arr1)
    Set arr1 = Nothing
    Set arr2 = Nothing

'-----FRIDAY-----
'add overtime and absence friday
    dte = .Range("CO2").Value
    arr1 = .Range("AS21:AS32").Value
    arr2 = .Range("DI21:DI32").Value
    For i = LBound(arr1) To UBound(arr1)
        If arr2(i, 1) = "A" Then
            arr1(i, 1) = arr2(i, 1)
        End If
    Next i
    ' write to sheet and clear arrays
    writeCel.Offset(5) = dte
    writeCel.Offset(5, 1).Resize(, UBound(arr1)).Value = Application.Transpose(arr1)
    Set arr1 = Nothing
    Set arr2 = Nothing

'-----SATURDAY-----
'add overtime saturday no absence
    dte = .Range("DE2").Value
    arr1 = .Range("AU21:AU32").Value
    ' write to sheet and clear array
    writeCel.Offset(6) = dte
    writeCel.Offset(6, 1).Resize(, UBound(arr1)).Value = Application.Transpose(arr1)
    Set arr1 = Nothing

End With
      
    
    ActiveSheet.Copy After:=Worksheets("OVERTIME")
    ActiveSheet.Name = "W.E." & Format(Range("C2").Value, "dd.mm.yy")
    ActiveSheet.Shapes("Button 1").Delete
        
 
    ActiveSheet.Buttons.add(75, 150, 150, 100).Select
    With Selection
    .Name = "New Button"
    .OnAction = "Button3_Click"
    .Text = "SAVE CHANGES"
    .Font.Size = 24
    .Font.Bold = True
    ActiveSheet.Range("D5").Select
    End With
  
  
    Worksheets("ABACUS").Activate
    Range("D5:DK17").ClearContents
    Range("CY22:DJ32").ClearContents
    Range("C2").ClearContents
    Range("BP22:CE33").ClearContents
    Range("BE22:BV32").ClearContents
    Range("E3,G3,I3,K3,M3,O3,Q3,S3,U3,W3,Y3,AA3,AC3,AE3,AG3,AI3,AK3,AM3,AO3,AQ3,AS3,AU3,AW3,AY3,BA3,BC3,BE3,BG3,BI3,BK3,BM3,BO3").ClearContents
    Range("BQ3,BS3,BU3,BW3,BY3,CA3,CC3,CE3,CG3,CI3,CK3,CM3,CO3,CQ3,CS3,CU3,CW3,CY3,DA3,DC3,DE3,DG3,DI3,DK3").ClearContents
    [B25] = Range("DM2").Value
    [B26] = Range("DM2").Value
    [B27] = Range("DM2").Value
    [B28] = Range("DM2").Value
    [B29] = Range("DM2").Value
    [B30] = Range("DM2").Value
    [B31] = Range("DM2").Value
    [B32] = Range("DM2").Value
    ActiveWorkbook.Save
    
    End Sub

I have another button (Button 3) in another module which I need do to a similar operation. Hopefully I will be able to sort this one if you guys can do your magic on the above code.
Many thanks
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
On what row do these dates in column A start and what is that first date ?
 
Upvote 0
Hi. The dates in column A are rolling dates. The dates in column A start from the row after the row with the sums in N to X. So in the example below the sum for column A would be rows 287 to 314. I have added ?s in the row where the total for the sums would be.
This would then repeat for the next 28 days on so on.
Hope this helps 🤞

LATHE MAGIC ABACUS v10.xlsm
ABCDEFGHIJKLMNOPQRSTUVWX
28504/10/24FriAA
28605/10/24Sat2808028241202800
28706/10/24SunAA
28807/10/24Mon4AA4
28908/10/24TuesAA
29009/10/24Weds4AA4
29110/10/24ThurA
29211/10/24FriA
29312/10/24Sat
29413/10/24Sun00000000000
29514/10/24Mon0A000000000
29615/10/24Tues0A000A00000
29716/10/24Weds0A000A00000
29817/10/24Thur0A000000000
29918/10/24FriAA000000000
30019/10/24Sat00000000000
30120/10/24Sun00000000000
30221/10/24Mon00000000000
30322/10/24Tues00000000000
30423/10/24Weds00000000000
30524/10/24Thur00000000000
30625/10/24Fri00000000000
30726/10/24Sat00000000000
308
309
310
311
312
313
314???????????
315
316
OVERTIME
Cell Formulas
RangeFormula
N286:X286N286=SUM(C259:C286)
Cells with Conditional Formatting
CellConditionCell FormatStop If True
C1:M154,C155:F155,H155:J155,C156:J159,K155:M159,C160:M1048576Cell Value=0textNO
C167:M735Cell Valuecontains "A"textNO
 
Upvote 0
call this sub right after setting arr1 to Nothing on the Saturday

VBA Code:
Sub Formulas_28th_row()
    
    Dim frstCel As Range
    Dim fillRng As Range
    
With Sheets("OVERTIME")
    Set frstCel = .Range("N" & .Rows.Count).End(xlUp).Offset(28)
    Set fillRng = frstCel.Resize(, 11)
    frstCel.FormulaR1C1 = "=SUM(R[-27]C[-11]:RC[-11])"
    frstCel.AutoFill Destination:=fillRng
    
    With .Range("A" & fillRng.Row & ":X" & fillRng.Row).Borders(xlEdgeBottom)
        .LineStyle = xlContinuous
        .Weight = xlThick
    End With
    
End With

End Sub
 
Upvote 0
Solution
NoSparks, you have done it again, many many thanks.
I've noticed somewhere along the line, the light weight border to the bottom off the rows for each saturday for weeks 1 to 3 are no longer added when the code is run for the first three weeks out of four weeks.
I was using the code below, but when I've tried to add this line the border does not get added. Any thoughts on this?

VBA Code:
.Range("A" & writerow & ":X" & writerow).Borders(xlEdgeBottom).LineStyle = xlContinuous

Below is the mini sheet showing the border to the bottom of the saturdays for weeks 1 to 3 are missing.

LATHE MAGIC ABACUS v11.xlsm
ABCDEFGHIJKLMNOPQRSTUVWX
31301/11/24Fri00000000000
31402/11/24Sat0000000000080000000800
31503/11/24Sun00000000000
31604/11/24Mon00000000000
31705/11/24Tues00000000000
31806/11/24Weds00000000000
31907/11/24Thur00000000000
32008/11/24Fri00000000000
32109/11/24Sat00000000000
32210/11/24Sun00000000000
32311/11/24Mon00000000000
32412/11/24Tues00000000000
32513/11/24Weds00000000000
32614/11/24Thur00000000000
32715/11/24Fri00000000000
32816/11/24Sat00000000000
32917/10/24Sun44400000000
33018/10/24Mon00000000000
33119/10/24Tues00000000000
33220/10/24Weds00000000000
33321/10/24Thur00000000000
33422/10/24Fri00000000000
33523/10/24Sat00000000000
33624/11/24Sun00000000000
33725/11/24Mon00000000000
33826/11/24Tues00000000000
33927/11/24Weds00000000000
34028/11/24Thur00000000000
34129/11/24Fri00000000000
34230/11/24Sat0000000000044400000000
343
OVERTIME
Cell Formulas
RangeFormula
N342:X342,N314:X314N314=SUM(C287:C314)
Cells with Conditional Formatting
CellConditionCell FormatStop If True
C1:M154,C155:F155,H155:J155,C156:J159,K155:M159,C160:M1048576Cell Value=0textNO
C167:M735Cell Valuecontains "A"textNO


Many thanks
 
Upvote 0
I can't really see that being the solution because it would be called every week and you'll end up with 4 times more formulas than needed extending way down the sheet.

I would think you only want the formulas added probably the first week ?

When you say rolling dates, do you mean earlier rows of dates are deleted and the others move up
or do you mean that date just keep getting added to the bottom of the existing ?
I was using the code below, but when I've tried to add this line the border does not get added. Any thoughts on this?
That line says the line style is continuous (not dotted or dashed), you need a line after that for the weight of the line (ie: xlThin)
Just noticed the variable writeRow does not exist in the code you've posted here so you must be confusing different subs
 
Upvote 0
Thanks for your reply.
Below is the format I was hoping to achieve. Rolling dates refer to the dates getting added after the existing dates. Unfortunately the mini sheet does not make it that obvious each saturday has a border on the bottom of the row and the fourth saturday has a heavier weight border, this heavier border you have already supplied.
I have no idea how to add this border to the bottom of saturdays row for weeks 1 to 3.


LATHE MAGIC ABACUS v11.xlsm
ABCDEFGHIJKLMNOPQRSTUVWX
23010/08/24Sat240801228320800
23111/08/24SunAAA
23212/08/24MonAAA
23313/08/24TuesAAA
23414/08/24Weds4AAA
23515/08/24Thur4
23616/08/24Fri44
23717/08/24Sat
23818/08/24SunA44
23919/08/24MonAA
24020/08/24TuesAA
24121/08/24WedsA
24222/08/24ThurAA
24323/08/24FriAA
24424/08/24Sat4
24525/08/24Sun4AA884
24626/08/24MonAA44A4
24727/08/24Tues4AA44A
24828/08/24WedsAA44A4
24929/08/24Thur844A
25030/08/24Fri44A8
25131/08/24Sat8
25201/09/24Sun44A48
25302/09/24Mon4AA4A44
25403/09/24Tues4AA4A44
25504/09/24Weds4AA4A44
25605/09/24ThurAA884
25706/09/24FriA44
25807/09/24Sat45204056363204800
25908/09/24Sun4AA4
OVERTIME
Cell Formulas
RangeFormula
N258:X258,N230:X230N230=SUM(C203:C230)
Cells with Conditional Formatting
CellConditionCell FormatStop If True
C1:M154,C155:F155,H155:J155,C156:J159,K155:M159,C160:M1048576Cell Value=0textNO
C167:M735Cell Valuecontains "A"textNO
 
Upvote 0
If you pick a row in the first week of the 28 day (4 week) cycle to be your base starting point
you can count the Saturdays up to and including the one you are currently writing.
You can then MOD that count by 4 to figure out which of the 4 weeks you're working with.

The mini sheets are too mini to show what I asked in post 2 and the picture in your previous thread had the rows hidden
so I may be wrong using row 10 but with this you should get the idea, I believe it will be Wednesday of the first week.

This will look after inserting the formulas and the underlining of Saturday without the separate sub.
VBA Code:
Sub Data_To_OVERTIME()
    
    Dim arr1 As Variant
    Dim arr2 As Variant
    Dim i As Long
    Dim dte As Date
    Dim writeCel As Range
    Dim writeRow As Long
    
' determine where to start
With Sheets("OVERTIME")
    Set writeCel = .Range("A" & .Rows.Count).End(xlUp).Offset(1)
End With

With ActiveSheet    ' ABACUS when testing
'-----SUNDAY-----
'add overtime and absence sunday
    dte = .Range("M2").Value
    arr1 = .Range("AI21:AI32").Value
    arr2 = .Range("CY21:CY32").Value
    For i = LBound(arr1) To UBound(arr1)
        If arr2(i, 1) = "A" Then
            arr1(i, 1) = arr2(i, 1)
        End If
    Next i
    ' write to sheet and clear arrays
    writeCel = dte
    writeCel.Offset(0, 1).Resize(, UBound(arr1)).Value = Application.Transpose(arr1)
    Set arr1 = Nothing
    Set arr2 = Nothing

'-----MONDAY-----
'add overtime and absence monday
    dte = .Range("AC2").Value
    arr1 = .Range("AK21:AK32").Value
    arr2 = .Range("DA21:DA32").Value
    For i = LBound(arr1) To UBound(arr1)
        If arr2(i, 1) = "A" Then
            arr1(i, 1) = arr2(i, 1)
        End If
    Next i
    ' write to sheet and clear arrays
    writeCel.Offset(1) = dte
    writeCel.Offset(1, 1).Resize(, UBound(arr1)).Value = Application.Transpose(arr1)
    Set arr1 = Nothing
    Set arr2 = Nothing
    
'-----TUESDAY-----
'add overtime and absence tuesday
    dte = .Range("AS2").Value
    arr1 = .Range("AM21:AM32").Value
    arr2 = .Range("DC21:DC32").Value
    For i = LBound(arr1) To UBound(arr1)
        If arr2(i, 1) = "A" Then
            arr1(i, 1) = arr2(i, 1)
        End If
    Next i
    ' write to sheet and clear arrays
    writeCel.Offset(2) = dte
    writeCel.Offset(2, 1).Resize(, UBound(arr1)).Value = Application.Transpose(arr1)
    Set arr1 = Nothing
    Set arr2 = Nothing
    
'-----WEDNESDAY-----
'add overtime and absence wednesday
    dte = .Range("BI2").Value
    arr1 = .Range("AO21:AO32").Value
    arr2 = .Range("DE21:DE32").Value
    For i = LBound(arr1) To UBound(arr1)
        If arr2(i, 1) = "A" Then
            arr1(i, 1) = arr2(i, 1)
        End If
    Next i
    ' write to sheet and clear arrays
    writeCel.Offset(3) = dte
    writeCel.Offset(3, 1).Resize(, UBound(arr1)).Value = Application.Transpose(arr1)
    Set arr1 = Nothing
    Set arr2 = Nothing

'-----THURSDAY-----
'add overtime and absence thursday
    dte = .Range("BY2").Value
    arr1 = .Range("AQ21:AQ32").Value
    arr2 = .Range("DG21:DG32").Value
    For i = LBound(arr1) To UBound(arr1)
        If arr2(i, 1) = "A" Then
            arr1(i, 1) = arr2(i, 1)
        End If
    Next i
    ' write to sheet and clear arrays
    writeCel.Offset(4) = dte
    writeCel.Offset(4, 1).Resize(, UBound(arr1)).Value = Application.Transpose(arr1)
    Set arr1 = Nothing
    Set arr2 = Nothing

'-----FRIDAY-----
'add overtime and absence friday
    dte = .Range("CO2").Value
    arr1 = .Range("AS21:AS32").Value
    arr2 = .Range("DI21:DI32").Value
    For i = LBound(arr1) To UBound(arr1)
        If arr2(i, 1) = "A" Then
            arr1(i, 1) = arr2(i, 1)
        End If
    Next i
    ' write to sheet and clear arrays
    writeCel.Offset(5) = dte
    writeCel.Offset(5, 1).Resize(, UBound(arr1)).Value = Application.Transpose(arr1)
    Set arr1 = Nothing
    Set arr2 = Nothing

'-----SATURDAY-----
'add overtime saturday no absence
    dte = .Range("DE2").Value
    arr1 = .Range("AU21:AU32").Value
    ' write to sheet and clear array
    writeCel.Offset(6) = dte
    writeCel.Offset(6, 1).Resize(, UBound(arr1)).Value = Application.Transpose(arr1)
    Set arr1 = Nothing
End With
    
    '-----For formulas and/or under lining-------
    writeRow = writeCel.Offset(6, 1).Row
    With Sheets("OVERTIME")
        'Debug.Print Application.CountIf(.Range("B10:B" & writeRow), "Sat") Mod 4
        If Application.CountIf(.Range("B10:B" & writeRow), "Sat") Mod 4 = 0 Then
            .Range("N" & writeRow).FormulaR1C1 = "=SUM(R[-27]C[-11]:RC[-11])"
            .Range("N" & writeRow).AutoFill Destination:=.Range("N" & writeRow).Resize(, 11)
            With .Range("A" & writeRow & ":X" & writeRow).Borders(xlEdgeBottom)
                .LineStyle = xlContinuous
                .Weight = xlThick
            End With
        Else
            With .Range("A" & writeRow & ":X" & writeRow).Borders(xlEdgeBottom)
                .LineStyle = xlContinuous
                .Weight = xlThin
            End With
        End If
    End With
    
End Sub
 
Upvote 0
NoSparks, once again you are a genius. Despite my not very clear description of what I was hoping to achieve you have given me exactly what I was after.
Many thanks
 
Upvote 0
A final one, is there a way to center the values in columns N:X, rather than the current set to the right.
I've tried putting the code below in various places but still can't center the the columns.
VBA Code:
.HorizontalAlignment = xlCenter

Thanks
 
Upvote 0

Forum statistics

Threads
1,222,903
Messages
6,168,939
Members
452,227
Latest member
sam1121

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