0

Hi, I am trying to get the max number from a range. I have tied 2 approaches to do this, one is working other is not but it should work. Below code is working for me, I can get max with this code.

Dim mymax As Range
    Set mymax = Range("b13").CurrentRegion
    Range("b11").Value = Excel.Application.WorksheetFunction.Max(mymax)

Now the code below this is not working, why it is not working? Technically it should work. Can you please let me why its not giving me max number?

Dim mymax As String
       mymax = Range("b13").CurrentRegion.Address
       Range("b11").Value = Excel.Application.WorksheetFunction.Max(mymax)

Thanks for your time and opinion.

5
  • Just have a look at the documentation: Arguments that are error values or text that cannot be translated into numbers cause errors.
    – Storax
    Jun 20, 2020 at 10:15
  • Have read the documentation. So we need to feed number, name, array or references. What I dont get is .address returns reference address say "A1:C30", yes the data type is string. If we use =max(address) in excel sheet it gives max value. Above code returns "Excel.Application.WorksheetFunction.Max(A1:C3) so should it not be working here. Thats what I don't understand.
    – Ali Zubair
    Jun 20, 2020 at 14:00
  • And the address (A1:C30) returned by variable mymax contains numbers
    – Ali Zubair
    Jun 20, 2020 at 14:16
  • In VBA A1:C3 is not defined, only "A1:C3" is considered as a string and in the excel sheet A1:C3 is a range. To make it clearer if you enter in your sheet the formula =MAX("A1:C3") you will get an error.
    – Storax
    Jun 20, 2020 at 14:22
  • @Storax I understand it now. And -FaneDuru's code makes it very clear. Using "Range" with max makes it work >> Excel.Application.WorksheetFunction.Max(Range(mymax))
    – Ali Zubair
    Jun 20, 2020 at 14:33

2 Answers 2

1

What you tried in the second code will work only in the next two ways:

Dim mymax As String
    mymax = Range("b13").CurrentRegion.Address
    Range("b11").Value = Excel.Application.WorksheetFunction.Max(Range(mymax))

You must (re)transform the string in a range...

Dim myArr As Variant
    myArr = Range("b13").CurrentRegion.Value
    Range("C11").Value = Excel.Application.WorksheetFunction.Max(myArr)

Max function works for arrays, too...

2
  • @Ali Zubair: Should I understand that my code answers your question? If yes, glad I could help! In such a case, we here tick the code left side check box in order to make it **accepted answer"". In such a case, somebody else searching for a similar issue will know that the answer was appropriate...
    – FaneDuru
    Jun 20, 2020 at 14:15
  • Yes it also works. I have tried the one that converts string to range. Now it makes sense, everything we do with cells or ranges in vba we use range to declare its a range so using it here makes sense. Will try the 2nd one also
    – Ali Zubair
    Jun 20, 2020 at 14:27
0

"Technically it should work"? What gives you that idea? You are passing a string to a function expecting a range - there is no "technically", it simply doesn't work as you are using a wrong variable type.

There is a reason the first example works. In VBA you set ranges and that's what in does.

In the second example you are using a string when a function is expecting a range which simply cannot work. The Max function on the spreadsheet would not work with a string input - there is no reason why it should this way in VBA.

Let me put this this way:

enter image description here

Your second code was the equivalent of formula in E1, which returns 0. What you are looking for, was the formula in E2, which returns 4.

3
  • The function is not expecting a range, I'd recommend to look at the doc. A call like WorksheetFunction.Max(rg1,12,25,rg2) would be valid. Even a call with a string would be valid in case the string converts into a number.
    – Storax
    Jun 20, 2020 at 10:18
  • I was saying technically becuase variable mymax returns reference address "A1:C30", and this address contains numbers so it should work. In excel we say =max(A1:C30) right, After running the code it becomes Excel.Application.WorksheetFunction.Max(A1:C30) so shouldn't it work?
    – Ali Zubair
    Jun 20, 2020 at 14:15
  • @AliZubair - there is a difference between returning a string which reads "A1:C30" or a range referring to "A1:C30". Please see my edited answer. Jun 21, 2020 at 1:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.