Want2BExcel
Board Regular
- Joined
- Nov 24, 2021
- Messages
- 114
- Office Version
- 2016
- Platform
- Windows
My problem is that I have this macro, that should replace values in some formulas in my workbook. The formulas constructed it correct (I tried to copy it from the Immediate window directly into the cell and I get no errors). But when running the macro I get:
Formula Error: Error applying formula....Faulty Formula:
=IF(F$10="K-S";
IF(WEEKDAY(F$7;2)=1;9;
IF(WEEKDAY(F$7;2)=2;9;
IF(WEEKDAY(F$7;2)=3;9;
IF(WEEKDAY(F$7;2)=4;9;
IF(WEEKDAY(F$7;2)=5;9;
IF(WEEKDAY(F$7;2)=6;9;
IF(WEEKDAY(F$7;2)=7;9
)))))));
(F$9-F$8)*24)
I have tried using "Formula" both with commas and semicolons instead of "FormulaLocal" and semicolons, but my regional version of Excel 2016 requires the use of semicolons! So now I'm out of ideas. Hope someone have an idea of what is going on. IT SHOULD WORK
This is the code:
The Debug.Print also show the correct formula
New Formula:
Formula Error: Error applying formula....Faulty Formula:
=IF(F$10="K-S";
IF(WEEKDAY(F$7;2)=1;9;
IF(WEEKDAY(F$7;2)=2;9;
IF(WEEKDAY(F$7;2)=3;9;
IF(WEEKDAY(F$7;2)=4;9;
IF(WEEKDAY(F$7;2)=5;9;
IF(WEEKDAY(F$7;2)=6;9;
IF(WEEKDAY(F$7;2)=7;9
)))))));
(F$9-F$8)*24)
I have tried using "Formula" both with commas and semicolons instead of "FormulaLocal" and semicolons, but my regional version of Excel 2016 requires the use of semicolons! So now I'm out of ideas. Hope someone have an idea of what is going on. IT SHOULD WORK
This is the code:
VBA Code:
Sub ArbejdstidsNORM()
Dim ws As Worksheet
Dim dayOfWeek As Integer
Dim col As Integer
Dim row As Integer
Dim currentFormula As String
Dim newFormula As String
Dim workHours(1 To 7) As String
Dim columnLetter As String
Dim closeParentheses As String
workHours(1) = InputBox("Enter work hours for Monday (e.g., '7,4'):", "Monday Work Hours")
workHours(2) = InputBox("Enter work hours for Tuesday (e.g., '7,4'):", "Tuesday Work Hours")
workHours(3) = InputBox("Enter work hours for Wednesday (e.g., '7,4'):", "Wednesday Work Hours")
workHours(4) = InputBox("Enter work hours for Thursday (e.g., '7'):", "Thursday Work Hours")
workHours(5) = InputBox("Enter work hours for Friday (e.g., '6'):", "Friday Work Hours")
workHours(6) = InputBox("Enter work hours for Saturday (e.g., '1,8'):", "Saturday Work Hours")
workHours(7) = InputBox("Enter work hours for Sunday (e.g., '0'):", "Sunday Work Hours")
For Each ws In ThisWorkbook.Worksheets
For row = 28 To 32
For col = 6 To 42
columnLetter = Split(ws.Cells(1, col).Address, "$")(1)
If ws.Cells(row, col).HasFormula Then
currentFormula = ws.Cells(row, col).FormulaLocal
If InStr(currentFormula, "IF(WEEKDAY") > 0 Then
newFormula = "=IF(" & columnLetter & "$10=""K-S"";" & vbCrLf
For dayOfWeek = 1 To 6
newFormula = newFormula & _
"IF(WEEKDAY(" & columnLetter & "$7;2)=" & dayOfWeek & ";" & workHours(dayOfWeek) & ";" & vbCrLf
Next dayOfWeek
newFormula = newFormula & _
"IF(WEEKDAY(" & columnLetter & "$7;2)=7;" & workHours(7) & vbCrLf
closeParentheses = String(7, ")")
newFormula = newFormula & closeParentheses & ";" & vbCrLf & "(" & columnLetter & "$9-" & columnLetter & "$8)*24)"
Debug.Print "New Formula: " & newFormula
On Error GoTo FormulaError
ws.Cells(row, col).FormulaLocal = newFormula
On Error GoTo 0
End If
End If
Next col
Next row
Next ws
Exit Sub
FormulaError:
MsgBox "Error applying formula in " & ws.Name & " at cell " & ws.Cells(row, col).Address & vbCrLf & "Faulty Formula: " & newFormula, vbExclamation, "Formula Error"
On Error GoTo 0
End Sub
The Debug.Print also show the correct formula
New Formula:
Excel Formula:
=IF(F$10="K-S";
IF(WEEKDAY(F$7;2)=1;9;
IF(WEEKDAY(F$7;2)=2;9;
IF(WEEKDAY(F$7;2)=3;9;
IF(WEEKDAY(F$7;2)=4;9;
IF(WEEKDAY(F$7;2)=5;9;
IF(WEEKDAY(F$7;2)=6;9;
IF(WEEKDAY(F$7;2)=7;9
)))))));
(F$9-F$8)*24)
Last edited: