如何对Excel 宏 按照数据库的方式统计分析
2010-10-17 20:41
471 查看
Dim IsSourceWS As Boolean
Dim isTable1 As Boolean
Dim isTable2 As Boolean
Dim isTable3 As Boolean
IsSourceWS = False
isTable1 = False
isTable2 = False
isTable3 = False
sStartPath = ActiveWorkbook.Path & "/" & ActiveWorkbook.Name
If Worksheets.Count = 0 Then
Exit Sub
Else
For Each EacheWorksheet In Worksheets
If EacheWorksheet.Name = "原表" Then
IsSourceWS = True
Else
If EacheWorksheet.Name = "表1" Then
isTable1 = True
Else
If EacheWorksheet.Name = "表2" Then
isTable2 = True
Else
If EacheWorksheet.Name = "表3" Then
isTable3 = True
End If
End If
End If
End If
Next EacheWorksheet
End If
'*******************************没有原表则提示****************************************************
If IsSourceWS = False Then
MsgBox "没有发现命名为'原表'的Sheet页签,请核对"
Exit Sub
End If
'*********************************对Table1进行判断************************************************
If isTable1 = True Then
Application.DisplayAlerts = False
Worksheets("表1").Delete
Set AddWorksheet = Nothing
Set AddWorksheet = Worksheets.Add
AddWorksheet.Name = "表1"
Worksheets("表1").Move after:=Sheets(Sheets.Count)
Else
Set AddWorksheet = Nothing
Set AddWorksheet = Worksheets.Add
AddWorksheet.Name = "表1"
Worksheets("表1").Move after:=Sheets(Sheets.Count)
End If
'*********************************对Table2进行判断************************************************
If isTable2 = True Then
Application.DisplayAlerts = False
Worksheets("表2").Delete
Set AddWorksheet = Nothing
Set AddWorksheet = Worksheets.Add
AddWorksheet.Name = "表2"
Worksheets("表2").Move after:=Sheets(Sheets.Count)
Else
Set AddWorksheet = Nothing
Set AddWorksheet = Worksheets.Add
AddWorksheet.Name = "表2"
Worksheets("表2").Move after:=Sheets(Sheets.Count)
End If
'*********************************对Table3进行判断*****************************************************
If isTable3 = True Then
Application.DisplayAlerts = False
Worksheets("表3").Delete
Set AddWorksheet = Nothing
Set AddWorksheet = Worksheets.Add
AddWorksheet.Name = "表3"
Worksheets("表3").Move after:=Sheets(Sheets.Count)
Else
Set AddWorksheet = Nothing
Set AddWorksheet = Worksheets.Add
AddWorksheet.Name = "表3"
Worksheets("表3").Move after:=Sheets(Sheets.Count)
End If
'****************************************************连接数据库驱动************************************
lcconectionstring = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & sStartPath & ";" & _
"Extended Properties=Excel 8.0;"
lccommandtext = "SQL语句"
Set loadodbconection = CreateObject("adodb.connection")
Set loadodbrecordset = CreateObject("adodb.recordset")
loadodbconection.Open lcconectionstring
loadodbrecordset.Open lccommandtext, loadodbconection, 3, 1, 1
Dim r As Integer
Dim f As Integer
r = 1
Application.DisplayAlerts = False
Application.StatusBar = "正在执行数据统计分析中,请稍后......."
'*****************************************************************************************
'**********************************提高速度,采用按行写入**********************************
'*****************************************************************************************
ReDim RowInfo(5) As String
For f = 0 To loadodbrecordset.Fields.Count - 1
RowInfo(f) = loadodbrecordset.Fields(f).Name
Next
RowInfo(f) = "备注"
Sheets("表2").Select
Sheets("表2").Range(Cells(r, 1), Cells(r, f + 1)).Value2 = RowInfo
ReDim RowInfo(5) As String
If loadodbrecordset.PageCount = 0 Then
Application.StatusBar = ""
Exit Sub
End If
While Not loadodbrecordset.EOF
r = r + 1
For f = 0 To loadodbrecordset.Fields.Count - 1
RowInfo(f) = loadodbrecordset.Fields(f).Value
Next
Sheets("表2").Range(Cells(r, 1), Cells(r, f + 1)).Value2 = RowInfo
Sheets("表2").Cells(r, f + 1) = "备注一下"
loadodbrecordset.MoveNext
Wend
Sheets("表2").Cells.EntireColumn.AutoFit
'*****************************************************************************
Dim isTable1 As Boolean
Dim isTable2 As Boolean
Dim isTable3 As Boolean
IsSourceWS = False
isTable1 = False
isTable2 = False
isTable3 = False
sStartPath = ActiveWorkbook.Path & "/" & ActiveWorkbook.Name
If Worksheets.Count = 0 Then
Exit Sub
Else
For Each EacheWorksheet In Worksheets
If EacheWorksheet.Name = "原表" Then
IsSourceWS = True
Else
If EacheWorksheet.Name = "表1" Then
isTable1 = True
Else
If EacheWorksheet.Name = "表2" Then
isTable2 = True
Else
If EacheWorksheet.Name = "表3" Then
isTable3 = True
End If
End If
End If
End If
Next EacheWorksheet
End If
'*******************************没有原表则提示****************************************************
If IsSourceWS = False Then
MsgBox "没有发现命名为'原表'的Sheet页签,请核对"
Exit Sub
End If
'*********************************对Table1进行判断************************************************
If isTable1 = True Then
Application.DisplayAlerts = False
Worksheets("表1").Delete
Set AddWorksheet = Nothing
Set AddWorksheet = Worksheets.Add
AddWorksheet.Name = "表1"
Worksheets("表1").Move after:=Sheets(Sheets.Count)
Else
Set AddWorksheet = Nothing
Set AddWorksheet = Worksheets.Add
AddWorksheet.Name = "表1"
Worksheets("表1").Move after:=Sheets(Sheets.Count)
End If
'*********************************对Table2进行判断************************************************
If isTable2 = True Then
Application.DisplayAlerts = False
Worksheets("表2").Delete
Set AddWorksheet = Nothing
Set AddWorksheet = Worksheets.Add
AddWorksheet.Name = "表2"
Worksheets("表2").Move after:=Sheets(Sheets.Count)
Else
Set AddWorksheet = Nothing
Set AddWorksheet = Worksheets.Add
AddWorksheet.Name = "表2"
Worksheets("表2").Move after:=Sheets(Sheets.Count)
End If
'*********************************对Table3进行判断*****************************************************
If isTable3 = True Then
Application.DisplayAlerts = False
Worksheets("表3").Delete
Set AddWorksheet = Nothing
Set AddWorksheet = Worksheets.Add
AddWorksheet.Name = "表3"
Worksheets("表3").Move after:=Sheets(Sheets.Count)
Else
Set AddWorksheet = Nothing
Set AddWorksheet = Worksheets.Add
AddWorksheet.Name = "表3"
Worksheets("表3").Move after:=Sheets(Sheets.Count)
End If
'****************************************************连接数据库驱动************************************
lcconectionstring = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & sStartPath & ";" & _
"Extended Properties=Excel 8.0;"
lccommandtext = "SQL语句"
Set loadodbconection = CreateObject("adodb.connection")
Set loadodbrecordset = CreateObject("adodb.recordset")
loadodbconection.Open lcconectionstring
loadodbrecordset.Open lccommandtext, loadodbconection, 3, 1, 1
Dim r As Integer
Dim f As Integer
r = 1
Application.DisplayAlerts = False
Application.StatusBar = "正在执行数据统计分析中,请稍后......."
'*****************************************************************************************
'**********************************提高速度,采用按行写入**********************************
'*****************************************************************************************
ReDim RowInfo(5) As String
For f = 0 To loadodbrecordset.Fields.Count - 1
RowInfo(f) = loadodbrecordset.Fields(f).Name
Next
RowInfo(f) = "备注"
Sheets("表2").Select
Sheets("表2").Range(Cells(r, 1), Cells(r, f + 1)).Value2 = RowInfo
ReDim RowInfo(5) As String
If loadodbrecordset.PageCount = 0 Then
Application.StatusBar = ""
Exit Sub
End If
While Not loadodbrecordset.EOF
r = r + 1
For f = 0 To loadodbrecordset.Fields.Count - 1
RowInfo(f) = loadodbrecordset.Fields(f).Value
Next
Sheets("表2").Range(Cells(r, 1), Cells(r, f + 1)).Value2 = RowInfo
Sheets("表2").Cells(r, f + 1) = "备注一下"
loadodbrecordset.MoveNext
Wend
Sheets("表2").Cells.EntireColumn.AutoFit
'*****************************************************************************
相关文章推荐
- Excel在统计分析中的应用—第三章—数据库统计函数与数据透视表-Part1-(数据查询与筛选、分类汇总)
- Excel在统计分析中的应用—第四章—数据库统计函数与数据透视表-Part2-(数据库统计函数)
- 如何把股票软件的数据导入到数据库(access,sqlserver,oracle)然后自行统计分析?
- Excel在统计分析中的应用—第四章—数据库统计函数与数据透视表-Part3-(数据透视表、图)
- Excel在统计分析中的应用—第十一章—相关分析-简单线性相关-散点图法
- Excel在统计分析中的应用—第六章—抽样分布-大样本的抽样分布(差的抽样分布)
- Excel在统计分析中的应用—第七章—参数估计-总体均值的估计(总体方差已知下的估计)
- Excel在统计分析中的应用—第七章—参数估计-总体均值的估计(总体方差未知下的估计)
- 教你如何把.NET数据库导出到EXCEL(图文)
- 如何分析发生在过去的数据库性能问题
- 学以致用——微博文章内容统计分析之一(Excel+GraphLab)
- Excel在统计分析中的应用—第十二章—回归分析与预测-一元非线性回归分析与预测
- 如何统计分析CSDN博客流量
- 从注册流程 分析如何安全退出多个Activity 多种方式(附DEMO)
- Excel在统计分析中的应用—第十一章—相关分析-简单线性相关-相关系数的检验
- Oracle如何实现创建数据库、备份数据库及数据导出导入的一条龙操作-------sql方式
- 如何通过dba_hist_active_sess_history分析历史数据库性能问题
- Jxls导出excel的若干方式总结(十五)-- 简单分析与总结
- 润乾报表报表组如何使用openxml方式导出excel
- Excel在统计分析中的应用—第十一章—相关分析-简单线性相关-相关系数为零的检验(大样本)