-1

I wrote a script that runs on my computer, but doesn't on my coworker's computer.

It fails, giving

"Runtime Error 91"

on the line:

Set citiFile = y.Sheets("VBA Inputs").Range("B4")

The workbook is located on a shared drive, which is linked to workbook y. The second line is giving me the error, implying that citiFile is not defined.

This script works when I run it from the shared drive off my computer, but errors out when run by my coworker from the same location. The same references are checked as well, since they're set in the file.

Sub ImportFiles()
Dim x As Workbook
Dim y As Workbook
Dim z As Workbook
Dim a As Workbook
Dim citiFile As Object
Dim jpmFile As Object
Dim rbEmail As String
Dim yesterdaysDate As Object
Dim todaysDate As Object
Dim shellEmail As String
Dim Month1 As String
Dim Month2 As Variant
Dim cel As Range
Dim tax As Range
Dim cell As String
Dim rng1 As Range
Dim rng2 As Range
Dim rng3 As Range
Dim rng4 As Range
Dim rng5 As Range
Dim rng6 As Range
Dim rng7 As Range
Dim rng8 As Range
Dim rng9 As Range
Dim rng10 As Range
Dim rng11 As Range
Dim rng12 As Range
Dim rng13 As Range
Dim rng14 As Range
Dim folder As Outlook.MAPIFolder
Dim olApp As Outlook.Application
Dim olNS As Outlook.Namespace
Dim olfldr As Outlook.MAPIFolder
Dim sharedemail As Outlook.Recipient
Dim sharedemail2 As Outlook.Recipient
Dim olMail As Variant
Dim myTasks As Outlook.Items
Dim regEx As Object
Dim Matches As Variant
Dim Matches2 As Variant
Dim Pattern1 As String
Dim Pattern2 As String
Dim BodyText
Dim wordDoc As Word.Document
Dim olRng As Range
Dim olMail2 As Outlook.MailItem
Dim daysAgo As Long
Dim citiSaveFolder As Object
Dim itm As Outlook.MailItem
Dim objAtt As Outlook.Attachment
Dim todaysDate2 As String
Dim priorSaveFolder As Object
Dim yesterdaysDate2 As String
Dim todaysDateTax As String
Dim rngFound As Range
Dim rngFound2 As Range
Dim offsetRng As Range
Dim i As Integer
Dim offsetRngResult As String
Dim marginSubject As String
Dim todaysDate3 As Object
Dim Pattern3 As String

Application.DisplayAlerts = False
'Make sure the following are checked in Developer > Visual Basic > Tools > References
'Visual Basic for Applications
'Microsoft Excel XX.0 Object Library
'OLE Automation
'Microsoft Office XX.0 Object Library
'Microsoft Word XX.0 Object Library
'Microsoft Outlook XX.0 Object Library
'Microsoft Internet Controls
'Microsoft Forms 2.0 Object Library
'Microsoft HTML Object Library


'Open workbooks first
Set y = Workbooks.Open("\\[Shared drive server location]\Forecast VBA.xlsm")
Set citiFile = y.Sheets("VBA Inputs").Range("B4")
Set jpmFile = y.Sheets("VBA Inputs").Range("B3")

Set yesterdaysDate = y.Sheets("VBA Inputs").Range("H1")
Set todaysDate = y.Sheets("VBA Inputs").Range("B1")
Set todaysDate3 = y.Sheets("VBA Inputs").Range("L1")
...
...
...
Community
  • 1
  • 1
givenss93
  • 43
  • 6
  • 1
    Does the coworker already have the file open? – BigBen Dec 07 '20 at 16:44
  • In terms of trouble-shooting ... can your co-worker open the file "Forecast VBA.xlsm" manually? It is a macro-enabled file (.xlsm), so maybe their security settings are blocking it? Does the processing change the file, if not, maybe try opening it ReadOnly (parameter of Workbooks.Open). https://learn.microsoft.com/en-us/office/vba/api/excel.workbooks.open . See Remarks section, about opening macro-enabled files. – DS_London Dec 07 '20 at 16:55
  • Maybe comment out the Application.DisplayAlerts=False line and see it any errors are reported? – DS_London Dec 07 '20 at 17:24
  • @BigBen Yes, they can open the file, the script just doesn't work for them. They can open the file, open the VBA script, run it, etc., they just get an error on that line that I do not get when running the same script also on same file in the shared drive. – givenss93 Dec 07 '20 at 17:54
  • 1
    @givenss93 - my question was more, does the coworker *already* have the file open? Calling `Workbooks.Open` on an already open file can cause issues. – BigBen Dec 07 '20 at 17:55
  • @DS_London Yes, they can open the workbook manually. They were actually trying to run the script from the workbook that's defined as y in the code above. I'll get them to try it without the displayalerts line. – givenss93 Dec 07 '20 at 17:56
  • @BigBen Yes, but I am also not having any issues when running the script from the open workbook. Any particular reason why I would be able to run it from the "y" worksheet and she would not be able to? – givenss93 Dec 07 '20 at 17:58
  • 1
    I would first test if the workbook is open before attempting to open it. As I mentioned, calling `Workbooks.Open` on an already open workbook can cause weird things to happen, specifically a similar error to yours where the workbook variable is `Nothing`. – BigBen Dec 07 '20 at 17:59
  • @BigBen so should I just set y = activeworkbook? – givenss93 Dec 07 '20 at 18:02
  • 1
    No, you can [test if the workbook is open](https://stackoverflow.com/a/9382034/9245853). Though I don't think you need the `Dir` call. Or modify one of the other answers in that thread. – BigBen Dec 07 '20 at 18:03
  • @BigBen Thanks - changing that line to y = ThisWorkbook made it work – givenss93 Dec 07 '20 at 18:07

1 Answers1

0

Changed

Set y = Workbooks.Open("\\[Shared drive server location]\Forecast VBA.xlsm")

to

Set y = ThisWorkbook

and it fixed the issue. Thanks, @BigBen.

givenss93
  • 43
  • 6