代码似乎永远运行并且错误:未设置块变量(VBA)

时间:2018-06-29 03:19:14

标签: excel vba forever

我对VBA完全陌生,所以请多多包涵。

我正在尝试编写一个子过程,该子过程将遍历某一列中的每一行,并与另一张纸的标准进行比较。例如,如果它包含“ x”,则将返回该值。但是,当我尝试运行代码时,代码将永远运行并导致计算机挂起。

这是我到目前为止编写的代码。它不断提示错误:未设置对象变量和块变量。 PS:使用“ Application.WorksheetFunction.Index”和读取其他线程时出现错误,建议删除“ WorksheetFunction”。我不确定是否会导致此问题,并且我也想澄清删除单词'WorksheetFunction'

的基本原理。

非常感谢您!

Sub sub_inputData()
Dim ws As Worksheet: Set ws = ActiveSheet
Dim lastrow as range 
lastrow = ws.Cells (ws.Rows.Count, 17).End (xlUp).row 

Dim rng As Range
Set rng = ws.Range("Q4:Q" & lastrow)
Dim rngCell As Range

On Error Resume Next

For Each rngCell In rng
    If rngCell.Offset(0, -13) = "x" Then
       rngCell = Application.Index(Sheets("Data").Range _
       ("D805:D813"), Application.Match(rngCell.Offset(0, -15), Sheets("Data").Range _
       ("D805:D813"), 1))
    ElseIf rngCell.Offset(0, -13) = "y" Then
       rngCell = Application.Index(Sheets("Data").Range _
       ("D27:D34"), Application.Match(rngCell.Offset(0, -15), Sheets("Data").Range _
       ("D27:D34"), 1))
    ElseIf rngCell.Offset(0, -13) = "z" Then
       rngCell = Application.Index(Sheets("Data").Range _
       ("D718:D726"), Application.Match(rngCell.Offset(0, -15), Sheets("Data").Range _
       ("D718:D726"), 1))
    Else: rngCell = vbNullString
    End If

Next rngCell

     Call sub_code2
     Call sub_code3
     Set rngCell = Nothing
     Set rng = Nothing
End Sub 

3 个答案:

答案 0 :(得分:1)

与您在此处已修改的代码相关的问题。
1)Dim lastrow As Long,而不是Range
2)不需要Else:,只需使用Else
3)不需要Set rngCell = NothingSet rng = Nothing。请参见this链接以获取说明
4)由于您仅检查1个单元格的值,因此可以使用Select Case来获得一个较为简洁的代码。
5)On Error Resume Next不利于调试代码。您想要查看错误,以便可以处理它们。我建议查找该代码的要做不要

Sub sub_inputData()
Dim ws As Worksheet: Set ws = ActiveSheet
Dim lastrow As Long: lastrow = ws.Range("Q" & ws.Rows.Count).End(xlUp).Row
Dim rng As Range: Set rng = ws.Range("Q4:Q" & lastrow)
Dim rngCell As Range

Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual

For Each rngCell In rng
    Select Case rngCell.Offset(0, -13)
        Case "x"
            rngCell = Application.Index(Sheets("Data").Range _
            ("D805:D813"), Application.Match(rngCell.Offset(0, -15), Sheets("Data").Range _
            ("D805:D813"), 1))
        Case "y"
            rngCell = Application.Index(Sheets("Data").Range _
            ("D27:D34"), Application.Match(rngCell.Offset(0, -15), Sheets("Data").Range _
            ("D27:D34"), 1))
        Case "z"
            rngCell = Application.Index(Sheets("Data").Range _
            ("D718:D726"), Application.Match(rngCell.Offset(0, -15), Sheets("Data").Range _
            ("D718:D726"), 1))
        Case Else
            rngCell = ""
    End Select
Next rngCell

Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic

     Call sub_code2
     Call sub_code3
End Sub

答案 1 :(得分:0)

另一种可能性是使用Switch()函数:

Sub sub_inputData()
    Dim rngCell As Range, rangeToSearch As Range
    Dim val As Variant

    With ActiveSheet ' reference data sheet (better: With Worksheets("MyDataSheetName"))
        For Each rngCell In .Range("Q4", .Cells(.Rows.Count, "Q").End(xlUp)) ' loop throughreferenced sheet column Q cells from row 4 down to last not empty one
            val = rngCell.Offset(, -13).Value2 ' store column D current cell row value
            Set rangeToSearch = Sheets("Data").Range(Switch(val = "x", "D805:D813", val = "y", "D27:D34", val = "z", "D718:D726", True, "A1")) ' set range to search into with respect to stored value. set it to "A1" to signal no search is needed
            If rangeToSearch.Address <> "$A$1" Then ' if search is needed 
                rngCell.Value = Application.Index(rangeToSearch, Application.Match(rngCell.Offset(, -15).Value2, rangeToSearch, 1)) 'do the lookup
            Else
                rngCell.ClearContents ' clear current cell
            End If
        Next
    End With

    sub_code2 ' no need for 'Call' keyword
    sub_code3 ' no need for 'Call' keyword
End Sub

答案 2 :(得分:0)

您似乎正在根据D列中的值有效地选择一个查找范围,然后根据B列中的值对该范围进行查找。

如果是这样,您可以完全使用公式来执行此操作,这样会更加高效,因为它仅在需要时(即仅在其输入更改时)才在特定的单元格上运行。

这是一个使用表和表符号的示例。表格非常适合此操作,因为您无需修改​​公式即可处理新数据。

C2中的公式为=VLOOKUP([@ID],CHOOSE(VLOOKUP([@Condition],Conditions,2,FALSE),X,Y,Z),2,FALSE)

该公式使用E1:F3中的“条件”表来计算要在哪个其他表上进行查找。我已经将其他表命名为X,Y和Z。

enter image description here