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
 
The code looks consistent with your xlookup.
Give the msgbox a try and if you don't have any luck show me the message box and if possible an XL2BB of your row 6

I am login off for the night, so give me as much detail as you can so I can take a look tomorrow.
 
Upvote 0

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.

Forum statistics

Threads
1,223,909
Messages
6,175,310
Members
452,634
Latest member
cpostell

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