I was recently trying to write an Excel macro, and I needed to determine whether a particular worksheet existed. My weapon of choice for scripting is Python, and my VBA skills are admittedly poor. So, in good pythonic style, I took the Easier to Ask Forgiveness than Permission approach and and wrote something like this:
Debug.Print MyWorkbook.Worksheets(TabName).Name ''Check for an index error
If Err.Number <> 0 Then
''Code to add the sheet to the workbook
This worked great until I tried to eliminate the print statement. I changed the first line to:
MyWorkbook.Worksheets(TabName).Name
And suddenly I started getting "Object doesn't support this property or method" errors. As a Python programmer this surprised me, I do similar things in Python all the time.
I was curious so I did a little reading. I was able to find documentation about expression statements like the above in Python, but not in VBA. Are expression statements simply not allowed in VBA? If so, is there a reason why they aren't? Does anyone know where I can read about this?