Hi everyone, I am not very familiar with VBA and I used the code below to import data from one workbook to another.
The data does import to the selected cells but if I click cancel after the file directory comes out, I get the 1004 error and its saying the underlined line is wrong. Also, the file opened for data import asks if I would like to save the changes after it is opened. Is there a way to make it go away too? Please let me know how I can fix this. Thank you very much!
Sub 按鈕1_Click()
' 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
' make weak assumption that active workbook is the target
Set targetWorkbook = Application.ActiveWorkbook
' get the customer workbook
filter = "Text files (*.xlsx),*.xlsx"
caption = "Please Select an input file "
customerFilename = Application.GetOpenFilename(filter, , caption)
Set customerWorkbook = Application.Workbooks.Open(customerFilename)
' 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(1)
targetSheet.Range("J4", "J43").Value = sourceSheet.Range("I16", "I50").Value
' Close customer workbook
customerWorkbook.Close
End Sub
Bookmarks