三、 使用VB實現(xiàn)Excel自動獲取外部數(shù)據(jù)
(1) 進行手動獲取外部數(shù)據(jù)(1)步驟前,單擊“工具”菜單中的“宏”,選擇“錄制新宏”,在“宏名”的編輯框中鍵入宏名dbftoxls,按“確定”鍵;
(2) 完成手動獲取外部數(shù)據(jù)(1)-(8)步驟;
(3) 單擊“工具”菜單中的“宏”,選擇“停止錄制”。這樣就將獲取外部數(shù)據(jù)的過程記錄為宏。
(4) 編輯dbftoxls宏,加以修改,使它作為Visual Basic模塊表中的一個子程序,并設置調(diào)用參數(shù)。
提供的程序如下:
`設置初值
Const apppath = "c:\my documents\palmxls\"
Const modulefile = apppath + "module.xls"
Const staticspre = "TTT"
Const dbfpre = "ATV00"
`調(diào)用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
相關推薦:北京 | 天津 | 上海 | 江蘇 | 山東 |
安徽 | 浙江 | 江西 | 福建 | 深圳 |
廣東 | 河北 | 湖南 | 廣西 | 河南 |
海南 | 湖北 | 四川 | 重慶 | 云南 |
貴州 | 西藏 | 新疆 | 陜西 | 山西 |
寧夏 | 甘肅 | 青海 | 遼寧 | 吉林 |
黑龍江 | 內(nèi)蒙古 |