|
楼主 |
发表于 2014-4-9 13:50
|
显示全部楼层
本帖最后由 cloudshare 于 2014-4-9 13:55 编辑
zhaogang1960 发表于 2014-4-9 12:43
请参考下面附件
Private Sub CommandButton1_Click()
'AutoScreenUpdate False
'AutoCalculation False
Sheets("预算总成本").Range("a2:e1048576").ClearContents
Path = ThisWorkbook.Path & "\"
f = Dir(Path & "*.xls")
While f > ""
If f <> ThisWorkbook.Name Then
Dim cnn As Object, MyWorkSheet As String
MyWorkSheet = "预算成本总表$"
Set cnn = CreateObject("ADODB.Connection")
cnn.Open "Provider=Microsoft.Ace.OLEDB.12.0;Extended Properties=Excel 12.0;Data Source=" & Path & f
If cnn.OpenSchema(20, Array(Empty, Empty, MyWorkSheet, Empty)).EOF Then
GoTo a
Else
ThisWorkbook.Sheets("预算总成本").Cells(ThisWorkbook.Sheets("预算总成本").Cells(Rows.Count, 1).End(xlUp).Row + 1, 3) = GetValue(Path, f, "预算成本总表", "b2")
ThisWorkbook.Sheets("预算总成本").Cells(ThisWorkbook.Sheets("预算总成本").Cells(Rows.Count, 1).End(xlUp).Row + 1, 4) = GetValue(Path, f, "预算成本总表", "d2")
ThisWorkbook.Sheets("预算总成本").Cells(ThisWorkbook.Sheets("预算总成本").Cells(Rows.Count, 1).End(xlUp).Row + 1, 5) = GetValue(Path, f, "预算成本总表", "e28") - GetValue(Path, f, "预算成本总表", "e23") - GetValue(Path, f, "预算成本总表", "e24") - GetValue(Path, f, "预算成本总表", "e25") - GetValue(Path, f, "预算成本总表", "e26")
If ThisWorkbook.Sheets("预算总成本").Cells(Rows.Count, 1).End(xlUp).Row = 1 Then
ThisWorkbook.Sheets("预算总成本").Cells(ThisWorkbook.Sheets("预算总成本").Cells(Rows.Count, 1).End(xlUp).Row + 1, 1) = 1
Else
ThisWorkbook.Sheets("预算总成本").Cells(ThisWorkbook.Sheets("预算总成本").Cells(Rows.Count, 1).End(xlUp).Row + 1, 1) = ThisWorkbook.Sheets("预算总成本").Cells(ThisWorkbook.Sheets("预算总成本").Cells(Rows.Count, 1).End(xlUp).Row, 1).Value + 1
End If
End If
End If
a: f = Dir()
Wend
'AutoCalculation True
'AutoScreenUpdate True
End Sub
Sub rescreenupdate()
AutoScreenUpdate True
End Sub
Private Function GetValue(Path, File, Sheet, 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(Ref).Range("A1").Address(, , xlR1C1)
' 运行XLM宏
GetValue = ExecuteExcel4Macro(arg) ------打开文件选择对话框
End Function
为何执行到函数部分 GetValue = ExecuteExcel4Macro(arg) 为何会弹出打开文件对话框,让选择一个文件?
|
-
截图
|