How to find the MAX Value in a range defined with R1C1?

Melanie_

New Member
Joined
Feb 8, 2023
Messages
6
Office Version
  1. 2016
Platform
  1. Windows
Hi,

I'm new to VBA and this forum, so sorry, if this is a stupid question. I' trying to write a function for a university project. In this function there's a situation where I want to define a range with using R1C1 syntax. I don't want to use smth like "A1:B1" since the range is dependent on the cell in which the function is used (dynamic range?).
After defining the range I want to get the maxium value of it.

See the code below. I don't know what's wrong with it, but it doesn't work. I tried defining the range with A1:B1 style as a test and this worked perfectly fine. I've been starring at this for a while now an can't figure out the problem. I appreciate any suggestions, since I couldn't find a solution using Google. Thanks!

Note: I want to use the function in Sheet x, but the range is defined in sheet y ("Windspeed_all").
Code:

Function JustATest()

'defining variables
Dim h_address As String
Dim h_row As Integer
Dim v_row As Integer
Dim v_range As Range
Dim v_max As Double

h_address = Application.Caller.Address
h_row = Range(h_address).Row
v_row = h_row + 1

'everything is fine to this point!
'now I want to define my range, i tried it like this:

Set v_range = Application.ThisWorkbook.Worksheets("Windspeed_all").Range(Cells(v_row, 3), Cells(v_row, 37))
v_max = Application.WorksheetFunction.Max(v_range)
JustATest = v_max

End Function
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Try this:
- you need to tell VBA that the 2 references to Cells belong to Worksheets("Windspeed_all")

VBA Code:
With ThisWorkbook.Worksheets("Windspeed_all")
    Set v_range = .Range(.Cells(v_row, 3), .Cells(v_row, 37))
    v_max = Application.WorksheetFunction.Max(v_range)
End With
 
Upvote 0
Solution
Try this:
- you need to tell VBA that the 2 references to Cells belong to Worksheets("Windspeed_all")

VBA Code:
With ThisWorkbook.Worksheets("Windspeed_all")
    Set v_range = .Range(.Cells(v_row, 3), .Cells(v_row, 37))
    v_max = Application.WorksheetFunction.Max(v_range)
End With
Oh, thanks alot for the quick help! This worked, yay :)
 
Upvote 0
I got stuck again...maybe you can help. In the next step I want to find the address of v_max, take the column number, and pick a value in this very column but another row...
I tried this:

With ThisWorkbook.Worksheets("Windspeed_all")
Set v_range = .Range(.Cells(v_row, 3), .Cells(v_row, 37))
v_max = Application.WorksheetFunction.Max(v_range)
v_max_address = v_range.Find(What:=v_max).Address
End With
 
Upvote 0
I got stuck again...maybe you can help. In the next step I want to find the address of v_max, take the column number, and pick a value in this very column but another row...
I tried this:

With ThisWorkbook.Worksheets("Windspeed_all")
Set v_range = .Range(.Cells(v_row, 3), .Cells(v_row, 37))
v_max = Application.WorksheetFunction.Max(v_range)
v_max_address = v_range.Find(What:=v_max).Address
End With
Argh, posted befor finishing code. Again. I tried this:

VBA Code:
With ThisWorkbook.Worksheets("Windspeed_all")
    Set v_range = .Range(.Cells(v_row, 3), .Cells(v_row, 37))
    v_max = Application.WorksheetFunction.Max(v_range)
    v_max_address = v_range.Find(What:=v_max).Address
    h_column = Range(v_max_address).Column
    height = .Range(4, h_column).Value
End With

JustATest = height
 
Upvote 0
Assuming you really do want the value in Row 4. Change the below.
Rich (BB code):
' Replace this
h_column = Range(v_max_address).Column
height = .Range(4, h_column).Value
' To this
h_column = .Range(v_max_address).Column    ' Added the "." before Range
height = .Cells(4, h_column).Value                    ' Changed Range to Cells
 
Upvote 0
Assuming you really do want the value in Row 4. Change the below.
Rich (BB code):
' Replace this
h_column = Range(v_max_address).Column
height = .Range(4, h_column).Value
' To this
h_column = .Range(v_max_address).Column    ' Added the "." before Range
height = .Cells(4, h_column).Value                    ' Changed Range to Cells
Yes, I want values in row 4. I tried your code, but unfortunately it doesn't work. Do you have another idea? But thanks anyway!
It looks like this now:

VBA Code:
With ThisWorkbook.Worksheets("Windspeed_all")
    Set v_range = .Range(.Cells(v_row, 3), .Cells(v_row, 37))
    v_max = Application.WorksheetFunction.Max(v_range)
    v_max_address = v_range.Find(What:=v_max).Address
    h_column = .Range(v_max_address).Column
    height = .Cells(4, h_column).Value
End With

JustATest = height

In the mean time I found another solution by just using xlookup. But I would still be interested in where my mistake is.
 
Upvote 0
What does, "it doesn't work" mean ?
Did it error out ? Did it give a different result ?
Show me your Xlookup formula that works.

Another thing you can do is add the code below just before your End With and see if what appears in the message box makes sense.

VBA Code:
      MsgBox "v_max: " & v_max & Chr(10) & _
             "v_max_address: " & v_max_address & Chr(10) & _
             "h_column: " & h_column & Chr(10) & _
             "Height address: " & .Cells(4, h_column).Address & Chr(10) & _
             "Height: " & Height
 
Upvote 0
What does, "it doesn't work" mean ?
Did it error out ? Did it give a different result ?
Show me your Xlookup formula that works.

Another thing you can do is add the code below just before your End With and see if what appears in the message box makes sense.

VBA Code:
      MsgBox "v_max: " & v_max & Chr(10) & _
             "v_max_address: " & v_max_address & Chr(10) & _
             "h_column: " & h_column & Chr(10) & _
             "Height address: " & .Cells(4, h_column).Address & Chr(10) & _
             "Height: " & Height
I'm using excel in german and the result after using the function is "#WERT!", I think in english it would be "#VALUE!"

My xlookup formula, that works (german): =XVERWEIS(I5;Windspeed_all!C6:AK6;Windspeed_all!$C$4:$AK$4)
where I5 is the max. windspeed (v_max) which I determined in another column by =MAX(Windspeed_all!C6:AK6)


I will try your suggestions later today, thanks!! This will for sure be helpfull while debugging
 
Upvote 0

Forum statistics

Threads
1,223,896
Messages
6,175,260
Members
452,627
Latest member
KitkatToby

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