简体   繁体   中英

Set range using a variable for the row - Excel VBA

Short and (hopefully) simple question, I have searched a worksheet range to find a particular reference number and then want to search a range in that row for the first blank cell from the left, using the code snippet below (which I developed from this question: 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

However, the Set Searchrange line is throwing a

Runtime 424 - object required error

but as I understand it, I have written the line correctly.

Any insight as to what I've done wrong would be appreciated.

This should work for you:

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

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