使用多张纸运行时错误91

时间:2018-07-19 00:58:57

标签: excel excel-vba runtime-error

我是VBA的新手,但是我一直在努力学习如何通过在excel中创建简单的宏来帮助我的团队提高效率。我在使用此代码时遇到了一些问题,希望您能帮助我解决这个问题。

我正在尝试从不同的工作表中获取特定信息,并将所有信息收集到同一工作表中。该宏必须非常健壮,因为其他用户可以编辑工作表并进行计算。

也欢迎任何其他建议来改进我的代码。

感谢您对高级的帮助!

'For updating spreadsheet to QUOTE

Sub main()

Application.ScreenUpdating = False

'Decalring worksheets
Dim wsO As Worksheet ' Ostendo
Set wsO = Sheets("Ostendo Import")

Dim wsP As Worksheet ' project costing
Set wsP = Sheets("Project Costing")

Dim wsH As Worksheet ' ostendo help
Set wsH = Sheets("Ostendo Help")

'finding the end of the row
Dim lastRow As Long
lastRow = wsO.UsedRange.Rows.Count


'Find the position of all values
Dim i As Integer

Dim Vfind(9) As String
Dim rngFound(9) As Range
Dim rngStart(9) As Range

Vfind(0) = "ITEMCODE"
Vfind(1) = "ITEMDESCRIPTION"
Vfind(2) = "SOURCEDBY"
Vfind(3) = "STDSELLPRICE"
Vfind(4) = "STDBUYPRICE"
Vfind(5) = "AVERAGECOST"
Vfind(6) = "PRIMARYSUPPLIER"
Vfind(7) = "JOBNOTES"
Vfind(8) = "ADDITIONALFIELD_1"
Vfind(9) = "ADDITIONALFIELD_3"

For i = 0 To 9

    Set rngStart(i) = wsO.Rows(2).Find(Vfind(i))
    Set rngFound(i) = rngStart(i).Offset(1, 0)

Next i

 'updating each ostendo field and updating it for QUOTING

 Dim j As Integer
 Dim strFind As String

'Inputing item code [0]
j = 0
strFind = "Item Code"
rngFound(j).Value = wsP.Rows(8).Find(strFind).Offset(1, 0)

'Updating Item Discription [1]
j = 1
strFind = "Item Discription"
rngFound(j).Value = wsH.Rows(4).Find(strFind).Offset(1, 0) 'ISSUE HERE RUN TIME ERROR 91  PLEASE HELP

'updating sourced by [2]
j = 2
rngFound(j).Value = "Assembly"

'updating standard sell price [3]
j = 3
strFind = "Adjusted Price"
rngFound(j).Value = wsP.Rows(8).Find(strFind).Offset(1, 0)

'updating COST for items without an assmebly (RAW FEATURES) [4]
j = 4
rngFound(j).ClearContents

'updating COST for items with an assembly (QUOTING COST) [5]
j = 5
strFind = "Total Light Cost"
rngFound(j).Value = wsP.Rows(8).Find(strFind).Offset(1, 0)

'updating Supplier [6]
'j = 6
'rngFind(j).ClearContents

'Updating Job Notes [7] Code to be Updated at a later date
j = 7
rngFound(j).Value = "Overall Dimensions: " & wsH.Rows(4).Find("Overall Dimentions: [H:W:D] or [DIA:D]").Offset(1, 0) & Chr(10) & "Finish: " & wsH.Rows(4).Find("Finish:").Offset(1, 0) & Chr(10) & "Light: " & wsH.Rows(4).Find("Light:").Offset(1, 0) & Chr(10) & "Driver: " & wsH.Rows(4).Find("Driver:").Offset(1, 0) & Chr(10) & "Dimming: " & wsH.Rows(4).Find("Dimming:").Offset(1, 0) & Chr(10) & "Features: " & wsH.Rows(4).Find("Features:").Offset(1, 0)


'updating supplier [8]
j = 8
strFind = "Supplier Code"
rngFound(j).ClearContents


'updating supplier code
j = 9
'strFind = "Supplier"
rngFound(j).ClearContents


'autofill loop
Dim k As Integer

For k = 0 To 8
rngFound(k).AutoFill Destination:=wsO.Range(rngFound(k), Cells(lastRow, rngFound(k).Column)), Type:=xlFillValues

Next k


Application.ScreenUpdating = True


End Sub

1 个答案:

答案 0 :(得分:0)

因此,事实证明该错误是在搜索“商品说明”而不是“商品说明”。然后,.find调用返回Nothing,对Nothing执行.Offset则给出运行时错误。修正拼写即可解决。 为了获得更高的清晰度并避免运行时错误,请考虑执行以下操作:

DIM c as range
c=wsH.Rows(4).Find(strFind)
if c Is Nothing Then
'give some error message about strFind not found
Else
    rngFound(j).Value=c.Offset(1, 0)
End If

但可能要对其做一个sub()以避免重复您自己。

当然,这是一种样式,但是我不相信j或strfind会为您做很多事情,而不是直接弹出值。