Check for date and add other cell value to variable

jarhead58

Active Member
Joined
Sep 21, 2017
Messages
364
Office Version
  1. 2021
Platform
  1. Windows
Hey all! I am trying to check a range of cells in col a to see if they fall within this year and if so, add the value of a column/cell, say "I" in the same row to "b". Here is the code I'm trying to use.

VBA Code:
Private Sub Worksheet_Activate()
Dim a As Integer
Dim b As Integer
Dim StartDate As Long
Dim EndDate As Long
Dim totl As Long
Dim rng As Range
Dim daterng As Range

StartDate = "1/1/2021"
EndDate = "12/31/2021"

a = 0
b = 0

Set daterng = Range("A2:A366")
Set rng = Range("I2:I366")

For Each cell In rng
If daterng >= StartDate And daterng <= EndDate Then
If cell > -15 Then
        b = cell.Value + 15
        totl = totl + b
      Else
      End If
    End If
Next cell
Range("J9").Value = totl
    
End Sub
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Admins, just wanted to let you know that I am also inquiring with excelforum.com on the same subject to broaden the field. If it's a problem, feel free to remove my post. I understand that there is a lot going on so I'm not going to press anyone for answers; you all do this for free and it is greatly appreciated!
 
Upvote 0
Thanks for letting us know that you have asked this question elsewhere, can you please a link to your thread on Excel Forum.

Cross-posting (posting the same question in more than one forum) is not against our rules, but the method of doing so is covered by #13 of the Forum Rules.
 
Upvote 0
 
Upvote 0

Forum statistics

Threads
1,214,918
Messages
6,122,257
Members
449,075
Latest member
staticfluids

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