How to add a new spreadsheet with VBA-Code, using VBA

后端 未结 3 1674
情歌与酒
情歌与酒 2021-01-03 01:36

I am creating a macro and part of the macros function is to make VBA create a new spreadsheet. Because of the nature of distribution the name will change. I need to add code

3条回答
  •  礼貌的吻别
    2021-01-03 02:04

    The default .Add method adds a sheet at the start of the list. Often you want to add it at the end before adding the code lines, as explained by Siddarth Rout. To do that anywhere you can use:

        ActiveWorkbook.Worksheets.ADD  After:=ActiveWorkbook.Sheets(ActiveWorkbook.Worksheets.Count)
    

    It is easier to read if you have defined and set WB:

      Dim WB as Excel.workbook
      Set WB = ActiveWorkbook
          WB.Sheets.ADD  After:=WB.Sheets(WB.Sheets.Count)
          Set VBC = ActiveSheet     'If using in Siddarth Rout's code above
    

    Sheets and Worksheets are interchangeable, as illustrated.

提交回复
热议问题