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
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)
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.
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.
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)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With