VBA to call WorksheetFunction from a different Workbook

SliderJeff

Board Regular
Joined
Oct 5, 2016
Messages
63
Hey guys,

I'm running into an issue where I can't seem to get my VBA to perform the WorksheetFunction.Max on the correct worksheet's column.

Here is the prototype for my subroutine so you can see what variables I have access to:

Code:
Sub FindExtremeValues(ByVal fileNum As Integer, ByRef fileName As String, ByRef outputWorkbook As Workbook, ByRef inputWorkbook As Workbook)

The key params to pay attention to are the outputWorkbook, which is where I want to store the result of the Max function, and inputWorkbook, where I want to grab Column B and perform the Max function. "fileName" is the same as the name of the sheet in inputWorkbook upon which I wish to perform the Max function.

Here is what I tried, but it appears that the "Columns"B:B"" portion of that line is grabbing the Column B from outputWorkbook and not inputWorkBook.
Code:
    Cells(3 + fileNum * NUM_PARAMS_IN_CSV, 2).Value = Workbooks(inputWorkbook.Name).Worksheets(fileName).Application.WorksheetFunction.Max(Columns("B:B"))

The left hand side of the equal sign is just fine. It is correctly storing the Max value into the appropriate cell in outputWorkbook.

So my questions are:

1.) How to I get the right hand side of the equation to perform the Max function on Column B of inputWorkbook?

2.) Is there a better/cleaner way to do all this "dereferencing" of workbooks, worksheets and functions than what I have written, as this seems exorbitantly long, likely because I am just cutting my teeth on VBA over the last week or two.

Please let me know if you need to see any other code in my sub or

Thanks for any help you can provide!

Regards,
Jeff
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Rich (BB code):
Cells(3 + fileNum * NUM_PARAMS_IN_CSV, 2).Value = Workbooks(inputWorkbook.Name).Worksheets(fileName).Application.WorksheetFunction.Max(Columns("B:B"))

Both items in red above are unqualified so VBA will take them from the active sheet. You need something like this:

Rich (BB code):
Cells(3 + fileNum * NUM_PARAMS_IN_CSV, 2).Value = Application.WorksheetFunction.Max(inputWorkbook.Worksheets(fileName).Columns("B:B"))

If would be even better to quality the Cells() on the left:

Rich (BB code):
outputWorkbook.Worksheets("SomeSheet").Cells(3 + fileNum * NUM_PARAMS_IN_CSV, 2).Value = Application.WorksheetFunction.Max(inputWorkbook.Worksheets(fileName).Columns("B:B"))

WBD
 
Upvote 0
PERFECT, WBD! Thanks a bunch! I'm still working my way through figuring out how best to call functions vs. referencing workbooks and worksheets and in what order to do it. Clearly I was doing it bass-ackwards based on your excellent correction of my code. Thanks again!

Regards,
Jeff
 
Upvote 0

Forum statistics

Threads
1,217,253
Messages
6,135,487
Members
449,942
Latest member
Gitad

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