VBA Excel“编译错误:需要对象" [英] VBA Excel "Compile error: Object Required"

查看:145
本文介绍了VBA Excel“编译错误:需要对象"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用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)

运行代码时,出现编译错误以进行调试,并且读取为Object Required.问我该怎么办?

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

这是更大的代码段:

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

推荐答案

您正尝试将set关键字与字符串变量一起使用.只有对象才需要设置.删除集合,就可以了:)

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 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)

这篇关于VBA Excel“编译错误:需要对象"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆