简体   繁体   中英

Excel (2010) VBA: Take in a date as a variable and store it

Sounds simple enough: just store a date in a variable that is taken from a cell. That's all I really need. But I keep getting a 'Object required' error.

I have a variable called cell, and the information I need is offset two and three columns to the left (so -2 and -3 using offset). I have tried using a string variable and converting it with CDate(), I have tried using an integer and storing it there, I tried datevalue. I am at a loss. Here is the latest revision of my code...

Function daysBetween(percent, quarters, cell As Range) As Boolean
'this function returns true if the date is past the allotted time for the group

cell.Select
Dim target As String
Dim issue As String
Dim targetCell As Range
Dim issueCell As Range
Set targetCell = ActiveCell.Value
Set targetCell = targetCell.Offset(0, -2)
Set issueCell = ActiveCell.Value
Set issueCell = issueCell.Offset(0, -3)
Set issue = DateValue(issueCell).Value
Set target = DateValue(targerCell).Value
If ((target - issue - (Date - target)) / (target - issue)) > (percent * quarters) Then
    daysBetween = True
End If
End Function

Thank you, I'm sorry about how messy it is... I am teaching myself VBA, I don't know what I'm doing 75% of the time :)

Drop the Set s from the values:

 Function daysBetween(percent, quarters, cell As Range) As Boolean
'this function returns true if the date is past the allotted time for the group

cell.Select
Dim target As String
Dim issue As String
Dim targetCell As Range
Dim issueCell As Range
Set targetCell = ActiveCell
Set targetCell = targetCell.Offset(0, -2)
Set issueCell = ActiveCell
Set issueCell = issueCell.Offset(0, -3)
issue = DateValue(issueCell).Value
target = DateValue(targerCell).Value
If ((target - issue - (Date - target)) / (target - issue)) > (percent * quarters) Then
    daysBetween = True
End If
End Function

I'm using Excel 2016 MSO (16.0.8730.2175) 64-bit and here's (an edited version of) the code I'm using:

Dim worksheet_ As Worksheet
Set worksheet_ = Worksheets("NotTheNameOfMyWorksheet")

Dim columnNumber As Integer
Dim rowNumber As Integer

columnNumber = 1
rowNumber = 314

Dim cellValue As Variant
cellValue = worksheet_.Cells(rowNumber, columnNumber).Value

Dim date_ As Date
date_ = CDate(cellValue)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM