Syntax error - Excel formulas in VBA code

Damian37

Active Member
Joined
Jun 9, 2014
Messages
301
Office Version
  1. 365
Hi Kevin,
Per our discussion, I would like to add the Excel formulas I’m currently using in Excel to my VBA code. The file I’m working on is here: All Items- CAPA Tracker - Macro Ver 4 with calculated columns.xlsm
Here are the formulas I've placed within the Excel worksheet. Please let me know if you have any questions. Thanks so much!

Retrieving Due Date from two columns (Original Due Date & Revised Due Date) - =IF(S4<>"", S4, R4)
Retrieving Date Closed. Some cells may not have a value yet. - =IF(ISBLANK(W4),"",TEXT(W4,"mm/dd/yyyy"))
Calculating # of days from Date Closed against Due Date. - =IF(ISBLANK(W4), AP4-TODAY(), AP4-AQ4)
Designating if project was "Closed On Time", "Overdue", or "In Progress". - =IF(O4= "In Progress", "In Progress", IF(AR4>0, "Completed On Time", IF(AR4<0, "Overdue",IF(AR4=0, "Completed On Time"))))


D
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Please see if the following gives you what you want. Link to the file for testing: All Items- CAPA Tracker Fields Generic shareable - Marcro Unifinished Ver 5.xlsm

VBA Code:
Option Explicit
Sub FilterMultipleCriteria_V5()
    Dim ws1 As Worksheet, ws2 As Worksheet, criteriaArray
    
    'Set the data range & criteria
    Set ws1 = Worksheets("Report Data")
    Set ws2 = Worksheets("Filtered Data")
    criteriaArray = Array("High", "Moderate-High")
    
    'Filter, copy and add formulas
    If ws1.AutoFilterMode Then ws1.AutoFilterMode = False
    With ws1.Range("A1").CurrentRegion
        .AutoFilter 5, criteriaArray, 7
        If .SpecialCells(xlCellTypeVisible).Address <> .Rows(1).Address Then
            ws2.Range("A1").CurrentRegion.Offset(1).ClearContents
            .Offset(1).Resize(.Rows.Count - 1).Copy ws2.Range("A2")
            Dim LRow As Long
            LRow = ws2.Cells.Find("*", , xlFormulas, , 1, 2).Row
            With ws2
                .Range("AP2:AP" & LRow).Formula = "=IF(S2<>"""", S2, R2)"
                .Range("AQ2:AQ" & LRow).Formula = "=IF(ISBLANK(W2),"""",W2)"
                .Range("AR2:AR" & LRow).Formula = "=IF(ISBLANK(W2), AP2-TODAY(), AP2-AQ2)"
                .Range("AS2:AS" & LRow).Formula = "=IF(O2= ""In Progress"", ""In Progress"", IF(AR2>0, ""Completed On Time"", IF(AR2<0, ""Overdue"",IF(AR2=0, ""Completed On Time""))))"
                With .Range("AP2:AS" & LRow)
                    .Value = .Value
                End With
                .Columns("AP:AQ").NumberFormat = "mm/dd/yyyy"
            End With
        End If
    End With
    ws1.AutoFilter.ShowAllData
    
End Sub
 
Upvote 0
Solution
Please see if the following gives you what you want. Link to the file for testing: All Items- CAPA Tracker Fields Generic shareable - Marcro Unifinished Ver 5.xlsm

VBA Code:
Option Explicit
Sub FilterMultipleCriteria_V5()
    Dim ws1 As Worksheet, ws2 As Worksheet, criteriaArray
  
    'Set the data range & criteria
    Set ws1 = Worksheets("Report Data")
    Set ws2 = Worksheets("Filtered Data")
    criteriaArray = Array("High", "Moderate-High")
  
    'Filter, copy and add formulas
    If ws1.AutoFilterMode Then ws1.AutoFilterMode = False
    With ws1.Range("A1").CurrentRegion
        .AutoFilter 5, criteriaArray, 7
        If .SpecialCells(xlCellTypeVisible).Address <> .Rows(1).Address Then
            ws2.Range("A1").CurrentRegion.Offset(1).ClearContents
            .Offset(1).Resize(.Rows.Count - 1).Copy ws2.Range("A2")
            Dim LRow As Long
            LRow = ws2.Cells.Find("*", , xlFormulas, , 1, 2).Row
            With ws2
                .Range("AP2:AP" & LRow).Formula = "=IF(S2<>"""", S2, R2)"
                .Range("AQ2:AQ" & LRow).Formula = "=IF(ISBLANK(W2),"""",W2)"
                .Range("AR2:AR" & LRow).Formula = "=IF(ISBLANK(W2), AP2-TODAY(), AP2-AQ2)"
                .Range("AS2:AS" & LRow).Formula = "=IF(O2= ""In Progress"", ""In Progress"", IF(AR2>0, ""Completed On Time"", IF(AR2<0, ""Overdue"",IF(AR2=0, ""Completed On Time""))))"
                With .Range("AP2:AS" & LRow)
                    .Value = .Value
                End With
                .Columns("AP:AQ").NumberFormat = "mm/dd/yyyy"
            End With
        End If
    End With
    ws1.AutoFilter.ShowAllData
  
End Sub
Hi Kevin,
I ran the updated query, however, some of the rows are coming out incorrectly. The highlighted line in the screenshot is showing how instead of calculating the # of days some rows are being populated with dates instead of numbers. Any ideas why this might be happening? Thanks.

1703187399547.png
 
Last edited:
Upvote 0
I'm away from my home laptop for a while now, but I will get on to it.
 
Upvote 0
I'm away from my home laptop for a while now, but I will get on to it.
I appreciate it. The previous code is working great, and I can continue to use the calculated columns that I created. So, no need to rush. I do really appreciate all of your help. Thank you very much.

D
 
Upvote 0
OK, not sure what happened there. When I ran the code in post #2 it ran just fine. Link to your supplied file with the macro: All Items- CAPA Tracker - Macro Ver 5 with calculated columns.xlsm
What it looked like for me:
All Items- CAPA Tracker - Macro Ver 5 with calculated columns.xlsm
ABCDEFORSWAAAIAOAPAQARAS
1Project NameControl IDObservation ID (Project Name-Observation #)Observation DescriptionCriticalityVulnerabilityCAPA StateOriginal Due DateRevised Due DateDate ClosedCAPA Start Date (Date Project Completed/Communicated)Date SubmittedUpdated DateDue DateDate Closed# of Days to completionOverdue/Completed on Time
2Generic21-99921-999-03GenericHighHighClosed-Verified30/06/202230/11/202212/1/2022 12:00 AM12/22/2021 12:00 AM5/12/202212/20/2023 3:49 PM11/30/202212/01/2022-1Overdue
3GenericE.1-20-p-00222-001-20-4GenericModerate-HighModerateClosed-Verified31/10/20239/21/2023 12:00 AM12/16/2022 12:00 AM21/09/202312/20/2023 2:54 PM10/31/202309/21/202340Completed On Time
4GenericA.1-05-b-01522-002-05-02GenericModerate-HighModerate-HighClosed-Verified30/11/202211/30/2022 12:00 AM10/28/2022 12:00 AM5/12/202212/20/2023 3:49 PM11/30/202211/30/20220Completed On Time
5GenericA.1-05-b-01522-002-05-03GenericModerate-HighModerate-HighClosed-Verified30/11/202230/01/20231/27/2023 12:00 AM10/28/2022 12:00 AM27/01/202312/11/2023 4:00 PM01/30/202301/27/20233Completed On Time
6Generic22-01021-010-2GenericHighHighClosed-Verified30/11/202211/30/2022 12:00 AM8/8/2022 12:00 AM5/12/202212/11/2023 4:00 PM11/30/202211/30/20220Completed On Time
7Generic22-026-1322-026-13-1GenericModerate-HighModerate-HighClosed-Verified31/03/202331/05/20235/22/2023 12:00 AM29/03/202312/19/2023 5:40 PM05/31/202305/22/20239Completed On Time
8Generic22-026-1322-026-13-1GenericModerate-HighModerate-HighClosed-Verified30/11/202211/30/2022 12:00 AM11/30/2022 12:00 AM12/19/2023 5:40 PM11/30/202211/30/20220Completed On Time
9Generic22-026-4122-026-41-1GenericModerate-HighModerate-HighClosed-Verified31/01/20232/3/2023 12:00 AM11/30/2022 12:00 AM3/02/202312/19/2023 5:40 PM01/31/202302/03/2023-3Overdue
10Generic22-026-4122-026-41-2GenericModerate-HighModerate-HighClosed-Verified30/11/202211/30/2022 12:00 AM11/30/2022 12:00 AM12/19/2023 5:40 PM11/30/202211/30/20220Completed On Time
11Generic22-026-4122-026-41-2GenericModerate-HighModerate-HighClosed-Verified31/01/20231/27/2023 12:00 AM11/30/2022 12:00 AM27/01/202312/19/2023 5:40 PM01/31/202301/27/20234Completed On Time
12Generic22-026-4122-026-41-2GenericModerate-HighModerate-HighClosed-Verified31/03/20231/31/2023 12:00 AM11/30/2022 12:00 AM31/01/202312/19/2023 5:40 PM03/31/202301/31/202359Completed On Time
Filtered Data
 
Upvote 0

Forum statistics

Threads
1,224,818
Messages
6,181,151
Members
453,021
Latest member
Justyna P

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