如何对Excel 宏 按照数据库的方式统计分析

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

'*****************************************************************************

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值