0

Sorry for the noob question but I would like to understand the code I just got here. I'd like to explain these to my stakeholders in layman's terms.

  1. "On Error Resume Next"

    • Does this mean that if the first worksheet gives an error (just do not show it) then test the next worksheets for errors (still, do not show them)? So can it be considered a looping statement somehow without the errors popping up?
  2. "WorksheetExists = Not ws Is Nothing"

    • So if a true is found, that is equivalent to 1, right? Zero is for false. So that will give us "Not 1 is nothing" or "not zero is nothing" so how can someone explain it to a non programmer like a priest or a English teacher?
  3. Can "WorksheetExists = Not ws Is Nothing" be coded in a way that is friendlier to newbie programmers?

Complete Code:

Function WorksheetExists(sheet_name As String, Optional wb As Workbook) As Boolean

    Dim ws As Worksheet
    If wb Is Nothing Then Set wb = ThisWorkbook

    On Error Resume Next
        Set ws = wb.Sheets(sheet_name)
    On Error GoTo 0

'    If ws Is Nothing Then
'        WorksheetExists = False
'    Else
'        WorksheetExists = True
'    End If
'
    WorksheetExists = Not ws Is Nothing
    ' is ws not nothing? Yes, it is something. Return TRUE.
    ' is ws not nothing? No, it is really nothing. Return FALSE.

End Function

Thank you.

XYZKLM
  • 137
  • 1
  • 1
  • 8
  • To be honest, you might want to start with the [VBA docs](https://learn.microsoft.com/en-us/office/vba/api/overview/language-reference), and look for a thorough discussion of `On Error Resume Next`, `Not`, and `Nothing`... That might be more a more fruitful endeavor. You're bringing some extraneous stuff (loops, booleans, etc.) into the discussion. – BigBen Feb 26 '20 at 04:33
  • Hi BigBen, I will check it out. I still need a direction on how to explain these in layman's terms though. – XYZKLM Feb 26 '20 at 04:38
  • Like how do you convert "Not ws Is Nothing" in plain English that can be understood by a cab driver or an English teacher? – XYZKLM Feb 26 '20 at 04:39
  • Not ws Is Nothing = this worksheet exists? ws Is Nothing = this worksheet does not exist? – XYZKLM Feb 26 '20 at 04:42
  • Yep that's it, bingo. `Not ws Is Nothing` -> "`ws`is something" – BigBen Feb 26 '20 at 04:42
  • **that is friendlier to newbie programmers** `If ws Is Nothing Then WorksheetExists = False Else WorksheetExists = True` Though I like `Not ws Is Nothing` :) – Siddharth Rout Feb 26 '20 at 04:43
  • Just for the sake of readability, I will use If ws Is Nothing Then WorksheetExists = False Else WorksheetExists = True until I fully grasp the idea of the other one "Not ws Is Nothing"... Thanks again. – XYZKLM Feb 26 '20 at 04:45
  • For other simpler (*Easily understandable from newbies point of view*) versions you may want to see [Test or check if sheet exists](https://stackoverflow.com/questions/6688131/test-or-check-if-sheet-exists) – Siddharth Rout Feb 26 '20 at 04:52
  • I edited my code above and put comments for myself. This might help someone in the future. Thanks everyone! – XYZKLM Feb 26 '20 at 06:25

0 Answers0