如何在vba中编写代码,以便每次函数调用它时都会在工作簿中创建不同的工作表

时间:2017-06-29 17:30:06

标签: excel vba ms-office

 Public Sub text1(st As String)
    Dim oXL As Object        ' Excel application
    Dim oBook As Object      ' Excel workbook
    Dim oSheet As Object     ' Excel Worksheet
    Dim i As Integer


    'Start Excel and create a new workbook
    Set oXL = CreateObject("Excel.application")
    Set oBook = oXL.Workbooks.Open("E:\karan.xlsx")
    Set ws = oBook.Sheets.Add
    oBook.activesheet.Name = st
    ws.Activate

1 个答案:

答案 0 :(得分:0)

根据我的理解,您希望在调用函数时添加工作簿。在这种情况下,你可以使用它:

Sub AddWorkbook()
    Sheets.Add After:=Sheets(Sheets.Count)
End Sub

如果您还想添加名称:

Private Sub AddWorkbookWithName(workbookName As String)
    Sheets.Add After:=Sheets(Sheets.Count)
    Sheets(Sheets.Count).Select
    Sheets(Sheets.Count).Name = workbookName
End Sub

Sub AddWorkbook()
    AddWorkbookWithName ("MyWorkbookName")
End Sub