Search code examples
excelvba

Get a worksheet name using Excel VBA


I would like to create an user-defined function in Excel that can return the current worksheet. I could use the

sheetname = ActiveSheet.Name

But the problem with this is, it works and suddenly it starts to get different sheet name. For example, instead of SHEET I LOVE YOU it returns SHEET I HATE YOU.

Is there anyway to fix this - or it might possible because I think it can not be static but varies?


Solution

  • Function MySheet()
    
      ' uncomment the below line to make it Volatile
      'Application.Volatile
       MySheet = Application.Caller.Worksheet.Name
    
    End Function
    

    This should be the function you are looking for