Add formula only if sheet exists?

TheWennerWoman

Active Member
Joined
Aug 1, 2019
Messages
301
Office Version
  1. 365
Platform
  1. Windows
I have a master sheet with lots of data and have a routine that splits it into a number of sheets, depending on the number of records chosen by the user. So if there are 100,000 records in the master sheet and the user choses 20,000, the routine will create five new sheets.

I am trying to future-proof this so have the following
VBA Code:
Sheets("Control").Select
Range("E81").FormulaR1C1 = "=IFERROR(journal_1!R[-76]C[4],"""")"
Range("E82").FormulaR1C1 = "=IFERROR(journal_2!R[-77]C[4],"""")"
Range("E83").FormulaR1C1 = "=IFERROR(journal_3!R[-78]C[4],"""")"
Range("E84").FormulaR1C1 = "=IFERROR(journal_4!R[-79]C[4],"""")"
Range("E85").FormulaR1C1 = "=IFERROR(journal_5!R[-80]C[4],"""")"
Range("E86").FormulaR1C1 = "=IFERROR(journal_6!R[-81]C[4],"""")"
Range("E87").FormulaR1C1 = "=IFERROR(journal_7!R[-82]C[4],"""")"
Range("E88").FormulaR1C1 = "=IFERROR(journal_8!R[-83]C[4],"""")"
Range("E89").FormulaR1C1 = "=IFERROR(journal_9!R[-84]C[4],"""")"
Range("E90").FormulaR1C1 = "=IFERROR(journal_10!R[-85]C[4],"""")"
Range("E91").FormulaR1C1 = "=IFERROR(journal_11!R[-86]C[4],"""")"
Range("E92").FormulaR1C1 = "=IFERROR(journal_12!R[-87]C[4],"""")"

How can I amend the above to only add the formula if the sheet exists? In the above, journal_6 onwards wouldn't exist in this example.
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Could you use something like this?

VBA Code:
Sub Test()
  Dim i As Long
  Dim shName As String
  
  With Sheets("Control")
    For i = 1 To 12
      shName = "journal_" & i
      If ShtExists(shName) Then .Range("E" & 80 + i).FormulaR1C1 = "=IFERROR(" & shName & "!R5C[4],"""")"
    Next i
  End With
End Sub

Function ShtExists(SheetName As String, Optional wb As Workbook) As Boolean
  On Error Resume Next
  ShtExists = (IIf(wb Is Nothing, ActiveWorkbook, wb).Sheets(SheetName).Name <> "")
  On Error GoTo 0
End Function
 
Upvote 1
Solution

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

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