The last time this question was asked it was wrong! Write a function subroutine
ID: 3849302 • Letter: T
Question
The last time this question was asked it was wrong! Write a function subroutine called NameExists of type Boolean that takes two arguments of type Range and String, respectively. It should search all of the cells in the given range and check whether any of them have their Value property equal to the given range and check whether any of them have their Value property equal to the given string. For example, if the string is “Davis”, it should check whether “Davis” is any of the cells. (A cell containing “Sam Davis” wouldn’t count. The contents have to be “Davis” exactly, including case.) If so, it should return True. Otherwise, it should return False.
PLEASE add comments I have posted a file, 'Ch10_Ex08_TestSheet.xlsx' to give you a better idea, and please use the file to add your code and test it. O. As instructed, you need to create two subroutines (a main subroutine and a function subroutine called 'NameExists'). In the main subroutine, you need to get two user inputs ((1) range address (e.g., A1:C50), (2) Name String (e.g., James)), and call the function subroutine (by passing the inputs as arguments), and printout the result through Message Box as to whether the name exists or doesn't exist in the range. Again, both the search range and the name should be input from the users and don't get confused by the gray area in the provided file, which doesn't mean anything - Again, I only provided the file so that you can understand the assignment question better and use the file to test your code.
Explanation / Answer
Sub NameExists()
Dim blnExists As Boolean
Dim strValue As String
strValue = "Bob"
blnExists = Application.WorksheetFunction.CountIf(Range("A1:G100"), strValue)
If blnExists Then
MsgBox strValue & " Has Been Found", vbInformation, "Hooray!!!"
Else
MsgBox strValue & " Has Not Been Found", vbInformation, "YaBoo Sucks!!!"
End Sub
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.