0

I have two sheets, with sheet 1 open I have a button click, and in the button click handler, I do:

Call Sheet2.DoSomething

In the DoSomething method, ActiveSheet refers to Sheet1 since that's the active sheet. Is there a way for DoSomething to reference its own sheet (Sheet 2) without explicitly specifying its name? So that if the sheet is renamed in the future, it won't break?

Other than fudging in passing a reference, I was specifically wondering if there was a keyword or something involving Me.

NibblyPig
  • 51,118
  • 72
  • 200
  • 356
  • Does this answer your question? [Difference between Thisworkbook.name and Activeworkbook.name in VBA](https://stackoverflow.com/questions/35426907/difference-between-thisworkbook-name-and-activeworkbook-name-in-vba) – braX Nov 16 '21 at 12:02
  • 1
    `me.Name` in DoSomething? – Nathan_Sav Nov 16 '21 at 12:04
  • Or just `Me` since that's a reference to the sheet itself. Most of the time, you don't even need that. – Rory Nov 16 '21 at 12:06
  • I would suggest to have a function/sub `DoSomething(ws as worksheet)` in normal module. Then call this function from Sheet1 and Sheet2 by `DoSomething me`. This would be a much clearer coding - and you can reuse it e.g. if Sheet3 wants to use `DoSomething` as well – Ike Nov 16 '21 at 13:26
  • Ah, thanks. I thought Me was something else because some of my code didn't seem to work without `ActiveSheet.` before it. I expect that will do the job. – NibblyPig Nov 16 '21 at 14:00

0 Answers0