2017-06-20 99 views
0
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 

末次撤消工作表/工作簿的Excel VBA

嗨,大家好, 所以我設法得到這個工作,但我的一些客戶端文件保護。所以搜索一下我已經設法擠壓一行sheet.unprotect代碼在裏面。但它給我一個錯誤,不是「Object Required」運行時錯誤'424'。我是一名擅長VBA的新手,希望能指出我的正確方向。我猜我錯過了過程中的一些變量聲明?

回答

2

假設你需要Unprotect,而不是工作簿 ..刪除sheet.Unprotect行,你有它目前並把它放回後設置SourceSheet:

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

謝謝CLR!只是意識到我把這條線放在了錯誤的地方。非常感謝 –