excel Range类的PasteSpecial方法在第一次运行时失败,但在第二次运行时未失败

gudnpqoy  于 2022-11-18  发布在  其他
关注(0)|答案(1)|浏览(146)

我最初的计划是从一个合并和居中的范围复制到相同的大小在第二本书到没有可用的各种剪切和粘贴选项。
在做了更多的阅读和跟随别人后,我几乎得到了它后,尝试不同的复制和粘贴选项。复制从一个指定的工作簿范围(A1:A5),这些是文本值。粘贴到活动的工作簿,它可以是任何名称的范围(A35:A39)。
错误弹出窗口
执行阶段错误1004,Range类别的PasteSpecial方法失败。
单元格A1、A2、A3、A4和A5中的每一个都有一个文本语句,该文本语句被复制并粘贴到A35、A36、A37、A38和A39。
我可以运行脚本和错误框弹出,点击结束,并再次运行宏按钮,最终结果符合我的要求与5行有文本复制从另一本书从A到AG与文本居中横向,

Option Explicit
Private Sub UpdateForm1_Click()

    'Dim wsActive As Worksheet
    'Set wsActive = ThisWorkbook.ActiveSheet
    Dim wbActive As Workbook
    Set wbActive = ThisWorkbook
    
    Dim Up_Location As String
    Dim Up_Name As String
    Up_Location = "T:\Repeats\"
    Up_Name = "PNL_UPDATE.xlsx"
    
    
 Application.ScreenUpdating = False
    Workbooks.Open Up_Location & Up_Name
        Worksheets("Sheet1").Activate
            ActiveSheet.Range("Text_1").Select
                ActiveSheet.Range("Text_1").Copy

    
    wbActive.Sheets("CustQuote").Unprotect Password:="1234"
    
    wbActive.Worksheets("CustQuote").Activate
    wbActive.Worksheets("CustQuote").Range("A35:A39").Select
    wbActive.Worksheets("CustQuote").Range("A35:A39").PasteSpecial xlPasteAll

    Range("A35:AG39").Select
    With Selection
        .HorizontalAlignment = xlCenterAcrossSelection
        .VerticalAlignment = xlCenter
        .ReadingOrder = xlContext
    End With
    Range("E2").Select
    wbActive.Sheets("CustQuote").Protect Password:="1234"
    
    Application.CutCopyMode = False
    Windows("PNL_UPDATE.xlsx").Activate
    ActiveWorkbook.Close
 Application.ScreenUpdating = True

End Sub
tzcvj98z

tzcvj98z1#

您可能会从阅读How to avoid using Select in Excel VBA中受益。
粘贴前请先复制,否则中间的代码会相互干扰。请使用“变量”,而不要选择和激活工作表或区域。使用With块和/或变量可避免代码和工作表名称重复。

Option Explicit

Private Sub UpdateForm1_Click()
    Const ThisWbPassword As String = "1234" ' define password only once as constant.
    
    Dim Up_Location As String
    Up_Location = "T:\Repeats\"
    
    Dim Up_Name As String
    Up_Name = "PNL_UPDATE.xlsx"
    
    Dim WbUp As Workbook  ' set opened workbook to a variable for easy later use
    Set WbUp = Workbooks.Open(Up_Location & Up_Name)

    With ThisWorkbook.Worksheets("CustQuote")
        .Unprotect Password:=ThisWbPassword
        WbUp.Worksheets("Sheet1").Range("Text_1").Copy  ' always copy right before pasting to avoid interference of code between.
        
        With .Range("A35:A39")
            .PasteSpecial xlPasteAll
            .HorizontalAlignment = xlCenterAcrossSelection
            .VerticalAlignment = xlCenter
            .ReadingOrder = xlContext
        End With

       .Protect Password:=ThisWbPassword
    End With
    
    Application.CutCopyMode = False
    WbUp.Close SaveChanges:=False
End Sub

第二次运行代码时,代码会在.Unprotect之后停止运行,工作表处于未保护状态,因此第二次运行是在未保护的工作表上。
通常您会希望使用错误处理来覆盖它,这样您的工作表就不会以未受保护的状态结束。
这是一种基本的错误处理,用于确保在发生错误时再次保护工作表

Private Sub UpdateForm1_Click()
    Const ThisWbPassword As String = "1234" ' define password only once as constant.
    
    Dim Up_Location As String
    Up_Location = "T:\Repeats\"
    
    Dim Up_Name As String
    Up_Name = "PNL_UPDATE.xlsx"
    
    Dim WbUp As Workbook  ' set opened workbook to a variable for easy later use
    Set WbUp = Workbooks.Open(Up_Location & Up_Name)

    With ThisWorkbook.Worksheets("CustQuote")
        .Unprotect Password:=ThisWbPassword
        On Error GoTo ReProtect ' in case of any error make sure the sheet is protected again
        WbUp.Worksheets("Sheet1").Range("Text_1").Copy  ' always copy right before pasting to avoid interference of code between.
        
        With .Range("A35:A39")
            .PasteSpecial xlPasteAll
            .HorizontalAlignment = xlCenterAcrossSelection
            .VerticalAlignment = xlCenter
            .ReadingOrder = xlContext
        End With
        
ReProtect:
       .Protect Password:=ThisWbPassword
       Err.Raise Err.Number 'throw the error message after protecting the sheet
    End With
    
    Application.CutCopyMode = False
    WbUp.Close SaveChanges:=False
End Sub

相关问题