Formula Error: Error applying formula with VBA even though it works manually

Want2BExcel

Board Regular
Joined
Nov 24, 2021
Messages
114
Office Version
  1. 2016
Platform
  1. 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:
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:

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
Instead of vbCRlF use Chr(10)

Ex:

Rich (BB code):
                            newFormula = "=IF(" & columnLetter & "$10=""K-S"";" & Chr(10)
 
Upvote 0
Solution
You're welcome!

vbCrLf is actually two characters, a carriage return and a line feed. You can put line feeds in formulas to break the line (code 10) but it doesn't like the carriage return (code13).
 
Upvote 0

Forum statistics

Threads
1,222,830
Messages
6,168,507
Members
452,194
Latest member
Lowie27

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