简体   繁体   中英

VBA invalid qualifier error

Not sure why I get an invalid qualifier error? Just looking in a range of numbers for least neg value and select (note ea number in ea of the cells is the result of a formula)

Sub Test()
    Dim c As Double
    c = .Find(What:=ActiveSheet.Evaluate("=LARGE(V17:V57,COUNTIF(V17:V57,"">0"")+1)"), LookIn:=xlValues)
            If c <> 0 Then
            c.Select
            End If
 End Sub

Based on comments so far the code is now as follows

Sub Test()
Dim c As Variant, a As Variant
    c = ActiveSheet.Evaluate("LARGE(V17:V57,COUNTIF(V17:V57,"">0"")+1)")
    Debug.Print c
    a = Application.Match(-0.199999999999818, "V17:V57", 0)
    'a = Application.Match(c, ActiveSheet.Range("V17:V57"))
    If IsError(a) Then Exit Sub
    Debug.Print ActiveSheet.Range("V17:V57").Cells(a, 1).Address
End Sub

.Find is a method of Range object.

You need to pass a range object.

General format would be:

Option Explicit
Sub Test()
    Dim c As Range
    Set c = ActiveSheet.Range("V17:V57").Find(What:="letter", LookIn:=xlValues)
    If Not c Is Nothing Then
        c.Select
    End If
End Sub

Note that c is a range object which is what is returned by .Find method. The example search range in ActiveSheet.Range("V17:V57") .

You test whether a value was found with the Is Nothing , as if the value wasn't found the range object c will be set to nothing.

You need to handle a possible error value from =LARGE(V17:V57,COUNTIF(V17:V57,">0")+1) if that is what the Evaluate formula produces otherwise you will get an error.

For example:

Option Explicit
Sub Test()
    Dim c As Range, a As Variant
    a = ActiveSheet.Evaluate("LARGE(V17:V57,COUNTIF(V17:V57,"">0"")+1)")
    If IsError(a) Then Exit Sub
    Set c = ActiveSheet.Range("V17:V57").Find(What:= a, LookIn:=xlValues)
    If Not c Is Nothing Then
        c.Select
    End If
End Sub

Make sure to format your column to shown the decimal places so that Find can match accurately.

See discussion here .


If you cannot format the sheet to show the require decimals try using Match. Sorry that I have c and a differently below.

Option Explicit
Sub Test()
    Dim c As Variant, a As Variant
    c = ActiveSheet.Evaluate("LARGE(V17:V57,COUNTIF(V17:V57,"">0"")+1)")
    a = Application.Match(c, ActiveSheet.Range("V17:V57"))
    If IsError(a) Then Exit Sub
    Debug.Print ActiveSheet.Range("V17:V57").Cells(a, 1).Address
End Sub

To use the entire sheet used range try this.

Sub Test()
    Dim c As Double
    With ActiveSheet.UsedRange
    Set c = .Find(What:=ActiveSheet.Evaluate("=LARGE(V17:V57,COUNTIF(V17:V57,"">0"")+1)"), LookIn:=xlValues)
            If c <> 0 Then
            c.Select
            End If
    End With
 End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM