Error-Handling Techniques
Sub entersquareRoot1()
Dim num As Double
num = InputBox("Enter a value")
ActiveCell.Value = Sqr(num)
End Sub
Sub entersquareRoot2()
Dim num As Double
num = InputBox("Enter a value")
If num < 0 Then
MsgBox "you must enter a positive number"
Exit Sub
End If
ActiveCell.Value = Sqr(num)
End Sub
Sub entersquareRoot3()
Dim num As Variant
num = InputBox("Enter a value")
If Not IsNumeric(num) Then
MsgBox "You must enter a number."
Exit Sub
End If
If num < 0 Then
MsgBox "you must enter a positive number"
Exit Sub
End If
ActiveCell.Value = Sqr(num)
End Sub
Sub entersquareRoot4()
Dim num As Variant
If TypeName(Selection) <> "Range" Then
MsgBox "select a range frist."
Exit Sub
End If
num = InputBox("Enter a value")
If Not IsNumeric(num) Then
MsgBox "You must enter a number."
Exit Sub
End If
If num < 0 Then
MsgBox "you must enter a positive number"
Exit Sub
End If
ActiveCell.Value = Sqr(num)
End Sub
Sub entersquareRoot5()
Dim num As Variant
Dim msg As String
On Error GoTo BadEntry
num = InputBox("Enter a value")
If num = "" Then Exit Sub
ActiveCell.Value = Sqr(num)
Exit Sub
BadEntry:
msg = "An error Occurred." & vbNewLine
msg = msg & "Make sure a range is selected"
msg = msg & "and you enter a nonnegative value."
MsgBox msg
End Sub
Sub entersquareroot6()
Dim num As Variant
Dim msg As String
Dim ans As Integer
TryAgain:
On Error GoTo Badentry
num = InputBox("Enter a value")
If num = "" Then Exit Sub
ActiveCell.Value = Sqr(num)
Exit Sub
Badentry:
msg = "An error occurred. Try again?"
ans = MsgBox(msg, vbYesNo)
If ans = vbYes Then Resume TryAgain
End Sub
Sub entersquareroot2()
Dim cell As Range
Dim errmsg As String
If TypeName(Selection) <> "Range" Then Exit Sub
On Error GoTo errorhandler
For Each cell In Range("A26:A46")
cell.Value = Sqr(cell.Value)
Next
Exit Sub
errorhandler:
Select Case Err
Case 5
Resume Next
Case 13
Resume Next
Case 1004
MsgBox "The cell is locked. Try again."
Exit Sub
Case Else
errmsg = Error(Err.Number)
MsgBox "Error:" & errmsg
Exit Sub
End Select
End Sub
Sub Macro1()
If Not WorkbookOpen(“Prices.xlsx”) Then
MsgBox “Please open the Prices workbook first!”
Exit Sub
End If
‘ [Other code goes here]
End Sub
No comments:
Post a Comment