2015-12-03 66 views
0

问题是,当我更改I16或I17中的值时,出现错误。 如何防止发生此错误? 我在I16和I17中检查图纸名称,因为每周都会有更新的图纸。 谢谢IFERROR in this macro?

Sub Compare() 


Call compareSheets(range("I16").Value, range("I17").Value) 


End Sub 




Sub compareSheets(Sofon As String, Sofon2 As String) 


Dim mycell As range 
Dim mydiffs As Integer 


For Each mycell In ActiveWorkbook.Worksheets(Sofon2).range("M:M") 
If Not mycell.Value = ActiveWorkbook.Worksheets(Sofon).Cells(mycell.Row, mycell.Column).Value Then 


mycell.Interior.Color = vbYellow 
mydiffs = mydiffs + 1 


End If 
Next 


MsgBox mydiffs & " differences found in Column M (Salesman)", vbInformation 


ActiveWorkbook.Sheets(Sofon2).Select 


End Sub 
+1

*我得到一个错误* - 你会得到什么错误?另外,在“I16”和“I17”中更改它们之后,您确定工作簿中存在工作表吗?你的代码也可以更好地用于**,而不是循环遍历M列中的每一个单元格,而只是用数据循环遍历单元格(通过查找包含数据的最后一行并将其设置到您的末尾范围)。 –

+0

我得到:运行时错误'9'下标超出范围。当我将I16或I17更改为与其中一张纸张的名称不相关的值时,会发生这种情况。但我希望它是万无一失的,这就是为什么我想知道如何添加一个IFERROR。如果你有更好的主意,请告诉我。 –

+1

查看“On Error GoTo”。 –

回答

2

您可以使用类似这样的调用compareSheets东西。它会警告你,如果两个范围中的任何一个不对应于图纸名称,并且如果为true,则不会调用compareSheets。

Dim Sheet1 As Worksheet 
Dim boolI16SheetCheck As Boolean 
Dim boolI17SheetCheck As Boolean 

    boolI16SheetCheck = False 
    boolI17SheetCheck = False 

    For Each Sheet1 in ActiveWorkbook.Worksheets 
     If Sheet1.Name = Activesheet.Range("I16").Value Then boolI16SheetCheck = True 
     If Sheet1.Name = Activesheet.Range("I17").Value Then boolI17SheetCheck = True 
     If boolI16SheetCheck = True And boolI17SheetCheck = True Then 
      Call compareSheets(range("I16").Value, range("I17").Value) 
      Exit Sub 
     End If 
    Next Sheet1 

    If boolI16SheetCheck = False Then 
     If boolI17SheetCheck = False Then 
      Msgbox "Neither I16 nor I17 sheet found." 
     Else 
      Msgbox "I16 sheet not found." 
     End If 
    Else 
     Msgbox "I17 sheet not found." 
    End If 

末次

+1

@scottcraner建议'On Error GoTo'也可以工作,并且会稍微短一些。我的个人偏好是在错误发生之前防止错误,尤其是因为错误可能会导致重置的麻烦。 – puzzlepiece87

+0

看到@ScottHoltzman的回答后,我决定检查两张纸是否已经在每个纸张循环后被发现,而不是每张纸。感谢斯科特! – puzzlepiece87

3

为了表明我在想什么。

我同意puzzlepiece87 On Error是挑剔的,但有了这个简单的东西,我会用它来避免多余的循环。

Sub compareSheets(Sofon As String, Sofon2 As String) 

Dim mycell As Range 
Dim mydiffs As Integer 

On Error GoTo nosheet 
For Each mycell In ActiveWorkbook.Worksheets(Sofon2).Range("M:M") 
    If Not mycell.Value = ActiveWorkbook.Worksheets(Sofon).Cells(mycell.Row, mycell.Column).Value Then 
     mycell.Interior.Color = vbYellow 
     mydiffs = mydiffs + 1 
    End If 
Next 


MsgBox mydiffs & " differences found in Column M (Salesman)", vbInformation 
ActiveWorkbook.Sheets(Sofon2).Select 
Exit Sub 

nosheet: 
If Err.Number = 9 Then 
    MsgBox "One or both sheets do not exist" 
Else 
    MsgBox Err.Description 
End If 

End Sub 
2

由于OP想要一个ISERROR类型的解决方案,我决定后结合有功能,以检查是否片材在工作簿中存在的代码。这个概念类似于已发布的答案,但它严格地将任何On Error声明保留在函数中,并使用常规代码块来评估错误。

Sub Compare() 

Dim bGo As Boolean 
Dim s1 As String, s2 As String 
s1 = Range("I16").Value2 
s2 = Range("I17").Value2 

If Not WorksheetExist(s1) Then 
    bGo = False 
    MsgBox "The sheet " & s1 & " does not exist in this workbook." 
End If 

If Not WorksheetExist(s2) Then 
    bGo = False 
    MsgBox "The sheet " & s2 & " does not exist in this workbook." 
End If 

If bGo Then compareSheets s1, s2 

End Sub 


Function WorksheetExist(sName As String, Optional wb As Workbook) As Boolean 

    Dim wbCheck As Workbook, ws As Worksheet 
    If wb Is Nothing Then Set wbCheck = ThisWorkbook Else: Set wbCheck = wb 

    On Error Resume Next 
    Set ws = wbCheck.Sheets(sName) 
    On Error GoTo 0 

    If Not ws Is Nothing Then WorksheetExist = True Else: WorksheetExist = False 

End Function 

,并根据@ puzzlepiece87方法,这里是一个改善WorksheetExist Function是完全的On Error声明消除。

Function WorksheetExist(sName As String, Optional wb As Workbook) As Boolean 

Dim wbCheck As Workbook, ws As Worksheet 
If wb Is Nothing Then Set wbCheck = ThisWorkbook Else: Set wbCheck = wb 

WorksheetExist = False 

For Each ws In wbCheck.Worksheets 
    If ws.Name = sName Then 
     WorksheetExist = True 
     Exit For 
    End If 
Next 

End Function