the object variable or With block variable not set" or Error "91"
There are few things that I will address.
1. Regarding the error, you need to check if the object exists before you use it. For example
The line MsgBox (orivalue) should be written as
Set orivalue = find_prevconfig(i)
If Not orivalue Is Nothing Then
MsgBox orivalue.Value
Else
MsgBox "Object is Nothing"
End If
2. Your object find_prevconfig will always be Nothing even if the condition is True. And that is because of a typo. Function name is find_prevconfig but you are using find_preconfig. It is advisable to always use Option Explicit
3. Fully qualify your objects. In your code if you do not do that, then it will refer to the active sheet and the active sheet may not be the sheet that you are expecting it to be. For example ThisWorkbook.Sheets("Sheet1").Range("E590").Offset(y, x2)
4. Even though, .Value is the default property of a range when you are assigning a value or reading a value, it is advisable to use it explicitly. I personally believe it is a good habit. Will help you avoid lot of headaches in the future when you are quickly skimming the code. Set rng = Range("SomeRange") vs SomeValue = Range("SomeRange").Value or SomeValue = Range("SomeRange").Value2
5. When you are doing a string comparison, it is advisable to consider that the strings can have spaces or can be of different case. "y" is not equal to "Y". Similarly, "Y " is not equal to "Y". I, if required, use TRIM and UCASE for this purpose as shown in the code below.
Your code can be written as (UNTESTED)
Option Explicit
Function find_prevconfig(x2 As Long) As Range
Dim y As Long
Dim rng As Range
Dim ws As Worksheet
'~~> Change sheet as applicable
Set ws = ThisWorkbook.Sheets("Sheet1")
For y = 0 To 30
If Trim(UCase(ws.Range("E590").Offset(y, x2).Value2)) = "Y" Then
Set rng = ws.Range("C590").Offset(y)
Exit For
End If
Next y
Set find_prevconfig = rng
End Function
Private Sub btn_confirm_Click()
Dim orivalue As Range
Dim i As Long
Dim ws As Worksheet
'~~> Change sheet as applicable
'~~> You can also pass the worksheet as a parameter if the comparision is
'~~> in the same sheet
Set ws = ThisWorkbook.Sheets("Sheet1")
For i = 0 To 30
If ws.Range("E26").Offset(0, i).Value = ws.Range("J6").Value Then
Set orivalue = find_prevconfig(i)
'~~> Msgbox in a long loop can be very annoying. Use judiciously
If Not orivalue Is Nothing Then
'MsgBox orivalue.Value
Debug.Print orivalue.Value
Else
'MsgBox "Object is Nothing"
Debug.Print "Object is Nothing"
End If
End If
Next i
End Sub
If range("E590").Offset(y, x2) = "Y" Thencondition fails, thefind_prevconfig()function returns default value (Nothing), which is the reasonmsgbox(find_prevconfig)in the function block codes, it run perfectly and the value that i search will be found correctly. The problem is when calling and assigning the function value to an object in the event subThisworkbook.sheets("sheet1"). But still when i called the value of the function in the event sub, the same error keep poppin out.If Trim(UCase(ThisWorkbook.Sheets("Sheet1").Range("E590").Offset(y, x2).Value)) = "Y" Theninstead ofIf range("E590").Offset(y, x2) = "Y" Then?