Eric Carolus
Board Regular
- Joined
- Sep 17, 2012
- Messages
- 133
- Office Version
- 2016
- Platform
- Windows
Hi folks
I have searched extensively on Google for solution to my problem, to no avail.
I am testing the OK and Cancel buttons of my Input box (when pressing a button on a worksheet.
The correct responses (message boxes) occur when:
a). I choose a name/school class outside V18:ax18.
b). when I press OK without entering a name/class,
c). When I enter a class in the range mentioned above.
My problem is:
When I press Cancel, I get the same response as in a). above.
Can someone help please? Any and all help will be appreciated.
The code is as follows:
Thank you so much.
Crow
I have searched extensively on Google for solution to my problem, to no avail.
I am testing the OK and Cancel buttons of my Input box (when pressing a button on a worksheet.
The correct responses (message boxes) occur when:
a). I choose a name/school class outside V18:ax18.
b). when I press OK without entering a name/class,
c). When I enter a class in the range mentioned above.
My problem is:
When I press Cancel, I get the same response as in a). above.
Can someone help please? Any and all help will be appreciated.
The code is as follows:
VBA Code:
Private Sub CommandButton5_Click()
Dim myRange As Variant
Dim foundIt As Range
Dim seekclass As Range
Set seekclass = Worksheets("Sheet1").Range("V18:AX18")
Dim class_per_to_count As String
'Indicate the class whose periods you wish to find
Application.DisplayAlerts = False
class_per_to_count = UCase(Application.InputBox("Please indicate which class's periods do you wish to find." _
& vbNewLine _
& vbNewLine _
& " Thank you" _
& vbNewLine _
& vbNewLine, " Number periods of a class", Type:=2))
Application.DisplayAlerts = True
'Where to look for the class
Set foundIt = seekclass.Find(What:=class_per_to_count, LookIn:=xlValues, LookAt:=xlWhole)
'Determine if the class is in the range of classes, here Worksheets("Sheet1").Range("V18:AX18")
Dim foundcell As Variant
Set foundcell = seekclass.Find(class_per_to_count)
If foundcell Is Nothing Then
MsgBox "Sorry, the class that you entered," _
& vbNewLine _
& vbNewLine _
& "is not in the list of classes!" _
& vbNewLine _
& vbNewLine _
& ".........................................................." _
& vbNewLine _
& vbNewLine _
& "Thanks", vbInformation, "Nothing found"
Exit Sub
End If
Exit Sub
If StrPtr(class_per_to_count) = 0 Then
MsgBox "[Cancel] or [X] clicked"
ElseIf Len(class_per_to_count) = 0 Then
MsgBox "You clicked 'OK' but DID NOT ENTER a class." _
& vbCrLf _
& vbCrLf _
& "....................................................................." _
& vbCrLf _
& vbCrLf _
& " Thank you" _
, vbInformation, " NO class entered!"
ElseIf Len(class_per_to_count) = 0 Then
MsgBox "EYou did not enter a valid class." _
& vbCrLf _
& vbCrLf _
& "..........................................." _
& vbCrLf _
& vbCrLf _
& " Thank you.", vbInformation, " No valid class entered."
Exit Sub
ElseIf Len(class_per_to_count) = 2 And Not class_per_to_count Like "#[A-Za-z]" Then
MsgBox "2You did not enter a valid class." _
& vbCrLf _
& vbCrLf _
& "..........................................." _
& vbCrLf _
& vbCrLf _
& " Thank you.", vbInformation, " No valid class entered."
Exit Sub
ElseIf Len(class_per_to_count) = 3 And Not class_per_to_count Like "##[A-Za-z]" Then
MsgBox "3You did not enter a valid class." _
& vbCrLf _
& vbCrLf _
& "..........................................." _
& vbCrLf _
& vbCrLf _
& " Thank you.", vbInformation, " No valid class entered."
ElseIf Len(class_per_to_count) > 3 Then
MsgBox ">You did not enter a valid class." _
& vbCrLf _
& vbCrLf _
& "..........................................." _
& vbCrLf _
& vbCrLf _
& " Thank you.", vbInformation, " No valid class entered."
Exit Sub
Else
If Not foundIt Is Nothing Then
If Len(foundIt.Address) = 5 And foundIt.Offset(8, 0).MergeCells = True Then
If Len(foundIt.Address) = 5 And foundIt.MergeCells = True Then
If foundIt.Offset(8, 0).MergeCells = True Then
'The class has zero (0) periods
If foundIt.MergeArea.Offset(8, 0).Cells(1, 1).Value = 0 Or foundIt.MergeArea.Offset(8, 0).Cells(1, 1).Value = "" Then
MsgBox foundIt & " has 0 " & " periods" _
& vbCrLf _
& vbCrLf _
& "..........................................." _
& vbCrLf _
& vbCrLf _
& " Thank you.", vbInformation, " XNumber of periods per class"
Else
MsgBox foundIt & " has " & foundIt.MergeArea.Offset(8, 0).Cells(1, 1).Value & " periods" _
& vbCrLf _
& vbCrLf _
& "..........................................." _
& vbCrLf _
& vbCrLf _
& " Thank you.", vbInformation, " Number of periods per class"
End If
End If
End If
'The length of the class e.g.11A is like $11$A, i.e is 5 (five)
ElseIf Len(foundIt.Address) = 6 And foundIt.Offset(8, 0).MergeCells = True Then
If Len(foundIt.Address) = 6 And foundIt.MergeCells = True Then
If foundIt.MergeArea.Offset(8, 0).Cells(1, 1).Value = 0 Or foundIt.MergeArea.Offset(8, 0).Cells(1, 1).Value = "" Then
MsgBox foundIt & " has 0 " & " periods" _
& vbCrLf _
& vbCrLf _
& "..........................................." _
& vbCrLf _
& vbCrLf _
& " Thank you.", vbInformation, " XNumber of periods per class"
Else
MsgBox foundIt & " has " & foundIt.MergeArea.Offset(8, 0).Cells(1, 1).Value & " periods" _
& vbCrLf _
& vbCrLf _
& "..........................................." _
& vbCrLf _
& vbCrLf _
& " Thank you.", vbInformation, " Number of periods per class"
End If
End If
End If
Else
MsgBox "You simply cancelled the process!" _
& vbCrLf _
& vbCrLf _
& "......................................................." _
& vbCrLf _
& vbCrLf _
& " Thank you" _
, vbInformation, " Process cancelled!"
End If
End If
End Sub
Thank you so much.
Crow