Assign a word to a range between two dates VBA

Isaac21

New Member
Joined
Jan 10, 2021
Messages
7
Office Version
  1. 2016
Platform
  1. Windows
Hello, I am attempting to assign the value "Senior" to the number difference between two dates in vba, as seen in the code below

basically im looking to assign a word to a range between two dates

but this vba code is not working, no error shows but it wont populate the cell in question

All help is appreciated
Thanks
Isaac

VBA Code:
Range("D2").Select
If (ActiveCell.Value > 1 / 1 / 1940) And (ActiveCell.Value < 1 / 1 / 2003) Then
    Range("J2").Value = "Senior"
    Else
    Range("J2").Value = ""
End If
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
You don't need to select. Avoid it unless no choice

VBA Code:
If Range("D2") > DateValue("1 / 1 / 1940") And Range("D2") < DateValue("1 / 1 / 2003") Then
    Range("J2").Value = "Senior"
    Else
    Range("J2").Value = ""
End If
 
Upvote 0
You don't need to select. Avoid it unless no choice

VBA Code:
If Range("D2") > DateValue("1 / 1 / 1940") And Range("D2") < DateValue("1 / 1 / 2003") Then
    Range("J2").Value = "Senior"
    Else
    Range("J2").Value = ""
End If
Thanks, I have a long list of data, useing this it only applies a name to the range from D2, how would i get it to go though a list of data and apply the name (eg D3, D4)
 
Upvote 0
You can define range and loop through the range. Something like this

VBA Code:
Dim cell As Range, rngD As Range

If cell > DateValue("1 / 1 / 1940") And cell < DateValue("1 / 1 / 2003") Then
    Range("J2").Value = "Senior"
    Else
    Range("J2").Value = ""
End If
 
Upvote 0
You can define range and loop through the range. Something like this

VBA Code:
Dim cell As Range, rngD As Range

If cell > DateValue("1 / 1 / 1940") And cell < DateValue("1 / 1 / 2003") Then
    Range("J2").Value = "Senior"
    Else
    Range("J2").Value = ""
End If
As you can tell I'm new to this, but when i try and run the new code it comes up with Run-time error '91": "Object variable or with block variable not set" any ideas?
 
Upvote 0
My bad. I did not loop anything.... hold only. I write step by step
 
Upvote 0
VBA Code:
Dim eRow&
Dim cell As Range, rngD As Range
Dim ws As Worksheet

' Assuming your worksheet name is Sheet1
Set ws = ActiveWorkbook.Sheets("Sheet1")

' Find last cell with data in column D and define range
Set cell = ws.Range("D2").End(xlDown)
Set rngD = ws.Range("D2", cell)

' Loop through range D2 to D last row
For Each cell In rngD
    If cell > DateValue("1 / 1 / 1940") And cell < DateValue("1 / 1 / 2003") Then
        Range("J" & cell.Row).Value = "Senior"
    Else
        Range("J" & cell.Row).Value = ""
    End If
Next
 
Upvote 0
Solution

Forum statistics

Threads
1,214,591
Messages
6,120,427
Members
448,961
Latest member
nzskater

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