David Van der Vieren
David Van der Vieren

Reputation: 275

Runtime error "424: Object Required"

I am having difficulties with this code. I am trying to make it so that based on the value of cell D25 the value of F25 will change. I made the code in VBA (don’t know if this is actually the best way) and it is giving me a 424 runtime error: Object Required. Could someone please point me in the right direction?

Sub Storage_vesel_Controle()

   Sheets("NSR Form").Select

   If Range("D25") = "1" Then Range("F25").Select.Value = "0"

   If Range("D25") = "2" Then Range("F25").Select.Value = ".95"

   If Range("D25") = "3" Then Range("F25").Select.Paste = ".98"

End Sub

Also, what do I need to add to make the code "always running"... in loop I think?

Upvotes: 1

Views: 3634

Answers (2)

David Zemens
David Zemens

Reputation: 53623

Clean it up a little bit. Using a Select Case statement will be easier to work with than mutliple If/Then statements.

Sub Storage_vesel_Controle()
    With Sheets("NSR Form")
        .Activate  '<this line is not actually necessary unless you want to be on the sheet.
        Select Case .Range("D25").Value
            Case 1
                .Range("F25").Value = 0
            Case 2
                .Range("F25").Value = 0.95
            Case 3
                .Range("F25").Paste = 0.98
            Case Else
                'do nothing, or, modify as needed
        End Select
    End With
End Sub

Upvotes: 1

Siddharth Rout
Siddharth Rout

Reputation: 149287

Further to my comment above, I wasn't planning on posting an answer but saw few things which I think I will bring to your notice and comments wouldn't accommodate so much of text.

I would recommend not using .Select to select the sheet but work with the sheet directly (See Below code) You might also want to see this link

Sub Storage_vesel_Controle()
    With Sheets("NSR Form")
        Select Case .Range("D25").Value
           Case 1: .Range("F25").Value = 0
           Case 2: .Range("F25").Value = 0.95
           Case 3: .Range("F25").Value = 0.98
        End Select
    End With
End Sub

EDIT:

Regarding your edit. You can use the Worksheet_Change Event to ensure that the values of F25 gets automatically updated or if you want a non VBA solution then simply use a Formula in F25

If you are interested in Worksheet_Change then see this. Else a simple formula like this in F25 will suffice your needs :)

=IF(D25=1,0,IF(D25=2,0.95,IF(D25=3, 0.98,"")))

Upvotes: 2

Related Questions