VBA: Ignore 'Select Sheet' Popup When Creating Mass Reference Formulas

Ottsel

Board Regular
Joined
Jun 4, 2022
Messages
167
Office Version
  1. 365
Platform
  1. Windows
I have a macro that creates references to other workbooks, but sometimes it encounters workbooks where the sheet doesn't exist, so I'd like it to simply give me the #REF! error or even give me "0" value, both are fine.

I pull in my directory, and it will then give me this popup at times when it cannot locate the file:
1690842021172.png


I've tried multiple ways to bypass this, but nothing seems to work.
On Error Resume Next
Application.DisplayAlerts = False
Application.EnableEvents = False

Any tips, suggestions, tricks or advice would be greatly appreciated!
 

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"
I have a macro that creates references to other workbooks
It would help if you put your macro here.


If you want to try it on your own.
Check if the sheet exists in the workbook, if it exists then continue.

Adapt the following code in your macro to execute the function "WorksheetExists"

VBA Code:
Sub checksheet()
  Dim wbName As String
  Dim shName As String
  
  wbName = "test_file.xlsx"
  shName = "MB"
  
  If WorksheetExists(wbName, shName) Then
    MsgBox "Exists"
    'continue
  Else
    MsgBox "Not exists"
    'do nathing
  End If

End Sub


Put the following function in your module.
VBA Code:
Function WorksheetExists(wbName As String, shName As String)
  Dim shExists As Variant
  shExists = Evaluate("ISREF('[" & wbName & "]" & shName & "'!A1)")
  If Not IsError(shExists) Then WorksheetExists = True
End Function
 
Upvote 0
Thanks Dante! I'll see how I can combined that into my code.

As for the code, it's just a for loop that creates a reference formula using data that is in columns A:E
So, example would be:
A1 = D:
B1 = CONTRACT
C1 = ABC
D1 = TKC
E1 = ContractFile001.xls

Here's the code:
VBA Code:
    '...Setup
    row = Range("A:A").Cells.SpecialCells(xlCellTypeConstants).Count
    PC = "]MB'!$AJ$4"
    ED = "]MB'!$AP$4"
    TD = "]MB'!$AP$5"
    HDW_D = "]MB'!$G$7"
    MATL_D = "]MB'!$M$7"
    TRS_D = "]MB'!$U$7"
    HRL_D = "]LB'!$L$9"

    '...Loops
    For r = 2 To row
    DRV = "='" & CAFdir.Cells(r, 1).Value
    JC = "\" & CAFdir.Cells(r, 2).Value
    BLDR = "\" & CAFdir.Cells(r, 3).Value
    TRACT = "\" & CAFdir.Cells(r, 4).Value
    Filename = "\[" & CAFdir.Cells(r, 5).Value
        On Error Resume Next
        CAFdir.Cells(r, 6).Formula = DRV & JC & BLDR & TRACT & Filename & PC
        CAFdir.Cells(r, 7).Formula = DRV & JC & BLDR & TRACT & Filename & ED
        CAFdir.Cells(r, 8).Formula = DRV & JC & BLDR & TRACT & Filename & TD
        CAFdir.Cells(r, 9).Formula = DRV & JC & BLDR & TRACT & Filename & HDW_D
        CAFdir.Cells(r, 10).Formula = DRV & JC & BLDR & TRACT & Filename & MATL_D
        CAFdir.Cells(r, 11).Formula = DRV & JC & BLDR & TRACT & Filename & TRS_D
        CAFdir.Cells(r, 12).Formula = DRV & JC & BLDR & TRACT & Filename & HRL_D
    Next r
 
Upvote 0
It would help if you put your macro here.


If you want to try it on your own.
Check if the sheet exists in the workbook, if it exists then continue.

Adapt the following code in your macro to execute the function "WorksheetExists"

VBA Code:
Sub checksheet()
  Dim wbName As String
  Dim shName As String
 
  wbName = "test_file.xlsx"
  shName = "MB"
 
  If WorksheetExists(wbName, shName) Then
    MsgBox "Exists"
    'continue
  Else
    MsgBox "Not exists"
    'do nathing
  End If

End Sub


Put the following function in your module.
VBA Code:
Function WorksheetExists(wbName As String, shName As String)
  Dim shExists As Variant
  shExists = Evaluate("ISREF('[" & wbName & "]" & shName & "'!A1)")
  If Not IsError(shExists) Then WorksheetExists = True
End Function
I'm going to keep playing around and bug testing, but I attempted this and didn't have any luck. Perhaps its the function? I'm not the best with custom functions, so I wouldn't know exactly what to edit within in it.
VBA Code:
    For r = 2 To row
    wbName = CAFdir.Cells(r, 5).Value
    shName = "MB"
    DRV = "='" & CAFdir.Cells(r, 1).Value
    JC = "\" & CAFdir.Cells(r, 2).Value
    BLDR = "\" & CAFdir.Cells(r, 3).Value
    TRACT = "\" & CAFdir.Cells(r, 4).Value
    Filename = "\[" & CAFdir.Cells(r, 5).Value
        On Error Resume Next
        If WorksheetExists(wbName, shName) Then
            CAFdir.Cells(r, 6).Formula = DRV & JC & BLDR & TRACT & Filename & PC
            CAFdir.Cells(r, 7).Formula = DRV & JC & BLDR & TRACT & Filename & ED
            CAFdir.Cells(r, 8).Formula = DRV & JC & BLDR & TRACT & Filename & TD
            CAFdir.Cells(r, 9).Formula = DRV & JC & BLDR & TRACT & Filename & HDW_D
            CAFdir.Cells(r, 10).Formula = DRV & JC & BLDR & TRACT & Filename & MATL_D
            CAFdir.Cells(r, 11).Formula = DRV & JC & BLDR & TRACT & Filename & TRS_D
            CAFdir.Cells(r, 12).Formula = DRV & JC & BLDR & TRACT & Filename & HRL_D
        Else
            CAFdir.Cells(r, 6).Formula = 0
            CAFdir.Cells(r, 7).Formula = 0
            CAFdir.Cells(r, 8).Formula = 0
            CAFdir.Cells(r, 9).Formula = 0
            CAFdir.Cells(r, 10).Formula = 0
            CAFdir.Cells(r, 11).Formula = 0
            CAFdir.Cells(r, 12).Formula = 0
        End If
    Next r
 
Upvote 0
Let's go from the beginning.

Note1:
You put this:​
"I have a macro that creates references to other workbooks"
That means you have other books.​
Could you give an example of how you have the name of the other book?​
You have this, but I don't see what you have in the cell CAFdir.Cells(r, 5)​
wbName = CAFdir.Cells(r, 5).Value

Note2:
You should remove this statement to see what problems you have, that line will cause all errors to be ignored and we won't know what problems you have.​
On Error Resume Next​

Note3:
To help you better, you must give clear and complete examples.​
You need to provide as much information as possible so I'm not guessing what you have on your sheet and what you want to do.​

Note4:
Again I ask you, it would help if you put your macro here, but you must put your whole macro. And if you modified the function, then put the function here.​

--------------
I hope to hear from you soon.
Respectfully
Dante Amor
--------------​

"Si no sabes por dónde empezar, empieza por un café"
"If you don't know where to start, start with a coffee"
:coffee:
 
Upvote 0
Let's go from the beginning.

Note1:
You put this:​
"I have a macro that creates references to other workbooks"
That means you have other books.​
Could you give an example of how you have the name of the other book?​
You have this, but I don't see what you have in the cell CAFdir.Cells(r, 5)​
wbName = CAFdir.Cells(r, 5).Value

Note2:
You should remove this statement to see what problems you have, that line will cause all errors to be ignored and we won't know what problems you have.​
On Error Resume Next​

Note3:
To help you better, you must give clear and complete examples.​
You need to provide as much information as possible so I'm not guessing what you have on your sheet and what you want to do.​

Note4:
Again I ask you, it would help if you put your macro here, but you must put your whole macro. And if you modified the function, then put the function here.​

--------------
I hope to hear from you soon.
Respectfully
Dante Amor
--------------​

"Si no sabes por dónde empezar, empieza por un café"
"If you don't know where to start, start with a coffee"
:coffee:
Sorry for the confusion. I'll try to explain better. Hopefully this is enough, but first as you suggest; coffee :coffee:!
Now then,

1. Column E contains the name of the workbook. Columns A through D contain its path to create the reference.
• Example of workbook name: ADSERT-PH215B-PC7.xls
• Example of reference formula: ↓
Excel Formula:
='S:\JOBCARDS\ADS\ERT\[ADSERT-PH215B-PC7.xls]MB'!$AJ$4
2. Will do! But, to show you what I currently have, I left it in there for this breakdown. ***Refer to bullet point #4 for Macro***
3. Below I have provided the following information. Hopefully, this is clear.
CAFdir is the sheet name
1690914319207.png

Now, on the CAFdir sheet I have multiple rows of data that I create a custom formula to reference the other workbooks to avoid opening them:
Dante's Example.xlsm
ABCDE
1Column1Column2Column3Column4Column5
2S:JOBCARDSADSERTADSERT-PH215B-PC7.xls
CAF-Directory

Now, when its ran it'll populate data as such:
Dante's Example.xlsm
ABCDEFGHIJKL
1Column1Column2Column3Column4Column5Column6Column7Column8Column9Column10Column11Column12
2S:JOBCARDSADSERTADSERT-PH215B-PC7.xls707/10/202309/09/20230.00%70.00%72.00%48.00
CAF-Directory
Cell Formulas
RangeFormula
F2F2='S:\JOBCARDS\ADS\ERT\[ADSERT-PH215B-PC7.xls]MB'!$AJ$4
G2G2='S:\JOBCARDS\ADS\ERT\[ADSERT-PH215B-PC7.xls]MB'!$AP$4
H2H2='S:\JOBCARDS\ADS\ERT\[ADSERT-PH215B-PC7.xls]MB'!$AP$5
I2I2='S:\JOBCARDS\ADS\ERT\[ADSERT-PH215B-PC7.xls]MB'!$G$7
J2J2='S:\JOBCARDS\ADS\ERT\[ADSERT-PH215B-PC7.xls]MB'!$M$7
K2K2='S:\JOBCARDS\ADS\ERT\[ADSERT-PH215B-PC7.xls]MB'!$U$7
L2L2='S:\JOBCARDS\ADS\ERT\[ADSERT-PH215B-PC7.xls]LB'!$L$9

4. The macro as a whole:
VBA Code:
Sub ImportDataCAF()

    Dim r, y As Long 'row
    Dim row As Long 'row count
    Dim cellValue As String
    Dim PC, ED, TD, HDW_D, MATL_D, TRS_D, HRL_D As String
    Dim DRV, JC, BLDR, TRACT, Filename As String
    
    '...Setup
    CAFdir.Activate
    CAFdir.Select
    row = Range("A:A").Cells.SpecialCells(xlCellTypeConstants).Count
    PC = "]MB'!$AJ$4"
    ED = "]MB'!$AP$4"
    TD = "]MB'!$AP$5"
    HDW_D = "]MB'!$G$7"
    MATL_D = "]MB'!$M$7"
    TRS_D = "]MB'!$U$7"
    HRL_D = "]LB'!$L$9"
    CAFdir.Activate
    
    '...Prevent from Formulas filling all the way down
    Application.Calculation = xlCalculationManual
    Application.ScreenUpdating = False
    Application.AutoCorrect.AutoFillFormulasInLists = False
    Application.DisplayAlerts = False
    Application.EnableEvents = False

    For r = 2 To row
    '...uses data provided in Columns A:E to create reference
    DRV = "='" & CAFdir.Cells(r, 1).Value
    JC = "\" & CAFdir.Cells(r, 2).Value
    BLDR = "\" & CAFdir.Cells(r, 3).Value
    TRACT = "\" & CAFdir.Cells(r, 4).Value
    Filename = "\[" & CAFdir.Cells(r, 5).Value
        '...this'll be removed, don't worry! :)
        On Error Resume Next
        CAFdir.Cells(r, 6).Formula = DRV & JC & BLDR & TRACT & Filename & PC
        CAFdir.Cells(r, 7).Formula = DRV & JC & BLDR & TRACT & Filename & ED
        CAFdir.Cells(r, 8).Formula = DRV & JC & BLDR & TRACT & Filename & TD
        CAFdir.Cells(r, 9).Formula = DRV & JC & BLDR & TRACT & Filename & HDW_D
        CAFdir.Cells(r, 10).Formula = DRV & JC & BLDR & TRACT & Filename & MATL_D
        CAFdir.Cells(r, 11).Formula = DRV & JC & BLDR & TRACT & Filename & TRS_D
        CAFdir.Cells(r, 12).Formula = DRV & JC & BLDR & TRACT & Filename & HRL_D
    Next r
End sub
 
Upvote 0
Replace your macro with the following.
I added a new function. Copy all the code:

VBA Code:
Sub ImportDataCAF()
  'Each variable must have the type.
  Dim r As Long, row As Long 'row count
  Dim PC As String, ED As String, TD As String
  Dim HDW_D As String, MATL_D As String, TRS_D As String, HRL_D As String
  Dim DRV As String, JC As String, BLDR As String, TRACT As String, Filename As String
  Dim fullPath As String, bookName As String, shName1 As String, shName2 As String

  '...Prevent from Formulas filling all the way down
  Application.Calculation = xlCalculationManual
  Application.ScreenUpdating = False
  Application.AutoCorrect.AutoFillFormulasInLists = False
  Application.DisplayAlerts = False
  Application.EnableEvents = False
  
  '...Setup
  CAFdir.Select
  row = Range("A:A").Cells.SpecialCells(xlCellTypeConstants).Count
  PC = "]MB'!$AJ$4"
  ED = "]MB'!$AP$4"
  TD = "]MB'!$AP$5"
  HDW_D = "]MB'!$G$7"
  MATL_D = "]MB'!$M$7"
  TRS_D = "]MB'!$U$7"
  HRL_D = "]LB'!$L$9"
  
  For r = 2 To row
    '...uses data provided in Columns A:E to create reference
    DRV = "='" & CAFdir.Cells(r, 1).Value
    JC = "\" & CAFdir.Cells(r, 2).Value
    BLDR = "\" & CAFdir.Cells(r, 3).Value
    TRACT = "\" & CAFdir.Cells(r, 4).Value
    Filename = "\[" & CAFdir.Cells(r, 5).Value
    
    fullPath = Join(Application.Transpose(Application.Transpose(Range("A" & r & ":D" & r).Value)), "\")
    bookName = Range("E" & r).Value
    
    shName1 = "MB"
    If HasSheet(fullPath, bookName, shName1) Then
      CAFdir.Cells(r, 6).Formula = DRV & JC & BLDR & TRACT & Filename & PC
      CAFdir.Cells(r, 7).Formula = DRV & JC & BLDR & TRACT & Filename & ED
      CAFdir.Cells(r, 8).Formula = DRV & JC & BLDR & TRACT & Filename & TD
      CAFdir.Cells(r, 9).Formula = DRV & JC & BLDR & TRACT & Filename & HDW_D
      CAFdir.Cells(r, 10).Formula = DRV & JC & BLDR & TRACT & Filename & MATL_D
      CAFdir.Cells(r, 11).Formula = DRV & JC & BLDR & TRACT & Filename & TRS_D
    Else
      CAFdir.Cells(r, 6).Value = 0
      CAFdir.Cells(r, 7).Value = 0
      CAFdir.Cells(r, 8).Value = 0
      CAFdir.Cells(r, 9).Value = 0
      CAFdir.Cells(r, 10).Value = 0
      CAFdir.Cells(r, 11).Value = 0
    End If
    
    shName1 = "LB"
    If HasSheet(fullPath, bookName, shName1) Then
      CAFdir.Cells(r, 12).Formula = DRV & JC & BLDR & TRACT & Filename & HRL_D
    Else
      CAFdir.Cells(r, 12).Value = 0
    End If
  Next r
  
  Application.Calculation = xlCalculationAutomatic
  Application.ScreenUpdating = True
  Application.AutoCorrect.AutoFillFormulasInLists = True
  Application.DisplayAlerts = True
  Application.EnableEvents = True
End Sub

Function HasSheet(fPath As String, fName As String, sheetName As String)
  Dim f As String
  If Right(fPath, 1) <> "\" Then fPath = fPath & "\"
  f = "'" & fPath & "[" & fName & "]" & sheetName & "'!R1C1"
  HasSheet = Not IsError(Application.ExecuteExcel4Macro(f))
End Function
Note: The old function is no longer needed.

Regards
Dante Amor
🫡
 
Upvote 1
Solution
Replace your macro with the following.
I added a new function. Copy all the code:

VBA Code:
Sub ImportDataCAF()
  'Each variable must have the type.
  Dim r As Long, row As Long 'row count
  Dim PC As String, ED As String, TD As String
  Dim HDW_D As String, MATL_D As String, TRS_D As String, HRL_D As String
  Dim DRV As String, JC As String, BLDR As String, TRACT As String, Filename As String
  Dim fullPath As String, bookName As String, shName1 As String, shName2 As String

  '...Prevent from Formulas filling all the way down
  Application.Calculation = xlCalculationManual
  Application.ScreenUpdating = False
  Application.AutoCorrect.AutoFillFormulasInLists = False
  Application.DisplayAlerts = False
  Application.EnableEvents = False
 
  '...Setup
  CAFdir.Select
  row = Range("A:A").Cells.SpecialCells(xlCellTypeConstants).Count
  PC = "]MB'!$AJ$4"
  ED = "]MB'!$AP$4"
  TD = "]MB'!$AP$5"
  HDW_D = "]MB'!$G$7"
  MATL_D = "]MB'!$M$7"
  TRS_D = "]MB'!$U$7"
  HRL_D = "]LB'!$L$9"
 
  For r = 2 To row
    '...uses data provided in Columns A:E to create reference
    DRV = "='" & CAFdir.Cells(r, 1).Value
    JC = "\" & CAFdir.Cells(r, 2).Value
    BLDR = "\" & CAFdir.Cells(r, 3).Value
    TRACT = "\" & CAFdir.Cells(r, 4).Value
    Filename = "\[" & CAFdir.Cells(r, 5).Value
   
    fullPath = Join(Application.Transpose(Application.Transpose(Range("A" & r & ":D" & r).Value)), "\")
    bookName = Range("E" & r).Value
   
    shName1 = "MB"
    If HasSheet(fullPath, bookName, shName1) Then
      CAFdir.Cells(r, 6).Formula = DRV & JC & BLDR & TRACT & Filename & PC
      CAFdir.Cells(r, 7).Formula = DRV & JC & BLDR & TRACT & Filename & ED
      CAFdir.Cells(r, 8).Formula = DRV & JC & BLDR & TRACT & Filename & TD
      CAFdir.Cells(r, 9).Formula = DRV & JC & BLDR & TRACT & Filename & HDW_D
      CAFdir.Cells(r, 10).Formula = DRV & JC & BLDR & TRACT & Filename & MATL_D
      CAFdir.Cells(r, 11).Formula = DRV & JC & BLDR & TRACT & Filename & TRS_D
    Else
      CAFdir.Cells(r, 6).Value = 0
      CAFdir.Cells(r, 7).Value = 0
      CAFdir.Cells(r, 8).Value = 0
      CAFdir.Cells(r, 9).Value = 0
      CAFdir.Cells(r, 10).Value = 0
      CAFdir.Cells(r, 11).Value = 0
    End If
   
    shName1 = "LB"
    If HasSheet(fullPath, bookName, shName1) Then
      CAFdir.Cells(r, 12).Formula = DRV & JC & BLDR & TRACT & Filename & HRL_D
    Else
      CAFdir.Cells(r, 12).Value = 0
    End If
  Next r
 
  Application.Calculation = xlCalculationAutomatic
  Application.ScreenUpdating = True
  Application.AutoCorrect.AutoFillFormulasInLists = True
  Application.DisplayAlerts = True
  Application.EnableEvents = True
End Sub

Function HasSheet(fPath As String, fName As String, sheetName As String)
  Dim f As String
  If Right(fPath, 1) <> "\" Then fPath = fPath & "\"
  f = "'" & fPath & "[" & fName & "]" & sheetName & "'!R1C1"
  HasSheet = Not IsError(Application.ExecuteExcel4Macro(f))
End Function
Note: The old function is no longer needed.

Regards
Dante Amor
🫡
Outstanding!

This little section is super useful to know! (along with everything else). I'll have to try to apply this to other little projects for the future.
VBA Code:
    fullPath = Join(Application.Transpose(Application.Transpose(Range("A" & r & ":D" & r).Value)), "\")
    bookName = Range("E" & r).Value

As always, Dante, Thank you!
 
Upvote 0

Forum statistics

Threads
1,215,415
Messages
6,124,768
Members
449,187
Latest member
hermansoa

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