Can a worksheet object be declared globally in Excel VBA?

后端 未结 6 1408
余生分开走
余生分开走 2021-01-05 02:32

I\'m refactoring a number of modules in an Excel 2003 workbook and the same set of worksheets are declared in each procedure in each module; I\'d like to just declare them o

6条回答
  •  一向
    一向 (楼主)
    2021-01-05 02:52

    '1. Insert a module

    '2. Declare worksheet public variable in the module as follows

    Public xlwkGSModel As Worksheet
    

    '3. Instantiate this public variable in the application load event

    Sub Workbook_Open()
    
       Set xlwkGSModel = ActiveWorkbook.Worksheets("gs_model")
    
    End Sub
    

    'Now you can refer the gs_model worksheet with the xlwkGSModel variable

    'For example

    dim x as string
    
    x = xlwkGSModel.Cells(1,1)
    

提交回复
热议问题