简体   繁体   English

在workbook_open上添加工作表

[英]Adding worksheets on workbook_open

I have an existing worksheet "StudentSheet1" which I need to add as many times as a user needs. 我有一个现有的工作表“ StudentSheet1”,需要根据用户需要添加多次。

For eg, if a user enters 3 in cell "A1", saves it and closes the workbook. 例如,如果用户在单元格“ A1”中输入3,则将其保存并关闭工作簿。

I want to have three sheets: "StudentSheet1" , "StudentSheet2" and "StudentSheet3" when the workbook is opened next time. 我想在下次打开工作簿时使用三张纸:“ StudentSheet1”,“ StudentSheet2”和“ StudentSheet3”。

So I will have the Code in "Workbook_Open" event. 因此,在“ Workbook_Open”事件中将包含代码。 I know how to insert new sheets, but cant insert this particular sheet "StudentSheet1" three times 我知道如何插入新的工作表,但无法将此特定工作表“ StudentSheet1”插入三遍

Here is my code: 这是我的代码:

Private Sub Workbook_Open()
    Application.ScreenUpdating = False
    Dim ws As Worksheet
    Set ws = ActiveWorkbook.Sheets.Add(Type:=xlWorksheet, After:=Worksheets(1))
    Application.ScreenUpdating = True
End Sub

EDIT 编辑

Sorry I misread the question, try this: 抱歉,我看不懂问题,请尝试以下操作:

Private Sub Workbook_Open()
    Dim iLoop As Integer
    Dim wbTemp As Workbook

    If Not Sheet1.Range("A1").value > 0 Then Exit Sub

    Application.ScreenUpdating = False

    Set wbTemp = Workbooks.Open(Filename:="//Ndrive/Student/Student.xlsm")

    wbTemp.Sheets("StudentSheet1").Copy After:=ThisWorkbook.Sheets(Sheets.Count)
    wbTemp.Close

    Set wbTemp = Nothing

    With Sheet1.Range("A1")
        For iLoop = 2 To .Value
            Sheets("StudentSheet1").Copy After:=ThisWorkbook.Sheets(Sheets.Count)
            ActiveSheet.Name = "StudentSheet" & iLoop
        Next iLoop

        .Value = 0
    End With

    Application.ScreenUpdating = True

End Sub

Why are you wanting to add sheets on the workbook open? 为什么要在打开的工作簿上添加工作表? If the user disables macros then no sheets will be added. 如果用户禁用宏,则不会添加任何工作表。 As Tony mentioned, why not add the sheets when called by the user? 正如Tony提到的,为什么在用户调用时不添加工作表?

EDIT As per @Sidd's comments, if you need to check if the sheet exists first use this function: 编辑根据@Sidd的注释,如果您需要先检查工作表是否存在,请使用以下功能:

Function SheetExists(sName As String) As Boolean
    On Error Resume Next
    SheetExists = (Sheets(sName).Name = sName)
End Function

user793468, I would recommend a different approach. user793468,我建议使用其他方法。 :) :)

wbTemp.Sheets("StudentSheet1").Copy After:=ThisWorkbook.Sheets(Sheets.Count)

is not reliable. 不可靠。 Please see this link . 请看这个链接


EDIT: The above code will fail if the workbook has defined names. 编辑:如果工作簿已定义名称,则上面的代码将失败。 Otherwise it is absolutely reliable. 否则,它是绝对可靠的。 Thanks to Reafidy for catching that. 感谢Reafidy抓住了这一点。

I just noticed OP's comment about the shared drive. 我刚刚注意到OP关于共享驱动器的评论。 Adding amended code to incorporate OP's request. 添加修改的代码以合并OP的请求。

Tried and Tested 历经考验

Option Explicit

Const FilePath As String = "//Ndrive/Student/Student.xlsm"

Private Sub Workbook_Open()
    Dim wb1 As Workbook, wb2 As Workbook
    Dim ws1 As Worksheet, ws2 As Worksheet
    Dim TempName As String, NewName As String
    Dim ShtNo As Long, i As Long

    On Error GoTo Whoa

    Application.ScreenUpdating = False

    Set wb1 = ActiveWorkbook
    Set ws1 = wb1.Sheets("Sheet1")

    ShtNo = ws1.Range("A1")

    If Not ShtNo > 0 Then Exit Sub

    Set wb2 = Workbooks.Open(FilePath)
    Set ws2 = wb2.Sheets("StudentSheet1")

    For i = 1 To ShtNo
        TempName = ActiveSheet.Name
        NewName = "StudentSheet" & i

        If Not SheetExists(NewName) Then
            ws2.Copy After:=wb1.Sheets(Sheets.Count)
            ActiveSheet.Name = NewName
        End If
    Next i

    '~~> I leave this at your discretion.
    ws1.Range("A1").ClearContents

LetsContinue:
    Application.ScreenUpdating = True

    On Error Resume Next
    wb2.Close savechanges:=False
    Set ws1 = Nothing
    Set ws2 = Nothing
    Set wb2 = Nothing
    Set wb1 = Nothing
    On Error GoTo 0

    Exit Sub
Whoa:
    MsgBox Err.Description
    Resume LetsContinue
End Sub

'~~> Function to check if sheet exists
Function SheetExists(wst As String) As Boolean
    Dim oSheet As Worksheet
    On Error Resume Next
    Set oSheet = Sheets(wst)
    On Error GoTo 0

    If Not oSheet Is Nothing Then SheetExists = True
End Function

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM