简体   繁体   English

使用多张纸运行时错误 91

[英]Using Multiple sheets run time error 91

I am very new to VBA but have been working on my knowledge to help my team increase their efficiency by creating simple macros in excel.我对 VBA 非常陌生,但一直在努力利用我的知识,通过在 excel 中创建简单的宏来帮助我的团队提高效率。 I am having some issues with this code and hope that you could help me figure it out.我对这段代码有一些问题,希望你能帮我弄清楚。

I am trying to grab particular information from different worksheets and collect it all into the same sheet.我试图从不同的工作表中获取特定信息并将其全部收集到同一张表中。 The macro needs to be quite robust as other users edit the sheet and make calculations.当其他用户编辑工作表并进行计算时,宏需要非常强大。

Also open to any other suggestions for improving my code too.也欢迎任何其他改进我的代码的建议。

Thank you for your help in advanced!感谢您在高级方面的帮助!

'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

So it turned out the error was searching for "Item Discription" instead of "Item Description".所以原来错误是搜索“项目描述”而不是“项目描述”。 Then the .find call returned Nothing, and doing a .Offset on Nothing gave the runtime error.然后 .find 调用返回 Nothing,并且在 Nothing 上执行 .Offset 会导致运行时错误。 Fixing the spelling resolved it.修复拼写解决了它。 To get greater clarity and avoid the run time error, consider doing something like:为了更加清晰并避免运行时错误,请考虑执行以下操作:

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

but probably make a sub() of it to avoid repeating yourself.但可能会对其进行 sub() 以避免重复。

Also, it is a style thing for sure, but I'm not convinced j or strfind are doing much for you as opposed to popping in the values directly.此外,这肯定是一种风格,但我不相信 j 或 strfind 对您有多大帮助,而不是直接弹出值。

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

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