sub 使用sql處理資料(
)
false
'建立資料庫相關物件
'建立資料庫連線
dim cnn as
object
set cnn = createobject(
"adodb.connection"
)'建立 包含描述資料來源模式目錄的集合
dim mycat as
object
set mycat = createobject(
"adox.catalog"
)'建立乙個資料集儲存資料
dim rst as
object
set rst = createobject(
"adodb.recordset"
)'設定資料庫連線
dim strconn$
strconn =
"provider=microsoft.ace.oledb.12.0;extended properties=excel 12.0;data source="
cnn.open strconn & thisworkbook.fullname
'遍歷資料夾,處理資料
dim sql$, sheetname$, r&
dim ph$, f$
ph = thisworkbook.path & "\" '路徑
f = dir(ph &
"*.xlsx"
)'檔名
dowhile f <>
""'迴圈所有檔案
if(f <> thisworkbook.name)*
(f <>
"0030.xlsx"
)then
'如果不是本檔案,進行提取
mycat.activeconnection = strconn & ph & f
sheetname = replace(mycat.tables(
0).name
,"'",""
)'替換表名中的單引號
'寫sql
sql = "select
* from [excel 12.0
;hdr=no;database=
" & ph & f & "].[" & sheetname & "]" '獲取第1列:f1 或 [第一列的列名]"
'sql = "select f1,f3 from [" & sheetname & "] where f1 is not null" '獲取第1列:f1 或 [第一列的列名]
'執行sql
rst.open sql, cnn,3,
1'填充資料
r = range(
"a"& rows.count).end
(xlup).row +
1 range(
"a"& r).resize(rst.recordcount)
="'"
&left
(f, instrrev(f,
".")-1
)' '去除工作簿字尾名,填充在「a」列
range(
"b"& r).copyfromrecordset rst '查詢結果
rst.close
endif
f = dir
loop
cnn.close
true
endsub
VBA高階 在VBA中使用SQL語句篩選日期
sub multipleselect group1 dim cnn as new adodb.connection dim rst as adodb.recordset dim mypath as string dim sql as string dim i as integer dim d as ...
在WPS中使用VBA7
從microsoft shared和system32中提取出vba及其依賴的基本檔案,複製並設定登錄檔 後,即可讓wps 2012使用vba7。但似乎wps2012只認放在office10中的mso.dll。所需的檔案有 vba目錄 只需vba6 vba7中的vbe6ext.olb,vbe7.dll...
在pandas中使用sql
from pandasql import sqldf 查詢記憶體中的pandas資料框 pysqldf lambda q sqldf q,globals 匯入模組,自帶資料,尋找pandas資料框 from pandasql import sqldf,load meat,load births py...