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"
The code, with the great effort from you guys, is
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
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 | |||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | |||
257 | 06/09/24 | Fri | A | 4 | 4 | ||||||||||||||||||||||
258 | 07/09/24 | Sat | 4 | 52 | 0 | 4 | 0 | 56 | 36 | 32 | 0 | 48 | 0 | 0 | |||||||||||||
259 | 08/09/24 | Sun | 4 | A | A | 4 | |||||||||||||||||||||
260 | 09/09/24 | Mon | 4 | A | 4 | 4 | A | 4 | |||||||||||||||||||
261 | 10/09/24 | Tues | 4 | A | 4 | 4 | A | 4 | |||||||||||||||||||
262 | 11/09/24 | Weds | A | 4 | 4 | 4 | A | 4 | |||||||||||||||||||
263 | 12/09/24 | Thur | 4 | 4 | A | ||||||||||||||||||||||
264 | 13/09/24 | Fri | 4 | 4 | A | ||||||||||||||||||||||
265 | 14/09/24 | Sat | |||||||||||||||||||||||||
266 | 15/09/24 | Sun | 4 | A | |||||||||||||||||||||||
267 | 16/09/24 | Mon | A | A | A | 4 | 4 | 4 | |||||||||||||||||||
268 | 17/09/24 | Tues | A | A | |||||||||||||||||||||||
269 | 18/09/24 | Weds | 4 | A | A | ||||||||||||||||||||||
270 | 19/09/24 | Thur | 4 | A | 4 | ||||||||||||||||||||||
271 | 20/09/24 | Fri | A | A | 4 | ||||||||||||||||||||||
272 | 21/09/24 | Sat | |||||||||||||||||||||||||
273 | 22/09/24 | Sun | A | ||||||||||||||||||||||||
274 | 23/09/24 | Mon | A | ||||||||||||||||||||||||
275 | 24/09/24 | Tues | A | A | |||||||||||||||||||||||
276 | 25/09/24 | Weds | A | ||||||||||||||||||||||||
277 | 26/09/24 | Thur | 4 | A | 4 | ||||||||||||||||||||||
278 | 27/09/24 | Fri | 4 | A | 4 | ||||||||||||||||||||||
279 | 28/09/24 | Sat | |||||||||||||||||||||||||
280 | 29/09/24 | Sun | 4 | ||||||||||||||||||||||||
281 | 30/09/24 | Mon | A | A | 4 | ||||||||||||||||||||||
282 | 01/10/24 | Tues | A | A | |||||||||||||||||||||||
283 | 02/10/24 | Weds | A | A | |||||||||||||||||||||||
284 | 03/10/24 | Thur | A | A | |||||||||||||||||||||||
285 | 04/10/24 | Fri | A | A | |||||||||||||||||||||||
286 | 05/10/24 | Sat | 28 | 0 | 8 | 0 | 28 | 24 | 12 | 0 | 28 | 0 | 0 | ||||||||||||||
287 | 06/10/24 | Sun | A | A | |||||||||||||||||||||||
OVERTIME |
Cell Formulas | ||
---|---|---|
Range | Formula | |
N286:X286,N258:X258 | N258 | =SUM(C231:C258) |
Cells with Conditional Formatting | ||||
---|---|---|---|---|
Cell | Condition | Cell Format | Stop If True | |
C1:M154,C155:F155,H155:J155,C156:J159,K155:M159,C160:M1048576 | Cell Value | =0 | text | NO |
C167:M735 | Cell Value | contains "A" | text | NO |
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