VBA and formula help for simple code/formula

ItalianPlatinum

Well-known Member
Joined
Mar 23, 2017
Messages
793
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Hello - i have two issues that i am trying to solve for and looking for a little bit of guidance.

  1. A formula to pull Prior business day. I thought the formula I have would work but when I add a holiday on my list of say 12/27/22 (TEST) it should pull 12/23/22. seeing today is 12/28/22, yesterday is a holiday and day prior to yesterday is 12/26/22 (holiday). then weekend of 12/24 - 12/25. So 12/23/22 is valid
  2. When I insert the formula to calculate via VBA it fails but when I insert the formula into the range as a formula it works? what could i be doing wrong?
Book2
ABCDEF
1Formula20221226
2Value#VALUE!
3
4
5
Sheet1
Cell Formulas
RangeFormula
B1B1=TEXT(WORKDAY(TODAY(),-1,[HOLIDAYS.xlsx]Sheet1!$B:$B),"YYYYMMDD")


HOLIDAYS.xlsx
ABC
1Christmas Day12/26/2002CLOSED
2TEST12/27/2022TEST
3New Year’s Day1/1/2023CLOSED
4Martin Luther King, Jr. Day1/18/2023CLOSED
5Presidents Day2/15/2023CLOSED
6Good Friday4/2/2023CLOSED
7Memorial Day5/31/2023CLOSED
8Independence Day7/5/2023CLOSED
9Labor Day9/6/2023CLOSED
10Thanksgiving Day11/25/2023CLOSED
11Christmas Day12/24/2023CLOSED
Sheet1


VBA Code:
Option Explicit
Sub FormulaHolidays()

With Sheets("sheet1")
    .Range("B1").Formula = "=TEXT(WORKDAY(TODAY(),-1,[HOLIDAYS.xlsx]Sheet1!$B:$B),""YYYYMMDD"")"
    .Range("B2").Value = .[TEXT(WORKDAY(TODAY(),-1,[HOLIDAYS.xlsx]Sheet1!$B:$B),"YYYYMMDD"]
End With

End Sub
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
You would need 26th in your holiday range as well if im understanding.
 
Upvote 0
It is, isnt it? B:B. I renamed the holidays sheet to sheet2 to avoid that confusion

=TEXT(WORKDAY(TODAY(),-1,'D:\User\Desktop\VBA TESTING\[HOLIDAYS.xlsx]Sheet2'!$B1:$B10000),"YYYYMMDD")
 
Upvote 0
Well with what you have there i would just have your 2nd line as

.Range("B2").Value = .Range("B1").Value
 
Upvote 0
So I have that as a workaround but I didnt know if there was a way to make it work. or do i need really to go the approach of putting the formula in the range then applying its value over it.
 
Upvote 0
To be honest thats pretty much the way id always do it. Cant see the gain in the evaluate method here.
 
Upvote 0

Forum statistics

Threads
1,214,989
Messages
6,122,622
Members
449,093
Latest member
catterz66

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