【问题标题】:Unprotect sheet/Workbook取消保护工作表/工作簿
【发布时间】:2017-06-20 09:39:06
【问题描述】:

我设法让它工作,但我的一些客户端文件受到保护。

Sub VBA_Read_External_Workbook()

    ' Get customer workbook...
    Dim customerBook As Workbook
    Dim filter As String
    Dim caption As String
    Dim customerFilename As String
    Dim customerWorkbook As Workbook
    Dim targetWorkbook As Workbook
    Dim sheet As String
    
    ' make weak assumption that active workbook is the target
    Set targetWorkbook = Application.ActiveWorkbook
    
    ' get the customer workbook
    filter = "Text files (*.xlsb),*.xlsb"
    caption = "Please Select an input file "
    customerFilename = Application.GetOpenFilename(filter, , caption)
    
    Set customerWorkbook = Application.Workbooks.Open(customerFilename)
    sheet.Unprotect ("CADDRP")
    
    ' assume range is A1 - C10 in sheet1
    ' copy data from customer to target workbook
    Dim targetSheet As Worksheet
    Set targetSheet = targetWorkbook.Worksheets(1)
    Dim sourceSheet As Worksheet
    Set sourceSheet = customerWorkbook.Worksheets(3)
    
    targetSheet.Range("A1", "C10").Value = sourceSheet.Range("D85", "D95").Value
    
    ' Close customer workbook
    customerWorkbook.Close

End Sub

我把sheet.unprotect挤进去了。

它给了我一个错误

“需要对象”运行时错误“424”。

我猜我在这个过程中错过了一些变量声明?

【问题讨论】:

    标签: vba excel


    【解决方案1】:

    假设您需要Unprotect Sheet,而不是 Workbook.. 删除您当前拥有的 sheet.Unprotect 行并将其放回 设置 SourceSheet 之后:

    Set sourceSheet = customerWorkbook.Worksheets(3)
    sourceSheet.Unprotect ("CADDRP")
    

    【讨论】:

    • 谢谢CLR!!!只是意识到我把线放在了错误的地方。非常感谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多