cena
cena

Reputation: 420

how to find multiple strings using range.value?

i tried to use range("A1:I1").value to find multiple strings at the first row however it shows that error "mismatch". What have i done wrong here? Is there another way to do it?

enter image description here

Dim sht as worksheet

Set sht = ThisWorkbook.Sheets("Result")

If sht.range("A1:I1").value = " Voltage" and sht.range("A1:I1").value = " Time"     ,<---------error 'mismatch' occurs here  

call powerandtime

Upvotes: 2

Views: 250

Answers (3)

Error 1004
Error 1004

Reputation: 8220

You could try:

Sub test()

    Dim arrStrings As Variant
    Dim i As Long, Counter As Long
    Dim rng As Range

    Set rng = ThisWorkbook.Worksheets("Sheet1").Range("A1:I1")
    Counter = 0

    arrStrings = Split("Time,Electric", ",")

    For i = LBound(arrStrings) To UBound(arrStrings)

        If Not rng.Find(arrStrings(i), lookat:=xlWhole) Is Nothing Then

            Counter = Counter + 1
            GoTo NextIteration

        End If

NextIteration:

    Next i

    If Counter = UBound(arrStrings) + 1 Then

        Call PowerAndTime

    End If

End Sub

Upvotes: 0

FaneDuru
FaneDuru

Reputation: 42236

Sub testMatchBis()
Dim sh As Worksheet, rng As Range, voltPos As Long, timePos As Long
Dim rngBis As Range, arrBis as Variant

 Set sh = ActiveSheet ' use please your sheet here
 Set rng = sh.Range("A1:I1")
 voltPos = IsMatch(rng, "Voltage")
 timePos = IsMatch(rng, "Time")
 If voltPos <> 0 And timePos <> 0 Then
    Set rngBis = sh.Columns(voltPos)
    Set rngBis = Union(rngBis, sh.Columns(timePos))
    arrBis = rngBis.Value 'the both columns content will be input in an array
    rngBis.Select 'both columns will be selected. Of course, you need to determine
         'only part of the comumn keeping values (their last row) and limit the range
    'Call call powerandtime 'You must know what this sub must do...
 Else
    MsgBox "(At least) one of your searched strings could not be found in the range..."
 End If
End Sub

    Private Function IsMatch(rng As Range, strS As String) As Long
        On Error Resume Next
        IsMatch = WorksheetFunction.Match(strS, rng, 0)
        If Err.Number <> 0 Then
            Err.Clear: On Error GoTo 0
            IsMatch = 0
        End If
        On Error GoTo 0
    End Function

Upvotes: 0

JvdV
JvdV

Reputation: 75850

The problem here is that you are comparing an array of values against a single value. In case of such a small array you can make use of some Application.Methods. Another option would be to use Range.Find on the actual Range object. I'll demonstrate both below:


Application.Methods

Sub Test()

Dim ws As Worksheet: Set ws = ThisWorkbook.Worksheets("Result")
Dim arr As Variant

With Application
    arr = .Transpose(ws.Range("A1:I1").Value)
    If .Count(.Match(Array("Voltage", "Time"), arr, 0)) = 2 Then
        Call PowerAndTime
    End If
End With

End Sub

What happens here is that .Match will return an array of two elements. It will either return an error value to the array if either "voltage" or "time" is not found, or it would return a numeric value when either one of them is found. Then .Count will count numeric values within that returned array, and only if the count would be 2, is when both values are present within your initial range.

Note: .Match needs a 1D-array, hence the .Transpose at the start.


Range.Find

Sub Test()

Dim ws As Worksheet: Set ws = ThisWorkbook.Worksheets("Result")
Dim rng1 As Range, rng2 As Range

Set rng1 = ws.Range("A1:I1").Find("Voltage", lookat:=xlWhole)
Set rng2 = ws.Range("A1:I1").Find("Time", lookat:=xlWhole)

If Not rng1 Is Nothing And Not rng2 Is Nothing Then
    Call PowerAndTime
End If

End Sub

So only when both "Voltage" and "Time" are found as xlWhole values within your specific range, it would continue to call PowerAndTime.

Upvotes: 1

Related Questions