VBA range.formula2 error when using dynamic array formula SEQUENCE

markkeith

New Member
Joined
Sep 8, 2020
Messages
17
Office Version
  1. 365
Platform
  1. Windows
I'm trying to use the SEQUENCE formula in the code below but getting a run-time error.
Am I missing something in my code?

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = False
Application.ScreenUpdating = False

 If Not Intersect(Target, Range("startDate")) Is Nothing Then
  If IsDate(Range("startDate")) = True Then
   'Do nothing
   Select Case Weekday(Range("startDate"), vbMonday)
    Case 1
     Range("week").ClearContents
     Range("mon").Formula2 = "=IF(ISBLANK(startDate),"",SEQUENCE(1,days,startDate,1))"
    Case 2
     Range("week").ClearContents
     Range("tue").Formula2 = "=IF(ISBLANK(startDate),"",SEQUENCE(1,days,startDate,1))"
    Case 3
     Range("week").ClearContents
     Range("wed").Formula2 = "=IF(ISBLANK(startDate),"",SEQUENCE(1,days,startDate,1))"
    Case 4
     Range("week").ClearContents
     Range("thu").Formula2 = "=IF(ISBLANK(startDate),"",SEQUENCE(1,days,startDate,1))"
    Case 5
     Range("week").ClearContents
     Range("fri").Formula2 = "=IF(ISBLANK(startDate),"",SEQUENCE(1,days,startDate,1))"
    Case 6
     Range("week").ClearContents
     Range("sat").Formula2 = "=IF(ISBLANK(startDate),"",SEQUENCE(1,days,startDate,1))"
    Case 7
     Range("week").ClearContents
     Range("sun").Formula2 = "=IF(ISBLANK(startDate),"",SEQUENCE(1,days,startDate,1))"
   End Select
    
   Else
    MsgBox ("Please input a valid date.")
    Range("startDate").ClearContents
  End If
 End If
 
Application.EnableEvents = True
Application.ScreenUpdating = True
End Sub
 

Attachments

  • run-time error.jpg
    run-time error.jpg
    14.6 KB · Views: 12

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
When entering a formula with vba you have to double-up all the quote marks within the formula. So they should be like ..
Rich (BB code):
 Range("mon").Formula2 = "=IF(ISBLANK(startDate),"""",SEQUENCE(1,days,startDate,1))"
 
Upvote 0
Solution
When entering a formula with vba you have to double-up all the quote marks within the formula. So they should be like ..
Rich (BB code):
 Range("mon").Formula2 = "=IF(ISBLANK(startDate),"""",SEQUENCE(1,days,startDate,1))"
Thank you so much Peter, it works like a charm.
 
Upvote 0
You're welcome. Thanks for the follow-up. :)
 
Upvote 0

Forum statistics

Threads
1,215,632
Messages
6,125,913
Members
449,274
Latest member
mrcsbenson

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