VBA Excel“编译错误:需要对象”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28564505/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
VBA Excel "Compile error: Object Required"
提问by Sandra Barocio
I am working on VBA code in excel and i have the following piece of code
我正在 excel 中处理 VBA 代码,我有以下代码
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?
当我运行代码时,我得到一个编译错误来调试,它读取Object Required. 它要我做什么?
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
回答by Pillgram
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:)
您正在尝试将 set 关键字与字符串变量一起使用。只有对象需要设置。删除集合,你应该没问题:)
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)
回答by Wiktor Stribi?ew
Well, you declared an rngvariable, but you did not assign any value to it. So:
好吧,您声明了一个rng变量,但没有为其分配任何值。所以:
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.
代替
Set strModel = Right(rng.Offset(0, 13).Value, Len(rng.Offset(0, 13).Value) - 4)和
strModel = Right(rng.Offset(0, 13).Value, Len(rng.Offset(0, 13).Value) - 4) 'strip off leading "HSL-"为
rng变量分配一个值,即偏移量的某个起点。

