Error when using multiple cells in a Range command.

ayman110229

New Member
Joined
Aug 31, 2022
Messages
5
Office Version
  1. 2016
Platform
  1. Windows
Hi,
I'm new to VBA and still learning. I'm trying to use VBA to create alar Pop-ups for Vaccination Due Dates but whenever I use a range of cells the macro wont run from some reason. Here's what I mean:

Option Explicit
Sub Due_Date()
Dim DueDate_Col As Range
Dim Due As Range
Dim PopUp_Notification As String
Set DueDate_Col = Range("D9:D54")
For Each Due In DueDate_Col
If Due <> "" And Date >= Due - Range("F9:F54") Then
PopUp_Notification = PopUp_Notification & " " & Due.Offset(0, -2)
End If
Next Due
If PopUp_Notification = "" Then
MsgBox "No Vaccinations are Due."
Else: MsgBox "These Dogs Need to be Vaccinated: " & PopUp_Notification
End If
End Sub

The bold underlined part always comes back with an error. But it works only if I choose 1 cell for example "F9" how can I fix this.

Thank you
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Welcome to the Board!

I am afriad that your code does not seem to make much sense to me, specifically this part:
VBA Code:
Date >= Due - [B][U]Range("F9:F54")[/U][/B][U]
[/U]
Can you explian exactly what is in cell F9:F54, and what exactly you are trying to do there?
 
Upvote 0
Welcome to the Board!

I am afriad that your code does not seem to make much sense to me, specifically this part:
VBA Code:
Date >= Due - [B][U]Range("F9:F54")[/U][/B][U]
[/U]
Can you explian exactly what is in cell F9:F54, and what exactly you are trying to do there?
The range F9:F54 contains the value of how many days passed from the vaccination due date.
 
Upvote 0
The range F9:F54 contains the value of how many days passed from the vaccination due date.
Book1.xlsx
BCDEFG
6Worm Treatments
7DogNext DueDays Due
8
9Aros26/08/20225
10
11Bara31/08/20220
12
13Ben26/09/2022-26
14
15Bengo26/09/2022-26
16
17Cheetah26/09/2022-26
18
19Cizer26/09/2022-26
20
21Dusty26/09/2022-26
22
23Dzurko26/09/2022-26
24
25Jacksy26/09/2022-26
26
27Juliet26/09/2022-26
28
29Leetah26/09/2022-26
30
31Linda26/09/2022-26
32
33Max26/09/2022-26
34
35Naomi26/09/2022-26
36
37Oakley26/09/2022-26
38
39Oliver26/09/2022-26
40
41Rony26/09/2022-26
42
43Santos26/09/2022-26
44
45Simba26/08/20225
46
47Toby26/09/2022-26
48
49Twicksy26/08/20225
50
51Twister27/08/20224
52
53Yogi26/09/2022-26
54
Worming Due Dates
Cell Formulas
RangeFormula
F9,F53,F51,F49,F47,F45,F43,F41,F39,F37,F35,F33,F31,F29,F27,F25,F23,F21,F19,F17,F15,F13,F11F9=TODAY()-D9
 
Upvote 0
Welcome to the Board!

I am afriad that your code does not seem to make much sense to me, specifically this part:
VBA Code:
Date >= Due - [B][U]Range("F9:F54")[/U][/B][U]
[/U]
Can you explian exactly what is in cell F9:F54, and what exactly you are trying to do there?
Book1.xlsx
BCDEFG
6Worm Treatments
7DogNext DueDays Due
8
9Aros26/08/20225
10
11Bara31/08/20220
12
13Ben26/09/2022-26
14
15Bengo26/09/2022-26
16
17Cheetah26/09/2022-26
18
19Cizer26/09/2022-26
20
21Dusty26/09/2022-26
22
23Dzurko26/09/2022-26
24
25Jacksy26/09/2022-26
26
27Juliet26/09/2022-26
28
29Leetah26/09/2022-26
30
31Linda26/09/2022-26
32
33Max26/09/2022-26
34
35Naomi26/09/2022-26
36
37Oakley26/09/2022-26
38
39Oliver26/09/2022-26
40
41Rony26/09/2022-26
42
43Santos26/09/2022-26
44
45Simba26/08/20225
46
47Toby26/09/2022-26
48
49Twicksy26/08/20225
50
51Twister27/08/20224
52
53Yogi26/09/2022-26
54
Worming Due Dates
Cell Formulas
RangeFormula
F9,F53,F51,F49,F47,F45,F43,F41,F39,F37,F35,F33,F31,F29,F27,F25,F23,F21,F19,F17,F15,F13,F11F9=TODAY()-D9
 
Upvote 0
OK, so each cycle should line up, so that each instance in D9:D54 looks at column F of that same row, right (i.e. D9 looks at F9, D10 looks at F10, D11 looks at F11, etc), right?
If so, change this line:
VBA Code:
If Due <> "" And Date >= Due - Range("F9:F54") Then
to this:
VBA Code:
If Due <> "" And Date >= Due - Due.Offset(0, 2) Then

And I highly, HIGHLY recommend NOT using merged cells... EVER!
They are probably the single worst feature of Excel and cause tons of issues, especially for things like VBA.

If merging cells across single rows, it is better to use the "Center Across Selection" formatting option shown here: Tom’s Tutorials For Excel: Using Center Across Selection Instead of Merging Cells – Tom Urtis
 
Upvote 0
Solution
OK, so each cycle should line up, so that each instance in D9:D54 looks at column F of that same row, right (i.e. D9 looks at F9, D10 looks at F10, D11 looks at F11, etc), right?
If so, change this line:
VBA Code:
If Due <> "" And Date >= Due - Range("F9:F54") Then
to this:
VBA Code:
If Due <> "" And Date >= Due - Due.Offset(0, 2) Then

And I highly, HIGHLY recommend NOT using merged cells... EVER!
They are probably the single worst feature of Excel and cause tons of issues, especially for things like VBA.

If merging cells across single rows, it is better to use the "Center Across Selection" formatting option shown here: Tom’s Tutorials For Excel: Using Center Across Selection Instead of Merging Cells – Tom Urtis
Thank you sir this definitely solved and I'll check out that tutorial.
 
Upvote 0
You are welcome.

I hope my change makes sense.
You can use the OFFSET function to move from whatever cell you are on. We want to move two columns to the right to get to column F.
 
Upvote 0

Forum statistics

Threads
1,214,649
Messages
6,120,731
Members
448,987
Latest member
marion_davis

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