Adding Days to a Date in VBA

SailorJerry7030

New Member
Joined
Apr 27, 2018
Messages
30
Here's the current code:

'SH DATA COL P = DUE DATE
Worksheets("SH Data").Range("P2").Formula = "=IF(AND(OR(J2=""NEWCLAIM2"",J2=""WEBRECVD"",J2=""KECREATED""),ISBLANK(M2)=TRUE)=TRUE,WORKDAY(D2,O2),IF(Q2=""REWORK"",WORKDAY(D2,O2)," _
& "IF(OR(Q2=""QUESTION"",Q2=""RQMT RECVD"",Q2=""ACTIONABLE PENDING""),WORKDAY(C2,O2)," _
& "IF(AND(OR(Q2=""NEW CLAIM"",Q2=""ACTIONABLE PENDING""),OR(J2=""AALSAPPROV"",J2=""DISBDONE"",J2=""OK"",J2=""PROCESSED"",J2=""CDSTIMEOUT"",J2=""AWDNOAPPRV"",J2=""NOAALSAPRV"")),WORKDAY(C2,O2)," _
& "IF(AND(ISBLANK(M2)=TRUE)=TRUE,WORKDAY(C2,O2),WORKDAY(M2,O2))))))"
'New code
Worksheets("SH Data").Range("P2").Formula = "=IF(AND(G2=""DIPAYMENTS"",J2=""PEND30""),D2+O2,"")"


Essentially it runs through the first formula, then goes back through and if the AND statement is met for G2 & J2 it adds Column D2 (Date) and O2 (in this situation "30") for 30 days. If it's not met, I want it to keep the results of the first long formula. Unfortunately, when I run it as-is, I get "Run-Time error '1004': Application-defined or object defined error. If I remove the false statement it works fine once the AND criteria is met, but everything from the first formula is overwritten with "FALSE". Any help please?
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
You start off in VBA and create formulas in the worksheet. Why?
Start from scratch and solve everything in VBA with loops and a lot of comments so the code is maintainable. Now it is not.
 
Upvote 0
You start off in VBA and create formulas in the worksheet. Why?
Start from scratch and solve everything in VBA with loops and a lot of comments so the code is maintainable. Now it is not.


Ok, first I'm not well versed in VBA code. I didn't write this (other than the new code) and just maintain it when changes are needed. Second, "Start from scratch" is not helpful. Could you maybe explain why the new line of code isn't working properly?
 
Upvote 0
Double-up the quote marks on the new line:
Rich (BB code):
"=IF(AND(G2=""DIPAYMENTS"",J2=""PEND30""),D2+O2,"""")"
 
Upvote 0
Your code is a mess! Tidy it up, i.e. start from scratch. It IS helpful!
 
Last edited by a moderator:
Upvote 0
Double-up the quote marks on the new line:
Rich (BB code):
"=IF(AND(G2=""DIPAYMENTS"",J2=""PEND30""),D2+O2,"""")"


Ok, I'll admit that was a silly mistake. However, it overwrites the first formula's results with blanks. Is there a way to code it so that the first formula's results remain there? If G2=Dipayments and J2= PEND30, add D2+O2, otherwise don't change what's currently inside the cell.
 
Upvote 0
Ok, I'll admit that was a silly mistake. However, it overwrites the first formula's results with blanks. Is there a way to code it so that the first formula's results remain there? If G2=Dipayments and J2= PEND30, add D2+O2, otherwise don't change what's currently inside the cell.
Not if you write anew formula to the cell. Since you're using VBA anyway, you could do it with a few lines of code like this:
Code:
With Worksheets("SH Data").Range("P2")
      If [G2] = "Dipayments" and [J2] = "PEND30" Then .Value = [D2] + [O2]
End With
 
Upvote 0
Not if you write anew formula to the cell. Since you're using VBA anyway, you could do it with a few lines of code like this:
Code:
With Worksheets("SH Data").Range("P2")
      If [G2] = "Dipayments" and [J2] = "PEND30" Then .Value = [D2] + [O2]
End With


It seems so simple, I dropped that code in just below the first long formula and it seems the sheet is still showing the Due Date column as adding off column C2. I should add that the condition underlined is met when G2=DIPAYMENTS and J2=PEND30, Q2 would equal ACTIONABLE PENDING. But with your formula at the end, it should overwrite it shouldn't it? I don't show anywhere else in the code where range P2 is referenced:

Worksheets("SH Data").Range("P2").Formula = "=IF(AND(OR(J2=""NEWCLAIM2"",J2=""WEBRECVD"",J2=""KECREATED""),ISBLANK(M2)=TRUE)=TRUE,WORKDAY(D2,O2),IF(Q2=""REWORK"",WORKDAY(D2,O2)," _
& "IF(OR(Q2=""QUESTION"",Q2=""RQMT RECVD"",Q2=""ACTIONABLE PENDING""),WORKDAY(C2,O2)," _
& "IF(AND(OR(Q2=""NEW CLAIM"",Q2=""ACTIONABLE PENDING""),OR(J2=""AALSAPPROV"",J2=""DISBDONE"",J2=""OK"",J2=""PROCESSED"",J2=""CDSTIMEOUT"",J2=""AWDNOAPPRV"",J2=""NOAALSAPRV"")),WORKDAY(C2,O2)," _
& "IF(AND(ISBLANK(M2)=TRUE)=TRUE,WORKDAY(C2,O2),WORKDAY(M2,O2))))))"
'New code
With Worksheets("SH Data").Range("P2")
If [G2] = "DIPAYMENTS" And [J2] = "PEND30" Then .Value = [D2] + [O2]
End With
 
Upvote 0
I have no idea what you want your code to accomplish or what your spreadsheet looks like so I can't provide much help.
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,289
Members
452,631
Latest member
a_potato

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