Mở Excel , bấm tổ hợp phím Alt_F11 để cửa sổ Microsoft Visual Basic mở ra .
Bẩm chọn menu Insert > Module
Bạn copy nội dung bên dưới
Private Function GetValue(path, file, sheet, ref)
Dim arg As String
If Right(path, 1) <> \"\\\" Then path = path & \"\\\"
arg = \"’\" & path & \"[\" & file & \"]\" & sheet & \"’!\" & Range(ref).Range(\"A1\").Address(, , xlR1C1)
GetValue = ExecuteExcel4Macro(arg)
End Function
Sub Sumworksheets()
Dim p, f, s, a, r
p = \"C:\\test\\\"
f = Dir(p & \"*.xls\")
TotalSum = 0
Do While f <> \"\"
s = \"Sheet1\"
a = \"A1\"
TotalSum = GetValue(p, f, s, a) + TotalSum
f = Dir()
Loop
Range(\"A2\") = TotalSum
End Sub
Quay trở lại Excel .
Chọn menu Developer > Macro
Chạy macro có tên là Sumworksheets , khi ấy bạn sẽ được kết quả như ý .Trong ví dụ này kết quả trả về ghi vào ô A2 của file mới