VBA Compiler Error: Invalid Qualifier

Ashfranc0223

New Member
Joined
Jan 17, 2019
Messages
1
When I execute the VBA code that is given below, I get an error "Compiler Error: Invalid Qualifier" and the name of subprocedure gets highlighted. Why does this happen? I have attached a copy of the code below:
Code:
Option Explicit
Sub TestA()


Dim numberrange As Range
Dim c As Range, maxv As Long, maxa As String


Set numberrange = Application.InputBox("Select a range to find max value", "Max Value", , , , , , 8)


maxv = numberrange.Cells(1, 1).Value
maxa = numberrange.Cells(1, 1).Address




For Each c In numberrange.Value
    If c.Value > maxv.Value Then
    c.Value = maxv.Value
    c.Address = maxa.Address
    End If
Next c


MsgBox ("max:" & maxv)


End Sub
 
Last edited by a moderator:

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Code:
[COLOR=#333333]c.Value = maxv.Value[/COLOR]
[COLOR=#333333]c.Address = maxa.Address[/COLOR]

You're asking the Value and Address of two variables you already explicitly made Values and Addresses in

Code:
[COLOR=#333333]maxv = numberrange.Cells(1, 1).Value[/COLOR]
[COLOR=#333333]maxa = numberrange.Cells(1, 1).Address[/COLOR]

Furthermore, the .Address property will return the column(s) and row(s) of whatever range it's attached to. Asking the address from Cells(1,1) will always return the same address, being $A$1.
 
Last edited:
Upvote 0
When I execute the VBA code that is given below, I get an error "Compiler Error: Invalid Qualifier" and the name of subprocedure gets highlighted. Why does this happen? I have attached a copy of the code below:

Option Explicit
Sub TestA()

Dim numberrange As Range
Dim c As Range, maxv As Long, maxa As String

Set numberrange = Application.InputBox("Select a range to find max value", "Max Value", , , , , , 8)

maxv = numberrange.Cells(1, 1).Value
maxa = numberrange.Cells(1, 1).Address

For Each c In numberrange.Value
If c.Value > maxv.Value Then
c.Value = maxv.Value
c.Address = maxa.Address
End If
Next c

MsgBox ("max:" & maxv)

End Sub
The variable maxv is declared as Long... it does not have a Value property. Try removing the .Value from the two red highlighted text above.

Similarly, the variable maxa is declared as a String... it does not have an Address property. Try removing the .Address from the blue highlighted text above.
 
Upvote 0
An alternative for you to consider after resolving the issues in your current code
- avoids looping
- uses Max function

Code:
Sub TestB()
    Dim NumberRange As Range, c As Range, v As Range, maxV As Double
    Set NumberRange = Application.InputBox("Select a range to find max value", "Max Value", , , , , , 8)
    maxV = WorksheetFunction.Max(NumberRange)
    Set v = NumberRange.Find(maxV, LookAt:=xlWhole)
    MsgBox "Max value = " & maxV & " in " & v.Address(0,0)
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,334
Messages
6,124,325
Members
449,154
Latest member
pollardxlsm

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top