excel 复制工作表并同时清除内容

hyrbngr7  于 2022-11-26  发布在  其他
关注(0)|答案(2)|浏览(177)

我正在使用此功能清除内容

Sub ClearData()

Range("K2,J3,B18:B38,H18:H38,I18:I38,J18:J38,F44").Value = ""

End Sub

另一个函数用于复制最后一个具有相同内容的工作表并给予命名

Public Sub CopySheetAndRename()
  Dim newName As String

  On Error Resume Next
  newName = InputBox("Enter the name for the copied worksheet")

  If newName <> "" Then
    ActiveSheet.Copy After:=Worksheets(Sheets.Count)
    On Error Resume Next
    ActiveSheet.Name = newName
  End If
  
End Sub

但我需要的是,当我按下按钮,以创建新的复制工作表,我也什么清除一些单元格在新的工作表。现在我有两个按钮,我只想一个按钮,必须做什么,其他2个正在做的。
我是新手,还在学习。
我试着合并代码,但没有成功。
谢谢大家的帮助。现在如果有人知道如何摆脱下一个文本框每次我做一个新的工作表,请告诉。

e4eetjau

e4eetjau1#

复制工作表

Option Explicit

Sub DuplicateWorksheet()
    
    ' Define constants.
    Const PROC_TITLE As String = "Duplicate Worksheet"
    Const IB_PROMPT As String = "Enter the name for the duplicate worksheet"
    Const CLEAR_RANGE As String = "K2,J3,B18:B38,H18:H38,I18:I38,J18:J38,F44"
    
    ' Input the destination worksheet name.
    Dim NewName As String: NewName = InputBox(IB_PROMPT, PROC_TITLE)
    
    ' Check if no entry or the dialog was canceled.
    If Len(NewName) = 0 Then
        MsgBox "No name entered.", vbExclamation, PROC_TITLE
        Exit Sub
    End If
    
    ' Reference the source worksheet and the workbook.
    Dim sws As Worksheet: Set sws = ActiveSheet
    Dim wb As Workbook: Set wb = sws.Parent
    
    Application.ScreenUpdating = False
    
    ' Copy the source and reference this copy, the destination worksheet.
    sws.Copy After:=wb.Sheets(wb.Sheets.Count) ' after last sheet
    Dim dws As Worksheet: Set dws = wb.Sheets(wb.Sheets.Count) ' last sheet
    
    Dim ErrNum As Long
    
    ' Attempt to rename the destination worksheet.
    On Error Resume Next ' prevent several possible errors
        dws.Name = NewName
        ErrNum = Err.Number
    On Error GoTo 0
    
    ' Check if the rename was successful.
    If ErrNum <> 0 Then ' could not rename
        Application.DisplayAlerts = False
            dws.Delete
        Application.DisplayAlerts = True
        MsgBox "Could not rename to '" & NewName & "'.", _
            vbExclamation, PROC_TITLE
        Exit Sub
    'Else ' could rename; do nothing (continue)
    End If
    
    ' Clear.
    dws.Range(CLEAR_RANGE).ClearContents
               
    ' Save!?
    'wb.Save
               
    Application.ScreenUpdating = True
    
    ' Inform.
    MsgBox "The name of the duplicated worksheet is '" & NewName & "'.", _
        vbInformation, PROC_TITLE
  
End Sub
ax6ht2ek

ax6ht2ek2#

您可以从宏中调用另一个宏。在CopySheetAndRename宏中,如果编写Call ClearData或甚至只编写ClearData(因为VBA可以自己计算出Call部分),VBA将在CopySheetAndRename宏中运行ClearData

Public Sub CopySheetAndRename()
  Dim newName As String

  On Error Resume Next
  newName = InputBox("Enter the name for the copied worksheet")

  If newName <> "" Then
    ActiveSheet.Copy After:=Worksheets(Sheets.Count)
    On Error Resume Next
    ActiveSheet.Name = newName
    ClearData
  End If
  
End Sub

我对你的代码的建议是不要使用ActiveSheet。随着你的代码变得越来越复杂,特别是当你在执行过程中创建或更改工作表时,ActiveSheet将很难跟踪,并可能最终给你带来问题。根据我的经验,一些东西最终会在错误的工作表上运行并损坏所有数据。
ClearData中添加一个Worksheet参数,这样你就可以确定你没有在错误的工作表上运行它。并且在ActiveSheet发生任何变化之前,尽快将新的工作表保存为变量。

Sub ClearData(Target As Worksheet)
  Target.Range("K2,J3,B18:B38,H18:H38,I18:I38,J18:J38,F44").Value = ""
End Sub
Public Sub CopySheetAndRename()
  Dim newName As String

  On Error Resume Next
  newName = InputBox("Enter the name for the copied worksheet")

  If newName <> "" Then
    ActiveSheet.Copy After:=Worksheets(Sheets.Count)
    Dim ws as Worksheet
    Set ws = Worksheets(Worksheets.Count)
    On Error Resume Next
    ws.Name = newName
    ClearData ws
  End If
  
End Sub

相关问题