VBA+ADO不打开文件对TXT、CSV、EXCEL、ACCESS数据库、远程数据库进行数据提取、统计、汇总方法:超全、超实用的方法,基本上以解决80%以上的数据统计!!!
SUB 数据提取()
Set cnn = CreateObject("ADODB.CONNECTION")
Set rs = CreateObject("ADODB.RECORDSET")
{
**1)连接远程数据库方法:***
cnn.Open = "DSN=GSM;uid=ailxl;pwd=ailxl;Database=感知数据库;" 'TABLE=DBO.DATA;"
SQL="SELECT * FROM FROM [Data] DATA
**2)不打开CSV或TXT文件读取当前目录下的文件内容**
cnn.Open "Provider = Microsoft.ace.Oledb.12.0;Extended Properties =TEXT;Data Source =" & ThisWorkbook.Path & "\"
Sql = "select * from [XX.CSV] "
**3)不打开EXCEL读取当前目录下的文件内容**
cnn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties=Excel 12.0;Data Source=" & ThisWorkbook.FullName
Sql = "SELECT * FROM [xx$] "
**4)不找开ACCESS数据库读取当前目录下文件XX内容**
CNN.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & ThisWorkbook.Path & "\VOLTE用户统计.accdb"
Sql = "SELECT * FROM xx "
}
Set rs = cnn.Execute(Sql) '
Sheets("SHEET2").Cells.ClearContents '清理保存数据的区域
For i = 0 To rs.Fields.Count - 1
Worksheets("SHEET2").Cells(1, i + 1) = rs.Fields(i).Name
Next
Sheets("SHEET2").Range("a2").CopyFromRecordset rs
rs.Close
cnn.Close
Set rs= Nothing
Set cnn = Nothing
END SUB