问题
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
回答1:
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)
回答2:
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
rngvariable, some starting point for the offsets.
来源:https://stackoverflow.com/questions/28564505/vba-excel-compile-error-object-required