Problem with my "If" Statement. (VBA)

Cuylaerts

New Member
Joined
May 30, 2017
Messages
39
Hello,

i would like my vba to check Dates on my worksheet if they are far away from each other but i seem to get an error in my formula.
this is what it looks like:

If ((Workday(Range("$E$"),1)-Workday(Range("$G$"),1))*Worksheet("ignore").Range("C20"))>Range("$F$") then
msgbox("test")
end if

what this would do is:

substract 2 dates in my worksheet and multiply it by "c20" on my ignore sheet (now i have the time in hours instead of days), then it compares those hours to "Range("$F$")" , if i have more hours then "Range("$F$") then i want a messagebox to appear.

thank you.
 

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"
...but i seem to get an error in my formula.
this is what it looks like:

If ((Workday(Range("$E$"),1)-Workday(Range("$G$"),1))*Worksheet("ignore").Range("C20"))>Range("$F$") then
msgbox("test")
end if
Two things...

1) You do not need to use $ signs for the argument to the Range object in VBA (they only apply to formulas on the worksheet).

2) The red highlighted addresses do not have row numbers.
 
Last edited:
Upvote 0
Update:
formula is now:
If ((WorksheetFunction.WorkDay(Range("$E$"), 1) - WorksheetFunction.WorkDay(Range("$G$"), 1)) * ActiveWorkbook.Worksheets("ignore").Range("C20")) > Range("$F$") Then

this removed a error, but stil not working.
 
Upvote 0
Hi Rick,

i deleted the $ signs.
i want the formula to work on the active row.
Then I would place the row number for the active cell in a variable and use Cells instead of Range to specify the cell references. Assuming your code line is correctly constructed, something like this...
Code:
[table="width: 500"]
[tr]
	[td]RowNum = ActiveCell.Row
If ((WorksheetFunction.WorkDay(Cells(RowNum, "E"), 1) - WorksheetFunction.WorkDay(Cells(RowNum, "G"), 1)) * ActiveWorkbook.Worksheets("ignore").Range("C20")) > Cells(RowNum, "F") Then[/td]
[/tr]
[/table]
 
Upvote 0

Forum statistics

Threads
1,214,858
Messages
6,121,956
Members
449,057
Latest member
FreeCricketId

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