Trouble returning SumIfs from different workbook

Yamezz

Active Member
Joined
Nov 22, 2006
Messages
336
Office Version
  1. 2019
I have the below code, trying to update my Volume variable from a table in another workbook. The code returns an error, "Subscript out of range".
I presume my problem is how I have referred to the table on the MDF workbook, but I don't know how to reference it properly without activating the MDF workbook, running the SumIfs (without "Workbooks(MDF).", then re-activating the main workbook; which would be much slower.

Can anyone offer any help?

(The MDF variable is just a shorthand for the full filename)

VBA Code:
Volume = Application.WorksheetFunction.SumIfs(Workbooks(MDF).Sheets("Vessels").Range("G3", "G" & VRows), _
      Sheets("Vessels").Range("I3", "I" & VRows), Vintage, _
      Sheets("Vessels").Range("J3", "J" & VRows), Label, _
      Sheets("Vessels").Range("K3", "K" & VRows), Blend, _
      Sheets("Vessels").Range("L3", "L" & VRows), Batch)
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Does the MDF variable include a file path? If so, you can't use it as an index for the Workbooks collection - you need just the file name.
 
Upvote 0
Along with Rory's comment, you will need to qualify all the sheets references with the workbook, not just the first one.
 
Upvote 0
Thanks guys. I used
VBA Code:
MDF = Right(MonthlyDataFile, 14) 'extract filename only from entire file path
which works fine with something like
VBA Code:
Windows(MDF).Activate
I didn't even think about fully defining the rest of the references... Whoops.
 
Upvote 0
Glad we could help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,215,011
Messages
6,122,680
Members
449,091
Latest member
peppernaut

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