• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Error : "Object variable or With block variable not set"

Igor R.

New Member
Hi!
This is the code that supposed to find the column with given value:
Code:
Sub main()
Dim s As Range
Set s = FindValueInCellRange(ThisWorkbook.Sheets(1).Range("A4:DO4"), "01/12/2022")
End Sub

Function FindValueInCellRange(MyRange As Range, MyValue As Variant) As Range
 With MyRange
        FindValueInCellRange = .Find(What:=MyValue, After:=.Cells(.Cells.Count), LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows,       SearchDirection:=xlNext).Address(RowAbsolute:=False, ColumnAbsolute:=False)
End With
End Function

This function works perfect when i use it in spreadsheet but prompt the error above when i use it in VBA.
I searched for solution in google but all i found did not help me.

Please, what I am doing wrong?
 
Hi,​
as your function has no sense so can never ever work ‼ Does not respect the Range.Find VBA help !​
First declared as Range but returns a String (Address) … :rolleyes:
When nothing is found so another error rises 'cause you forgot to check if a Range is found (Not Is Nothing).​
 
Back
Top