二、手动获取外部数据
(1) 单击“数据”,选取“获取外部数据”,单击“新建查询”;
(2) 出现“选取数据源”对话框,点中“使用查询向导创建/ 编辑查询”前的复选框,然后双击数据源名,如palm1;
(3) 在“查询向导——选择列”对话框中选择一个查询表名,单击 > 键,“查询中用到的列”框内会出现表中所有列名,单击“下一步”;
(4) 出现“查询向导——过滤数据”,单击“下一步”;
(5) 出现“查询向导——排序顺序”,单击“下一步”;
(6) 出现“查询向导——完成”,点中“将数据返回
Microsoft Excel”前的单选钮,单击“完成”;
(7) 出现“将外部数据返回到Excel”对话框,选中“新建工作表”,按“确定”;
(8) 在建立查询的工作簿内新建工作表,并放入转换好的数据。这样就将一个 dbf 文件转换好了。
(9)重复上述过程,所有子公司的dbf文件转换到同一个工作簿中。
三、 使用VB实现Excel自动获取外部数据
(1) 进行手动获取外部数据(1)步骤前,单击“工具”菜单中的“宏”,选择“录制新宏”,在“宏名”的编辑框中键入宏名dbftoxls,按“确定”键;
(2) 完成手动获取外部数据(1)-(8)步骤;
(3) 单击“工具”菜单中的“宏”,选择“停止录制”。这样就将获取外部数据的过程记录为宏。
(4) 编辑dbftoxls宏,加以修改,使它作为Visual Basic模块表中的一个子程序,并设置调用参数。
提供的程序如下:
`设置初值
Const apppath = "c:\my documents\palmxls\"
Const modulefile = apppath + "module.xls"
Const staticspre = "TTT"
Const dbfpre = "ATV00"
`调用dbftoxls的模块
Private Sub Cmdgeneratetable_Click()
Dim staticsfile As String
Dim s1 As String
Dim s2 As String
Dim s3 As String
Dim idyes As Integer
Dim dbfstring As String
On Error GoTo errhandler1
idyes = 6
s1 = txtyear.Text
s1 = Mid(s1, 3, 2)
s2 = txtmonth.Text
If Len(s2) = 1 Then
s2 = "0" + s2
End If
staticsfile = apppath + staticspre + s1 + s2 + ".xls"
If FileLen(staticsfile) > 0 Then
choice = MsgBox("该年月报表已存在,是否重新生成?", vbYesNo + vbExclamation + vbDefaultButton1, "")
If choice = idyes Then
Workbooks.Open FileName:=staticsfile
For i = 0 To companynum - 1
For j = 0 To tablenum - 1
dbfstring = dbfpre + Trim(Str$(j + 1)) + s2
sqlstring = sqlstringfunc(dbfstring, fieldlist(),
tablefieldnum(j))
Call dbftoxls(s(i, j), sqlstring)
Next j
Next i
ActiveWorkbook.Save
ActiveWorkbook.Close
End If
End If
Exit Sub
errhandler1:
Select Case Err
Case 53
Workbooks.Open FileName:=modulefile
s3 = s1 + "年" + s2 + "月"
Sheets("资产负债表").Range("e4").FormulaR1C1 = "'" + s3
ActiveWorkbook.SaveAs FileName:=staticsfile, FileFormat _
:=xlNormal, Password:="", WriteResPassword:="",
ReadOnlyRecommended:= _
False, CreateBackup:=False
For i = 0 To companynum - 1
For j = 0 To tablenum - 1
dbfstring = dbfpre + Trim(Str$(j + 1)) + s2
sqlstring = sqlstringfunc(dbfstring, fieldlist(),
tablefieldnum(j))
Call dbftoxls(s(i, j), sqlstring)
Next j
Next i
ActiveWorkbook.Save
ActiveWorkbook.Close
End Select
End Sub
`dbftoxls子程序
Sub dbftoxls(activesheetname, sqlstring)
Sheets(activesheetname).Activate
Cells.Select
Selection.Clear
Range("a1").Select
With ActiveSheet.QueryTables.Add(Connection:=Array(Array( _
"ODBC;CollatingSequence=ASCII;DBQ=C:\T\palm1;DefaultDir=
C:\T\palm1;Deleted=1;
Driver={Microsoft dBase Driver (*.dbf)};
DriverId=533;FIL"), Array( "=dBase III;
ImplicitCommitSync=Yes;
MaxBufferSize=512;
MaxScanRows= 8;
PageTimeout=600;
SafeTransactions=0;
Statistics=0;
Threads=3;Use" ), Array("rCommitSync=Yes;")),
Destination:=Range("A1"))
.Sql = Array( sqlstring)
.FieldNames = True
.RefreshStyle = xlInsertDeleteCells
.RowNumbers = False
.FillAdjacentFormulas = False
.RefreshOnFileOpen = False
.HasAutoFormat = True
.BackgroundQuery = True
.TablesOnlyFromHTML = True
.Refresh BackgroundQuery:=False
.SavePassword = True
.SaveData = True
End With
End Sub