取消保护工作表/工作簿Excel VBA

时间:2017-06-20 09:39:07

标签: excel vba excel-vba

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代码。但它给了我一个错误,而不是#34; Object Required"运行时错误' 424'我是一个擅长VBA的新手,如果可以指出我正确的方向,我将不胜感激。我猜测我在这个过程中错过了一些变量声明?

1 个答案:

答案 0 :(得分:2)

假设您需要Unprotect 工作表,而不是工作簿 ..删除您当前拥有它的sheet.Unprotect行并将其放入回到之后设置SourceSheet:

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