I want the following problem in VBA or VBE(Visual Basic Editor) in Excel. (Pleas
ID: 3676894 • Letter: I
Question
I want the following problem in VBA or VBE(Visual Basic Editor) in Excel. (Please note: 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)
Write a function subroutine called NameExists of type Boolean that takes two arguments of type Range and String, respectively. It searches all of the cells in the given range and checks whether any of them have their Value property equal to the given string. For example, if the string is “Davis”, it checks whether “Davis” is in any of the cells. (A cell containing “Sam Davis” wouldn’t count. The contents have to be “Davis” exactly.) If so, it returns True. Otherwise, it returns False.
Explanation / Answer
updated pls check:
Function NameExists(ByVal searchName As String, nameRange As Range) As Boolean
Dim i As Long
Dim j As Long
Dim v As Variant
v = nameRange.Value
NameExists = False
For i = 1 To UBound(v, 1)
For j = 1 To UBound(v, 2)
If v(i, j) = searchName Then
NameExists = True
Exit Function
End If
Next j
Next i
End Function
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.