VBA

Lelewiwi

Board Regular
Joined
Nov 8, 2023
Messages
50
Office Version
  1. 365
Platform
  1. Windows
Hi all, below is just some of the code I'm working with. There is a lot in between but essentially I need to utilize the Sourcewb in a SumIfs formula but can't seem to make it work. The Sourcewb will already be open and will be closed when this formula is complete. Any help is appreciated!

VBA Code:
Sub Macro12()

Application.ScreenUpdating = False

    Dim Sourcewb As Workbook
        MsgBox "Please select Adjustments file", vbOKOnly
        Dim Dbox As FileDialog
        Dim File_Path As String

        Set Dbox = Application.FileDialog(msoFileDialogFilePicker)
        Dbox.Title = "Choose and Open" & FileType
        Dbox.Filters.Clear
        Dbox.Show
        If Dbox.SelectedItems.count = 1 Then File_Path = Dbox.SelectedItems(1)
        Set Sourcewb = Workbooks.Open(Filename:=File_Path)

        ActiveCell.FormulaR1C1 = "=SUMIFS('[" & Sourcewb.Name & "]'!C12,'[" & Sourcewb.Name & "]'!C1,RC[-2],'[" & Sourcewb.Name & "]'!C4,RC[-1])"

Application.ScreenUpdating = True
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Hi @Lelewiwi:
You have some details in your code

1. In this line the variable FileType is not defined nor does it have content
Dbox.Title = "Choose and Open" & FileType

2. If you select more files or do not select a file, then later you will get an error:
If Dbox.SelectedItems.count = 1 Then File_Path = Dbox.SelectedItems(1)​
For the above I put a validation.


3. I don't know what your active cell is, but I assume that it is the cell that you have active before opening the workbook, since when you open the workbook, the active cell will be the one that contains the workbook just now.
ActiveCell.FormulaR1C1​
For the above, I am setting the active cell to an object before opening the workbook..

4. The sheet name is missing from the following formula.
"=SUMIFS('[" & Sourcewb.Name & "] xxxxx '!C12,'​
For the above I am going to assume that the data is on the first sheet

Try this and comment:
VBA Code:
Sub Macro12()
  Dim SourceWb As Workbook
  Dim SourceSh As Worksheet
  Dim Dbox As FileDialog
  Dim File_Path As String, FileType As String
  Dim Act_cell As Range

  Application.ScreenUpdating = False
  Set Act_cell = ActiveCell
 
  MsgBox "Please select Adjustments file", vbOKOnly
  Set Dbox = Application.FileDialog(msoFileDialogFilePicker)
  Dbox.Title = "Choose and Open" & FileType
  Dbox.Filters.Clear
  Dbox.AllowMultiSelect = False
  Dbox.Show
 
  If Dbox.SelectedItems.Count = 1 Then
    File_Path = Dbox.SelectedItems(1)
  Else
    Exit Sub
  End If
 
  Set SourceWb = Workbooks.Open(Filename:=File_Path)
  Set SourceSh = SourceWb.Sheets(1)
  Act_cell.FormulaR1C1 = "=SUMIFS('[" & SourceWb.Name & "]" & SourceSh.Name & _
                           "'!C12,'[" & SourceWb.Name & "]" & SourceSh.Name & _
                     "'!C1,RC[-2],'[" & SourceWb.Name & "]" & SourceSh.Name & "'!C4,RC[-1])"
 
  Application.ScreenUpdating = True
End Sub

🧙‍♂️
 
Last edited:
Upvote 0
Solution

Forum statistics

Threads
1,223,896
Messages
6,175,259
Members
452,626
Latest member
huntinghunter

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