VBA 從一個未開啟的Excel檔案中讀取資料到,已開啟的檔案中.
阿新 • • 發佈:2019-01-01
Sub CommandButton2_Click()
''根據專案名稱 獲取部門名
'A8 顯示在B8中 c3--c40
Dim xDis AsInteger
Dim xNo AsInteger
Dim strProject '專案名稱
Dim strDep '製造部門
Dim myPath$, myFile$, AK As Workbook, aRow%, tRow%, i AsInteger
Application.ScreenUpdating =False'凍結螢幕,以防螢幕抖動
myPath = ThisWorkbook.Path &"/"'把檔案路徑定義給變數
myFile =Dir(myPath &"data.xls") '依次找尋指定路徑中的*.xls檔案
xDis =40
strname = ActiveWorkbook.Name
Set AK = Workbooks.Open(myPath & myFile) '開啟符合要求的檔案
For xNo =3To xDis
strProject = Workbooks(strname).Worksheets("System").Range("A8").Value
strDep = Workbooks(strname).Worksheets("System ").Range("B8").Value
If (strProject = AK.Worksheets("二部").Range("C"&CStr(xNo)).Value) Then
Workbooks(strname).Worksheets("System").Range("B8").Value ="二部"
ExitFor
EndIf
If (strProject = Workbooks("data.xls").Worksheets("三部").Range("C"&CStr(xNo)).Value) Then
Workbooks(strname).Worksheets( "System").Range("B8").Value ="三部"
ExitFor
EndIf
If (strProject = Workbooks("data.xls").Worksheets("四部").Range("C"&CStr(xNo)).Value) Then
Workbooks(strname).Worksheets("System").Range("B8").Value ="四部"
ExitFor
EndIf
If (strProject = Workbooks("data.xls").Worksheets("五部").Range("C"&CStr(xNo)).Value) Then
Workbooks(strname).Worksheets("System").Range("B8").Value ="五部"
ExitFor
EndIf
Next xNo
Workbooks(myFile).Close False
Application.ScreenUpdating =True'凍結螢幕,此類語句一般成對使用
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
End Sub
''根據專案名稱 獲取部門名
'A8 顯示在B8中 c3--c40
Dim xDis AsInteger
Dim xNo AsInteger
Dim strProject '專案名稱
Dim strDep '製造部門
Dim myPath$, myFile$, AK As Workbook, aRow%, tRow%, i AsInteger
Application.ScreenUpdating =False'凍結螢幕,以防螢幕抖動
myPath = ThisWorkbook.Path &"/"'把檔案路徑定義給變數
myFile
xDis =40
strname = ActiveWorkbook.Name
Set AK = Workbooks.Open(myPath & myFile) '開啟符合要求的檔案
For xNo =3To xDis
strProject = Workbooks(strname).Worksheets("System").Range("A8").Value
strDep = Workbooks(strname).Worksheets("System
If (strProject = AK.Worksheets("二部").Range("C"&CStr(xNo)).Value) Then
Workbooks(strname).Worksheets("System").Range("B8").Value ="二部"
ExitFor
EndIf
If (strProject = Workbooks("data.xls").Worksheets("三部").Range("C"&CStr(xNo)).Value) Then
Workbooks(strname).Worksheets(
ExitFor
EndIf
If (strProject = Workbooks("data.xls").Worksheets("四部").Range("C"&CStr(xNo)).Value) Then
Workbooks(strname).Worksheets("System").Range("B8").Value ="四部"
ExitFor
EndIf
If (strProject = Workbooks("data.xls").Worksheets("五部").Range("C"&CStr(xNo)).Value) Then
Workbooks(strname).Worksheets("System").Range("B8").Value ="五部"
ExitFor
EndIf
Next xNo
Workbooks(myFile).Close False
Application.ScreenUpdating =True'凍結螢幕,此類語句一般成對使用
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
End Sub