First and foremost I'm an amateur when it comes to coding, and some of the code I have, I've figured out how to do by googling, so if what I'm doing is not intuitive, inefficient, or unfeasible, then please let me know.
I've been working making a macro that allows me to quickly write a "narrative" from preselected text from a survey. I have a few worksheets within the workbook, 10 out of which I reference in my code. In order to facilitate my coding, I found out how to name the worksheets via the "set" function so that I could simply call the worksheet without having to write out "Thisworkbook.Worksheets("Name").Range()" every time I have to access data on the worksheets, and instead, I simply use Name.Range() (where Name is the name I've given the worksheet via the Set function). I recently discovered how to set code to run when the workbook opens, which I set up to clear any data I may have forgotten to erase in the Template, however, Only some of the workbooks are named and remembered in the code later on. These are GI, PF, and TF. For example, I could open the Immediate window and write "GI.Activate" and it will activate the worksheet (this also works for the PF and TF worksheets), however, "WSPSS.Activate" (or any of the other worksheets) gives me 424 Run time error. I was wondering why this was. Am I doing something different/wrong that doesn't allow VBA to remember the other names, or is there a limit to how much could be done in the workbook open subroutine? Heres my code:
Option Explicit
Public GI, PF, WSFA, WSCom, WSSC, WSSoc, GenInfo, TF, WSNarrative, RTables, WSTSS, WSTGAC, WSPSS, WSPGAC As Worksheet
Private Sub Workbook_Open()
Set GI = ThisWorkbook.Worksheets("General Information"): Set PF = ThisWorkbook.Worksheets("Parent Form")
Set TF = ThisWorkbook.Worksheets("Teacher Form"): Set WSFA = ThisWorkbook.Worksheets("FA")
Set WSCom = ThisWorkbook.Worksheets("Com"): Set WSSC = ThisWorkbook.Worksheets("SC")
Set WSSoc = ThisWorkbook.Worksheets("Soc"):
Set WSPSS = ThisWorkbook.Worksheets("Parent SS"): Set WSPGAC = ThisWorkbook.Worksheets("Parent GAC")
Set WSTSS = ThisWorkbook.Worksheets("Teacher SS"): Set RTables = ThisWorkbook.Worksheets("Tables")
If ThisWorkbook.Path = "" Then 'This is so that only "new" files are cleared, but saved ones are left alone
GI.OptionButtonF.Value = False: GI.OptionButtonM.Value = False
Range("SFN").ClearContents: Range("SMN").ClearContents: Range("SLN").ClearContents: Range("DoB").Value = ""
Range("PFN").ClearContents: Range("PLN").ClearContents: Range("PEvalDate").ClearContents
Range("TFN").ClearContents: Range("TLN").ClearContents: Range("TEvalDAte").ClearContents
PF.Range("B3:K28").ClearContents: PF.Range("B30:K30").ClearContents: PF.Range("E37:H40").Value = ""
TF.Range("B3:K28").ClearContents: TF.Range("B30:K30").ClearContents: TF.Range("E37:H40").Value = ""
End If
End Sub
Edit:
Ok, as Tim Williams noted in a comment below, I seem to have changed the names of the worksheets in the VBA project explorer tree, and they don't match the names of the worksheets as I'm trying to set them, with the exception of GI,TF and PF. So that's probably the issue there. I'll go back into the explorer tree and rename them to what I'm using in the code.
Side question, if I already named the worksheets via the explorer tree, is there a point to using the set function to set the names of the worksheets?