Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Object Required Error in excel VBA [duplicate]

Tags:

excel

vba

Dim g1val, g2val As Integer

Set g1val = 0
Set g2val = 0

For i = 3 To 18
    If g1val > Cells(33, i).Value Then
        g1val = g1val
    Else
        g1val = Cells(33, i).Value
    End If
Next i

For j = 32 To 57
    If g2val > Cells(31, j).Value Then
        g2val = g2val
    Else
        g2val = Cells(31, j).Value
    End If
Next j

Here on second line i am getting an error saying object required.I have tried to make g1val and g2val as "Double" and tried to give 1 for their values at first.But those didn't work out. Can u help??....

like image 605
srt Avatar asked Sep 27 '13 10:09

srt


People also ask

What is object required error in Excel VBA?

In rare cases, this error occurs when you have a valid object but are attempting to perform an invalid action on the object. For example, you may receive this error if you try to assign a value to a read-only property. Check the object's documentation and make sure the action you are trying to perform is valid.

What are the 3 different types of error handling techniques in VBA?

AutoCAD to Excel - VBA Programming Hands-On! There are three types of errors in programming: (a) Syntax Errors, (b) Runtime Errors, and (c) Logical Errors.

How do I fix type mismatch error in VBA?

Step 1: Write the subprocedure for VBA Type Mismatch. Step 2: Again assign a new variable, let's say “A” as Byte data type. Let's understand the Byte Data type here. Byte can only store the numerical value from 0 to 255.


1 Answers

In order to set the value of integer variable we simply assign the value to it. eg g1val = 0 where as set keyword is used to assign value to object.

Sub test()

Dim g1val, g2val As Integer

  g1val = 0
  g2val = 0

    For i = 3 To 18

     If g1val > Cells(33, i).Value Then
        g1val = g1val
    Else
       g1val = Cells(33, i).Value
     End If

    Next i

    For j = 32 To 57
        If g2val > Cells(31, j).Value Then
           g2val = g2val
        Else
          g2val = Cells(31, j).Value
        End If
    Next j

End Sub
like image 195
Santosh Avatar answered Oct 20 '22 14:10

Santosh