【问题标题】:runtime error 91 excel vba运行时错误 91 excel vba
【发布时间】:2013-12-28 18:45:03
【问题描述】:

我正在使用以下代码:

Sub propZips()

' property variables
Dim wsProperty As Worksheet:              Set wsProperty = Worksheets("propertyOutput.csv")
Dim zipColumnProperty As Integer:       zipColumnProperty = 6
Dim propertyRows As Integer:               propertyRows = wsProperty.Cells(Rows.Count, 1).End(xlUp).Row - 1  ' less 1 for label row
Dim singlePropZip As String

' vendor variables
Dim wsVendor As Worksheet:                Set wsVendor = Worksheets("vendorOutput.csv")
Dim zipColumnVendor As Integer:         zipColumnVendor = 5
Dim vendorRows As Integer:                   vendorRows = wsVendor.Cells(Rows.Count, 1).End(xlUp).Row - 1 ' less 1 for label row
Dim venNameOffset As Integer:               venNameOffset = -3

' counter variables
Dim propCounter As Integer ' for loop through property zips
Dim venCounter As Integer '  for loop through vendors

Dim serviceArea As Range ' for holding cell address of vendor service area match
Dim firstAddress As String  ' also for helping match vendor service areas

Dim venName As Range ' hold vendors name
Dim singlePropAddress As String 'hold cell address of property zip code in question
Dim n As Integer ' count cells out to the right to print vendor names and categories on property page


For propCounter = 1 To propertyRows  ' loop through properties
    singlePropZip = wsProperty.Cells(propCounter + 1, zipColumnProperty) ' propety zip in question

    With Worksheets("vendorOutput.csv").Range(Cells(1, zipColumnVendor), Cells(vendorRows, zipColumnVendor))
            Set serviceArea = .Find(what:=singlePropZip, LookIn:=xlValues, LookAt:=xlPart)
            Debug.Print serviceArea.Address                   
    End With
Next propCounter ' end loop through properties
End Sub

我不断得到:

Runtime error '91'
object variable or with block variable not set

我不明白为什么。

【问题讨论】:

  • 检查第 28 行 - singlePropZip = wsProperty.Cells(propCounter + 1, zipColumnProperty)
  • 好的。我看着它,它似乎得到了正确的值。我错过了什么吗?
  • 嗯..我怀疑这可能设置不正确或更糟的是可能是空白的......因此可能会引发错误。循环是否适用于某些值?或者您从一开始就收到此错误。
  • 哪一行导致错误?
  • 当我点击调试时,它以黄色突出显示With Worksheets("vendorOutput.csv").Range(Cells(1, zipColumnVendor), Cells(vendorRows, zipColumnVendor))

标签: excel runtime-error vba


【解决方案1】:

尝试使用 Worksheets("vendorOutput.csv").Range(Worksheets("vendorOutput.csv").Cells(1, zipColumnVendor), Worksheets("vendorOutput.csv").Cells(vendorRows, zipColumnVendor))

因为如果 vendoroutput.csv 不是活动工作表,则 cells(x,x) 指向活动工作表上的单元格,即使 Range 是 Worksheets("vendorOutput.csv"),这显然不起作用很好。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多