Object Variable or With Block variable not set error message

SunderlandUK

New Member
Joined
Mar 7, 2011
Messages
12
Hi

I'm trying to use Excel as a cricket game and have found snippets of cricket related code on the internet and it seems to work. A random number is generated in cell C2 and is allocated to a batsman (bat number in cell M9). The corresponding numbers are entered in a table in a row next to their name. However, I would like to have this table in Sheet 2 - when I try and adapt the code, I get a "Object variable or With Block variable not set" error message for the code line c.value = Range("C2"). I don't know hoe to fix this.

1613569042537.png


Sub InsertScore()

currentbatrow = Range("M9")
If Range("C6") = 10 Then
Else

Set c = Range("E" & currentbatrow & ":" & "R" & currentbatrow).Find(what:="", LookIn:=xlValues, SearchOrder:=xlByRows)
If c Is Nothing Then
Range("M9").Value = Range("M9").Value + 1

currentbatrow = Range("M9")

Set c = Range("E" & currentbatrow & ":" & "R" & currentbatrow).Find(what:="", LookIn:=xlValues, SearchOrder:=xlByRows)

End If

c.Value = Range("C2")

If c.Value = "Bowled" Or c.Value = "Caught" Or c.Value = "LBW" Or c.Value = "Stumped" Then Range("M9").Value = Range("M9").Value + 1

c.Activate
End If

End Sub
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
try this
VBA Code:
Sub InsertScore()
    With Sheets("Sheet2")
        currentbatrow = .Range("M9")
        If .Range("C6") = 10 Then
        Else
            On Error Resume Next
            Set c = .Range("E" & currentbatrow & ":" & "R" & currentbatrow).Find(what:="", LookIn:=xlValues, SearchOrder:=xlByRows)
            On Error GoTo 0
            If c Is Nothing Then
                .Range("M9").Value = .Range("M9").Value + 1
                currentbatrow = .Range("M9")
                On Error Resume Next
                Set c = .Range("E" & currentbatrow & ":" & "R" & currentbatrow).Find(what:="", LookIn:=xlValues, SearchOrder:=xlByRows)
                On Error GoTo 0
            End If

            c.Value = .Range("C2")
            If c.Value = "Bowled" Or c.Value = "Caught" Or c.Value = "LBW" Or c.Value = "Stumped" Then Range("M9").Value = Range("M9").Value + 1
            c.Activate
        End If
    End With
End Sub
 
Upvote 0
Try
Replacing
VBA Code:
c.Activate
With
VBA Code:
If Not c Is Nothing Then
    c.Parent.Activate
    c.Activate
End If
 
Upvote 0
<
Try
Replacing
VBA Code:
c.Activate
With
VBA Code:
If Not c Is Nothing Then
    c.Parent.Activate
    c.Activate
End If
That certainly stopped the error message. The cursor went to cell F1 on sheet 2 but inserted no value and would not move
 
Upvote 0
post the whole of your sub if you want further help
thanks
 
Upvote 0
Original Code -

VBA Code:
Sub InsertScore()

currentbatrow = Range("M9")
If Range("C6") = 10 Then
Else
Set c = Range("E" & currentbatrow & ":" & "R" & currentbatrow).Find(what:="", LookIn:=xlValues, SearchOrder:=xlByRows)
If c Is Nothing Then
Range("M9").Value = Range("M9").Value + 1
currentbatrow = Range("M9")
Set c = Range("E" & currentbatrow & ":" & "R" & currentbatrow).Find(what:="", LookIn:=xlValues, SearchOrder:=xlByRows)


End If
c.Value = Range("C2")
If c.Value = "Bowled" Or c.Value = "Caught" Or c.Value = "LBW" Or c.Value = "Stumped" Then Range("M9").Value = Range("M9").Value + 1

c.Activate
End If

End Sub
 
Upvote 0
I am puzzled that you posted the original code - how does that help me solve your current issue?
 
Upvote 0
Sorry - new code

VBA Code:
Sub InsertScore2()
    With Sheets("Sheet2")
        currentbatrow = .Range("M9")
        If .Range("C6") = 10 Then
        Else
            On Error Resume Next
            Set c = .Range("E" & currentbatrow & ":" & "R" & currentbatrow).Find(what:="", LookIn:=xlValues, SearchOrder:=xlByRows)
            On Error GoTo 0
            If c Is Nothing Then
                .Range("M9").Value = .Range("M9").Value + 1
                currentbatrow = .Range("M9")
                On Error Resume Next
                Set c = .Range("E" & currentbatrow & ":" & "R" & currentbatrow).Find(what:="", LookIn:=xlValues, SearchOrder:=xlByRows)
                On Error GoTo 0
            End If

            c.Value = .Range("C2")
            If c.Value = "Bowled" Or c.Value = "Caught" Or c.Value = "LBW" Or c.Value = "Stumped" Then Range("M9").Value = Range("M9").Value + 1
            If Not c Is Nothing Then
                c.Parent.Activate
                c.Activate
                End If
            End If
    End With
End Sub
 
Upvote 0
Your code runs for me without any issue
What does the new message box return
VBA Code:
Sub InsertScore2()
    With Sheets("Sheet2")
        currentbatrow = .Range("M9")
        If .Range("C6") = 10 Then
        Else
            On Error Resume Next
            Set c = .Range("E" & currentbatrow & ":" & "R" & currentbatrow).Find(what:="", LookIn:=xlValues, SearchOrder:=xlByRows)
            On Error GoTo 0
            If c Is Nothing Then
                .Range("M9").Value = .Range("M9").Value + 1
                currentbatrow = .Range("M9")
                On Error Resume Next
                Set c = .Range("E" & currentbatrow & ":" & "R" & currentbatrow).Find(what:="", LookIn:=xlValues, SearchOrder:=xlByRows)
                On Error GoTo 0
            End If

On Error Resume Next
            c.Value = .Range("C2")
            If c.Value = "Bowled" Or c.Value = "Caught" Or c.Value = "LBW" Or c.Value = "Stumped" Then Range("M9").Value = Range("M9").Value + 1
MsgBox c.Address(0, 0) & vbCr & c.Parent.Name & vbCr & c.Value
            If Not c Is Nothing Then
                c.Parent.Activate
                c.Activate
                End If
            End If
    End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,563
Messages
6,125,572
Members
449,237
Latest member
Chase S

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top