IFERROR在这个宏中?

时间:2015-12-03 14:38:51

标签: excel-vba vba excel

问题是,当我更改I16或I17中的值时,我收到错误。怎么样 我可以防止这种错误发生吗? 我在I16和I17中查看了工作表名称,因为每周都会有更新的工作表。 谢谢

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

3 个答案:

答案 0 :(得分: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

答案 1 :(得分:2)

您可以使用与此类似的内容来调用compareSheets。如果两个范围中的任何一个与工作表名称不对应,它将发出警告,如果为真,则不会调用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

End Sub

答案 2 :(得分: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