Excel(2010)VBA:将日期作为变量输入并存储

时间:2015-05-11 18:23:16

标签: excel vba excel-vba variables

听起来很简单:只需将日期存储在从单元格中获取的变量中。这就是我真正需要的。但我一直收到“需要对象”的错误。

我有一个名为cell的变量,我需要的信息是向左偏移两列和三列(所以-2和-3使用偏移量)。我已经尝试使用字符串变量并使用CDate()进行转换,我尝试使用整数并将其存储在那里,我尝试了datevalue。我很茫然。这是我的代码的最新版本......

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

谢谢,我很抱歉这是多么的混乱......我在教自己VBA,我不知道我在做什么75%的时间:)

2 个答案:

答案 0 :(得分:2)

从值中删除设置

 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

答案 1 :(得分:0)

我正在使用Excel 2016 MSO(16.0.8730.2175)64位以及此处(已编辑版本)的代码我使用:

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)