【问题标题】:Excel (2010) VBA: Take in a date as a variable and store itExcel(2010)VBA:将日期作为变量并将其存储
【发布时间】:2018-02-15 23:01:41
【问题描述】:

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

我有一个名为单元格的变量,我需要的信息是向左偏移两列和三列(所以 -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% 的时间都不知道自己在做什么 :)

【问题讨论】:

    标签: vba excel variables


    【解决方案1】:

    从值中删除 Set

     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
    

    【讨论】:

    • 我终于在另一个问题上看到了一些关于这个的东西(我已经在互联网上闲逛了一两个小时了)。它现在可以编译......但它也给了我一个“运行时错误 91:对象变量或未设置块变量”...... :(
    • @seadoggie01 为范围变量及其值使用不同的变量名。
    • 我不太明白,抱歉……你能再解释一下吗? @gary 的学生
    • 究竟是什么Set-s 被删除了?您的示例代码与提问者完全相同,只是它不包含 Offset 行。
    • @KennyEvitt 你是对的............我会删除这个答案............谢谢
    【解决方案2】:

    我正在使用 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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-19
      • 2022-06-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多