0

I want that whenever the workbook is closed a Sub from a sheet(sheet13) should be called, but as shown in the screenshot Im getting the error Invalid or unqualified reference. By now I have tried

  B2_Click

  !Sheets("sheet13").B2_click

  !Sheets("Employee Names").B2_click

  Thisworkbook.Sheets("Employee Names").B2_click 

And I am getting an error every time.

enter image description here

Pᴇʜ
  • 56,719
  • 10
  • 49
  • 73
LOKE2707
  • 312
  • 1
  • 5
  • 19
  • 1
    Simply `Sheet13.B2_click` if your B2_click is `Public` and doesn't need parameters ... – Asger Mar 04 '19 at 10:24

2 Answers2

1

Your last example should work, so I would direct my troubleshooting towards the method you are calling. Try this in a new workbook:
In ThisWorkbook module

Private Sub Workbook_BeforeClose(Cancel As Boolean)
    ThisWorkbook.Worksheets("Sheet13").Test
End Sub

And in Sheet13 module

Sub Test()
    MsgBox "Closing"
End Sub

If that works, you have a starting point.

Sam
  • 5,424
  • 1
  • 18
  • 33
0

First off, the easy solution as already mentioned by others:
ThisWorkbook.Sheets("January").Something (string is case sensitive!)

There are multiple ways to reference a Sheet, and there are different types of names for worksheets. You have to differentiate between the "CodeName" and the "Name". You will easily find more information on the difference between those two.

The main differences are, that the Name can be changed by the user, it is the name visible on the tab at the bottom of the Excel Interface.
The CodeName can only be changed through the VBA Interface.
The CodeName and the Name are usually identical when creating a worksheet (Sheet1, etc..).

To Reference a Sheet by Name you can use this code: ThisWorkbook.Sheets("SomeName").SomeFunction()
The CodeName can be used directly like this: Sheet2.SomeFunction(), but I'd recommend changing the CodeName to something meaningful/expressive first!

(Name) is the CodeName, Name is the visible Name
Properties Sheet Example

More information on the Bang operator ! can be found here: https://rubberduckvba.wordpress.com/2018/03/15/vba-trap-default-members/

Extensive Answer on referencing Sheets:
https://stackoverflow.com/a/41481428/10223558

L8n
  • 728
  • 1
  • 5
  • 15