简体   繁体   中英

runtime error 91 excel vba

I'm using the following code:

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

I keep getting:

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

I don't understand why.

Try With Worksheets("vendorOutput.csv").Range(Worksheets("vendorOutput.csv").Cells(1, zipColumnVendor), Worksheets("vendorOutput.csv").Cells(vendorRows, zipColumnVendor))

Because if vendoroutput.csv is not the active sheet, then cells(x,x) points to the cells on the active sheet, even though the Range is Worksheets("vendorOutput.csv"), which obviously won't work very well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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