麻烦传递Cell对象? (我可能错了)

时间:2015-08-16 20:44:06

标签: excel-vba vba excel

首先非常感谢你。在过去的几个月里(我相信)我的编码已经取得了巨大的进步。任何和所有批评都是受欢迎的(撕裂我)。

最近我开始尝试使用不同的Subs(我不太明白何时使用函数等,但我认为这是一个很好的结构实践,当我想出来。

我在Sub ownerCHECK

中使用以下位代码命中运行时424错误
Sub OccupationNORMALIZATION()

    Dim infoBX As String

    ' initialize variables
    LRow = ActiveSheet.UsedRange.Rows.Count
    LCol = ActiveSheet.UsedRange.Columns.Count
    STATUScounter = LRow

        Do While infoBX = ""
            infoBX = InputBox("Enter Occupation Column", "Occupation Column")
        Loop

              restaurCHECK (infoBX)

Application.ScreenUpdating = True
Application.StatusBar = ""
End Sub

-

Sub restaurCHECK(infoBX As String)
    Dim RestaurantS(), RestaurantDQs() As Variant
    Dim i, LRow, LCol, STATUScounter As Long
    Dim rRng As Range

        LRow = ActiveSheet.UsedRange.Rows.Count
        LCol = ActiveSheet.UsedRange.Columns.Count
        STATUScounter = LRow
RestaurantS = Array("estaur", "food", "cafe", "beverage", "waiter", "waitr", _
                "waitstaff", "wait staff", "grill") 'array list of target occupations


RestaurantDQs = Array("fast", "pub", "import", "packing", "processing", "packag", _
                "retired", "anufact", "distrib") ' disqualifying words for Restaurante category

Set rRng = Range(infoBX & "2:" & infoBX & LRow)

Application.ScreenUpdating = False
    For Each cell In rRng
        ownerCHECK (cell)
        For i = LBound(RestaurantS) To UBound(RestaurantS)
                If InStrRev(cell.Value, UCase(RestaurantS(i))) > 0 Then
                    cell.Offset(, 1) = "Restaurants"
                    cell.Interior.Color = 52479
                End If
            Debug.Print cell.Value
        Next

        For i = LBound(RestaurantDQs) To UBound(RestaurantDQs)
            If InStrRev(cell.Value, UCase(RestaurantDQs(i))) And cell.Interior.Color = 52479 Then
                cell.Interior.Color = 255
                cell.Offset(, 1) = ""


            End If

        Next

                    STATUScounter = STATUScounter - 1
                    Application.StatusBar = "REMAINING ROWS " & STATUScounter & "           tristram "

    Next cell


End Sub

-

Sub ownerCHECK(str_owner As Range)
   Dim owner() As Variant
   owner() = Array("owner", "shareholder", "owns ")
        For i = LBound(owner) To UBound(owner)
                If InStrRev(str_owner, UCase(owner(i))) > 0 Then
                    cell.Offset(, 2) = "Owner"

                End If
        Next

End Sub

1 个答案:

答案 0 :(得分:0)

我可以在ownerCHECK()中看到几个问题:

  • “cell”未定义(除非它是全局的)
  • 您不应将“cell”用作变量名称(内部VBA属性)
  • 检查传入范围的有效性

Option Explicit

Sub ownerCHECK(ByRef rngOwner As Range)

    If Not rngOwner Is Nothing Then

        Dim owner() As Variant
        owner() = Array("OWNER", "SHAREHOLDER", "OWNS ")

        For i = LBound(owner) To UBound(owner)
            If InStrRev(UCase(rngOwner), owner(i)) > 0 Then
                rngOwner.Offset(, 2) = "Owner"
            End If
        Next

    End If

End Sub