chijanzen 發表於:2005.03.13 06:39 H0035 從其他檔案匯入資料-ExecuteExcel4Macro http://www.vba.com.tw/plog/post/1/169 Sub TestReadDataFromWorkbook() Dim r As Variant, s As String, n As Long '寫入標題列 For j = 1 To 3 Cells(3, j) = GetValue(ThisWorkbook.path, "test2.xls", "Sheet1", Cells(1, j).Address(0, 0)) Next i = 2 k = 3 Do '取得日期欄 r = GetValue(ThisWorkbook.path, "test2.xls", "Sheet1", Cells(i, 1).Address(0, 0)) '取得品名欄 s = GetValue(ThisWorkbook.path, "test2.xls", "Sheet1", Cells(i, 2).Address(0, 0)) '取得數量欄 n = GetValue(ThisWorkbook.path, "test2.xls", "Sheet1", Cells(i, 3).Address(0, 0)) If r = 0 Then Exit Do '最後一列跳出 If Month(CDate(r)) = 10 Then '篩選10月份資料 k = k + 1 Cells(k, 1) = CDate(r) Cells(k, 2) = s Cells(k, 3) = CLng(n) End If i = i + 1 Loop End Sub
Private Function GetValue(path, file, sheet, range_ref) Dim arg As String If Right(path, 1) <> "" Then path = path & "" If Dir(path & file) = "" Then GetValue = "File Not Found" Exit Function End If arg = "'" & path & "[" & file & "]" & sheet & "'!" & _ Range(range_ref).Range("A1").Address(, , xlR1C1) GetValue = ExecuteExcel4Macro(arg) End Function
Private Function GetValue(path, file, sheet, range_ref) Dim arg As String If Right(path, 1) <> "" Then path = path & "" If Dir(path & file) = "" Then GetValue = "File Not Found" Exit Function End If arg = "'" & path & "[" & file & "]" & sheet & "'!" & _ Range(range_ref).Range("A1").Address(, , xlR1C1) GetValue = ExecuteExcel4Macro(arg) End Function |