Sunday 31 July 2016

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.


Answer



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

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...