【问题标题】:Set range using a variable for the row - Excel VBA使用行的变量设置范围 - Excel VBA
【发布时间】:2017-03-09 22:49:53
【问题描述】:

简短且(希望如此)简单的问题,我已经搜索了一个工作表范围以找到一个特定的参考号,然后想在该行中的一个范围内搜索左侧的第一个空白单元格,使用下面的代码 sn-p (我从这个问题开发的:mrExcel help site)

Set projectSearchRange = Sheets("Milestones").Range("A:A").Find(projectref, , xlValues, xlWhole)

current_row = projectSearchRange.Row

Set searchrange = Sheets("Milestones").Range(Sheets("Milestones").Cells(current_row, 2), _
Sheets("Milestones").Cells(current_row, 23)).SpecialCells(xlCellTypeBlanks).Cells(1).Activate

milestoneduedate = ActiveCell.Offset(, 1).Value

但是,Set Searchrange 行抛出了一个

运行时 424 - 需要对象错误

但据我了解,我已经正确地写了这行。

任何关于我做错了什么的见解将不胜感激。

【问题讨论】:

  • 您需要声明所有范围对象的父对象,包括Cells(Set searchrange = Sheets("Milestones").Range(Sheets("Milestones").Cells(current_row, 2), Sheets("Milestones").Cells(current_row, 23)).SpecialCells(xlCellTypeBlanks).Cells(1).Activate
  • @ScottCraner - 谢谢你,但不幸的是它仍然抛出完全相同的错误。
  • 出错时Current_row的值是多少?
  • @ScottCraner 3. 我想知道 .Activate 之前末尾的“cells(1)”是否会触发它?

标签: excel vba


【解决方案1】:

这应该适合你:

Function MilestoneDueDate() As Variant

Dim projectSearchRange As Range
Dim Current_Row As Long
Dim SearchRange As Range

With Sheets("Milestones")
    Set projectSearchRange = .Range("A:A").Find(projectref, , xlValues, xlWhole)
    Current_Row = projectSearchRange.Row
    Set SearchRange = .Range(Cells(Current_Row, 2), Cells(Current_Row, 23)) _
       .SpecialCells(xlCellTypeBlanks).Cells(1)
End With

MilestoneDueDate = SearchRange.Offset(0, 1).Value

End Function

【讨论】:

  • 很好地删除了 424 错误,现在它在“set searchrange”行导致应用程序定义或对象定义错误。
  • 这可能是由于您定义的范围内没有任何空白单元格造成的。第 23 列是硬编码的。将最后一列更改为 100。这应该会找到一个空单元格。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-01
相关资源
最近更新 更多