Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VBA Excel "Compile error: Object Required"

Tags:

excel

vba

I am working on VBA code in excel and i have the following piece of code

Set strModel = Right(rng.Offset(0, 13).Value, Len(rng.Offset(0, 13).Value) - 4)

When I run the code I get a compile error to debug and it reads Object Required. What is it asking me to do?

This is a larger piece of the code:

strHSLtemp = "C:\Users\Desktop\To Do\MidDay Orders Macro Tool\Temp Files\HSL Orders Temp.xlsx"
wbHSLtemp = Dir(strHSLtemp)
Set wbHSLtemp = Workbooks.Open(strHSLtemp)
Set wsHSLtemp = wbHSLtemp.Sheets(1)
Dim arrModels() As String, strModel As String, blMultipleModels As Boolean, rngModel As range, lngModels As Long
Dim rng As range
Set strModel = Right(rng.Offset(0, 13).Value, Len(rng.Offset(0, 13).Value) - 4) 'strip off leading "HSL-"
strModel = Replace(strModel, " / ", "/") 'get rid of the spaces that appear to surround the forward slash
    If InStr(1, strModel, "/") > 0 Then 'yep, there are multiples
        blMultipleModels = True
    Else
        blMultipleModels = False
    End If
    If blMultipleModels = False Then 'just assign the model and move on in the outer loop
        wsHSLtemp.Cells(lastrowOutput, 12) = strModel
like image 891
Sandra Barocio Avatar asked Feb 17 '15 15:02

Sandra Barocio


People also ask

How do I fix compile errors in Excel VBA?

I have resolved same error by following these 4 steps : Open Excel file which is having issue, press Alt + F11 go into its Visual Basic Editor. From the Tools menu select References ( Note, if references option is disabled in tools menu try closing and reopening file and enable Macro before proceeding next steps)

What is object required in VBA?

Object Required in Excel VBA. Object required is an error which is caused at run time when we have defined any variable which is not an object but we try to assign some values using a SET statement. This error is a run time error that arises for various reasons.

Why am I getting a compile error in VBA?

A compilation error can also occur when VBA doesn't find anything missing while typing the code, but it does when the code is compiled or executed. VBA checks each line as you're typing the code and highlights the syntax error as soon as the line is incorrect and you hit enter.


2 Answers

You are trying to use the set keyword with a string variable. Set is only needed with Objects. Remove the set, and you should be fine:)

Specifically, Change this:

Set strModel = Right(rng.Offset(0, 13).Value, Len(rng.Offset(0, 13).Value) - 4)

To This:

strModel = Right(rng.Offset(0, 13).Value, Len(rng.Offset(0, 13).Value) - 4)
like image 137
Pillgram Avatar answered Oct 03 '22 11:10

Pillgram


Well, you declared an rng variable, but you did not assign any value to it. So:

  • Replace

    Set strModel = Right(rng.Offset(0, 13).Value, Len(rng.Offset(0, 13).Value) - 4)

    with

    strModel = Right(rng.Offset(0, 13).Value, Len(rng.Offset(0, 13).Value) - 4) 'strip off leading "HSL-"

  • Assign a value to rng variable, some starting point for the offsets.

like image 42
Wiktor Stribiżew Avatar answered Oct 03 '22 12:10

Wiktor Stribiżew