Using VBA to determine Max Values when multiple worksheets are involved

DonEB

Board Regular
Joined
Apr 26, 2016
Messages
133
Office Version
  1. 2019
Platform
  1. Windows
I was able to get the following VBA code to successfully work when there was only one worksheet involved. Unfortunately, once I added a second worksheet, the code no longer worked. However, I think I may understand the problem, I just don't know how to fix it.

When I had only one worksheet (named CommonData), the code below worked perfectly. I believe that's because any reference to cells or range of cells contained within the VBA code had only one worksheet to refer to.

Once I added a second worksheet (named Sheet1) and placed that worksheet in the first position, then the code below seem to referred to Sheet1 by default because when the code was run the results reflected the numbers contained with the ranges within the Sheet1 worksheet. Hope that makes sense.

What I'm trying to do is get the following code to refer to the CommonData worksheet. My first thought was to put Worksheets("CommonData").Range in front of the cell/range references in the code below but that didn't work for me so I must be messing up that code/logic somehow. Because I'm dealing with VBA, I'm getting myself a little confused with regards to where and when to appropriately use the "" to make this work.

If someone would be so kind as to provide some guidance on how to resolve this problem, I would very much appreciate it.

VBA Code:
Max1 = Evaluate("=LARGE(E5:INDEX(E5:X24," & n & "," & n & "),1)")
    
Max2 = Evaluate( _
    "=MAX(IF(E5:INDEX(E5:X24," & n & "," & n & ")<" & Max1 & ",E5:INDEX(E5:X24," & n & "," & n & ")))")
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
I wonder if this would work:
VBA Code:
Max1 = Evaluate("=LARGE(CommonData!E5:INDEX(CommonData!E5:X24," & n & "," & n & "),1)")
    
Max2 = Evaluate( _
    "=MAX(IF(CommonData!E5:INDEX(CommonData!E5:X24," & n & "," & n & ")<" & Max1 & ",CommonData!E5:INDEX(CommonData!E5:X24," & n & "," & n & ")))")
 
Upvote 0
How about
Excel Formula:
With Sheets("CommonData")
Max1 = .Evaluate("=LARGE(E5:INDEX(E5:X24," & n & "," & n & "),1)")
    
Max2 = .Evaluate( _
    "=MAX(IF(E5:INDEX(E5:X24," & n & "," & n & ")<" & Max1 & ",E5:INDEX(E5:X24," & n & "," & n & ")))")
End With
 
Upvote 0
Solution
How about
Excel Formula:
With Sheets("CommonData")
Max1 = .Evaluate("=LARGE(E5:INDEX(E5:X24," & n & "," & n & "),1)")
   
Max2 = .Evaluate( _
    "=MAX(IF(E5:INDEX(E5:X24," & n & "," & n & ")<" & Max1 & ",E5:INDEX(E5:X24," & n & "," & n & ")))")
End With
Thank you.... that worked. I was so close... I tried something similar but I forgot to put the period in front of Evaluate. Thanks again!!
 
Upvote 0
Glad we could help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,206
Members
452,618
Latest member
Tam84

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