Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 我的错误处理程序只工作一次,我不明白为什么_Excel_Vba_Resume - Fatal编程技术网

Excel 我的错误处理程序只工作一次,我不明白为什么

Excel 我的错误处理程序只工作一次,我不明白为什么,excel,vba,resume,Excel,Vba,Resume,我正试图借助命名范围翻译一些Excel文件。我需要知道哪些文本尚未定义,然后将其添加到名称数据库中。为此,我使用VLOOKUP将单元格的实际值与数据库中的值进行比较。我也不需要翻译数字、公式或任何不是字符串的东西 我发现将未定义的名称添加到数据库的唯一方法是在VLOOKUP遇到错误时使用错误处理程序。问题是这个错误处理程序只工作一次,我不明白为什么。我读过你必须用简历来做这件事,我也用过,但它不起作用 提前感谢您的帮助,并对语法错误表示歉意 这是我的密码 Sub translation() Di

我正试图借助命名范围翻译一些Excel文件。我需要知道哪些文本尚未定义,然后将其添加到名称数据库中。为此,我使用VLOOKUP将单元格的实际值与数据库中的值进行比较。我也不需要翻译数字、公式或任何不是字符串的东西

我发现将未定义的名称添加到数据库的唯一方法是在VLOOKUP遇到错误时使用错误处理程序。问题是这个错误处理程序只工作一次,我不明白为什么。我读过你必须用简历来做这件事,我也用过,但它不起作用

提前感谢您的帮助,并对语法错误表示歉意

这是我的密码

Sub translation()
Dim cell, cell1, cell2 As Range, search_value As String, i As Integer
i = 3
For Each cell In Hoja65.Range("D3:D10") 'Range to look up for words.
        If IsEmpty(cell) = False And IsError(cell) = False And IsNumeric(cell) = False Then ' Enter code if the cell is not empty and is not an error
                ' Check if read value is equal to any exception listed in Hoja65.Range("B3:E20")
                For Each cell1 In Hoja65.Range("E3:E10")
                        ' Check if read value is a formula beginning with "=", "+" or "-" or contains an hyperlink.
                        If cell1 = cell Or InStr(1, cell.FormulaLocal, "=", vbTextCompare) = 1 Or InStr(1, cell.FormulaLocal, "+", vbTextCompare) = 1 Or InStr(1, cell.FormulaLocal, "-", vbTextCompare) = 1 Or InStr(1, cell.Value, "http", vbTextCompare) <> 0 Then
                                GoTo continue
                        End If
                Next cell1
                ' If read value is not equal to exception
                On Error GoTo handler
                search_value = Application.WorksheetFunction.VLookup(cell.Value, Hoja64.Range("F9:G550"), 2, False) ' Range where the names are defined.
                cell.Value = "=" & search_value 'Change read to defined name.
        End If
continue:
Next cell
MsgBox "Execution finished"
Exit Sub ' VERY IMPORTANT: This line must ALWAYS precede any error handler in order to avoid an infinite loop.
handler: ' Executes only if VLOOKUP doesn't find a match
        With Hoja65
                For Each cell2 In .Range(.Cells(3, 3), .Cells(i, 3))
                If cell2 = cell Then
                        GoTo continue
                Else
                        .Cells(i, 2) = cell.Address ' Save address of unsaved name.
                        .Cells(i, 3) = cell.Value ' Save value of unsaved name.
                End If
                i = i + 1
                Next cell2
        End With
        Resume continue
'ActiveSheet.Shapes.Range(Array("Rounded Rectangle 2")).Select
'Selection.Formula = "=title270"
End Sub
这就是为什么。您不需要跳入/跳出错误处理子例程;GoTo不会重置错误状态,因此您正在进入错误编号为0的愉快路径,即您仍然处于错误状态

用Resume替换GoTo,你应该回到正轨

            If cell2 = cell Then
                    Resume continue

为什么在For中出现错误转到处理程序。。。下一个您不需要将其设置为任何其他值。因为只有在VLOOKUP行中出现错误时,我才需要代码来输入处理程序。然后,您应该在单元格后设置错误GoTo 0。Value==&search_value@Jeeped我不明白。我是否应该删除On Error GoTo处理程序并在单元格后写入On Error GoTo 0。Value==$search\u Value?这难道不意味着,如果一个错误出现在On错误之前的行中,并没有进入处理程序的标题吗?实际上,我根本无法理解您的代码。另外,请注意,最好使用@BruceWayne绝对。。。除此之外,VBA中没有Continue语句,所以OP在这里跳过循环迭代-不是在错误处理程序中,我的意思是,对于GoTo=我可能还应该提到,跳入/跳出With块通常是一个非常非常糟糕的主意。如果该条件为真,我需要在下一个单元格中重复for。如果没有GoTo,我怎么能这样做?@LuisMiguel Resume continue将执行与GoTo continue完全相同的操作,只是您将重置错误状态。或者,您可以使用If…End If块包装循环体,并通过添加1个缩进级别来避免完全转到。
            If cell2 = cell Then
                    Resume continue