简体   繁体   English

VBA Excel“编译错误:需要对象”

[英]VBA Excel "Compile error: Object Required"

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 .当我运行代码时,我得到一个编译错误来调试,它读取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

You are trying to use the set keyword with a string variable.您正在尝试将 set 关键字与字符串变量一起使用。 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.好吧,您声明了一个rng变量,但没有为其分配任何值。 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.rng变量赋值,这是偏移量的一些起点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM