run time error 13'

bedsy

New Member
Joined
Jun 29, 2016
Messages
34
Calling all excel masters,

HI all - i am a new vba code enthusiast that needs some help.

i have the old runtime error with my code.

I'm trying to open a group of files (MyFiles) automatically, sumifs some ranges and then input the totals into a second workbook. the code will be run from (input.xls)

the code will cycle through each spreadsheet in the target folder transferring the sumifs to the input files next blank line.

any assistance would be greatly appreciated.

thanks you all in advance.

VBA Code:
Sub OpenAllWorkbooks()
    'Step 1:Declare your variables
    Dim MyFiles As String
   Dim dblAnswer As Double
    Application.ScreenUpdating = False
    Set ws1 = ActiveSheet
    Set wb2 = Workbooks(MyFiles)
    Set ws2 = wb.Sheets("Arrivals")
    
    
    'Step 2: Specify a target folder/directory, you may change it.
    MyFiles = Dir("G:testfile\02 February\*.xlsx")
    Do While MyFiles <> ""
        'Step 3: Open Workbooks one by one
        Workbooks.Open "G:\testfile\02 February\" & MyFiles

ws1.Range("B2") = Application.WorksheetFunction.SumIfs(ws2.Range("H4:H26"), ws2.Range("A4:A26"), "PN", ws2.Range("B4:B26"), "COAL")
ws1.Range("B1") = Application.WorksheetFunction.CountIfs(ws2.Range("A4:A26"), "PN", ws2.Range("B4:B26"), "COAL", ws2.Range("H4:H26"), ">0")

 
      
        MsgBox ActiveWorkbook.Name
        ActiveWorkbook.Close SaveChanges:=True

        'Step 4: Next File in the folder/Directory
        MyFiles = Dir
    Loop
End Sub
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
Code:
Set wb2 = Workbooks(MyFiles)
"MyFiles" doesn't exist when this line of code is executed (by extension neither does ws2). HTH. Dave
 
Upvote 0
Thanks Dave..........
So i'm a little confused to say the least but I can not figure out how to reference the workbook that will open via the Dir. Note: Ws2 is supposed to reference at target worksheet on the opened file as this may not necessarily be the active sheet when opened.
 
Upvote 0
This part is wrong. I also thought that the ws2 sheet was from wb2 which on re-read is not what U have(?). Anyways, it seems to me that this is what you want...
Code:
Do While MyFiles <> ""
'Step 3: Open Workbooks one by one
Set wb2 = Workbooks.Open("G:\testfile\02 February\" & MyFiles)
Set ws2 = wb2.Sheets("Arrivals")
'etc.
HTH. Dave
 
Upvote 0

Forum statistics

Threads
1,223,894
Messages
6,175,252
Members
452,623
Latest member
Techenthusiast

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