Excel精英培训网

 找回密码
 注册
数据透视表40+个常用小技巧,让你一次学会!
查看: 1887|回复: 6

[已解决]请老师帮我优化下这个汇总代码!!!

[复制链接]
发表于 2012-10-22 11:42 | 显示全部楼层 |阅读模式
634.jpg 基础资料数据表.rar (15.37 KB, 下载次数: 9)
excel精英培训的微信平台,每天都会发送excel学习教程和资料。扫一扫明天就可以收到新教程
发表于 2012-10-22 12:18 | 显示全部楼层
  1. Private Sub CommandButton1_Click()
  2.     Dim Nowbook As Workbook
  3.     Dim ShName As Variant
  4.     Dim Arr As Variant
  5.     Dim myNewWorkbook As Integer
  6.     Dim intRow As Integer, t As Single
  7.     Dim ARow As Integer
  8.     Dim sWBName
  9.     '关闭刷屏
  10.     Application.ScreenUpdating = False
  11.     '禁止显示提示和警告消息
  12.     Application.DisplayAlerts = False
  13.    
  14.     myNewWorkbook = Application.SheetsInNewWorkbook
  15.     ShName = Array("汇总数据")
  16.     Arr = Array("数据一", "数据二", "数据三", "数据四", "数据五", "数据六", "数据七", "数据八")
  17.     Application.SheetsInNewWorkbook = 1
  18.     sWBName = [a2]
  19.     Set Nowbook = Workbooks.Add
  20.     With Nowbook
  21.             With .Sheets(1)
  22.     ActiveWindow.DisplayGridlines = False '关闭网格线
  23.                 .Name = ShName(0)
  24.                 .Range("B4").Resize(1, UBound(Arr) + 1) = Arr
  25.             End With
  26.     End With
  27.     Set Nowbook = Nothing
  28.    Application.SheetsInNewWorkbook = myNewWorkbook
  29.    
  30.     t = Timer
  31.     Dim cn As New ADODB.Connection, sql As String
  32.     'intRow = Sheet1.Range("B65536").End(xlUp).Row
  33.     cn.Open "provider=microsoft.jet.oledb.4.0;extended properties=excel 8.0;data source=" & ThisWorkbook.FullName
  34.     sql = "select 数据一,数据二,数据三,数据四,数据五,数据六,数据七,sum(数据八) from [原表$B4:I" & intRow & "]" & _
  35.     "GROUP BY 数据一,数据二,数据三,数据四,数据五,数据六,数据七"
  36.    
  37.     Sheets(1).Range("B5").CopyFromRecordset cn.Execute(sql)
  38.    
  39.     cn.Close
  40.     Set cn = Nothing
  41.    
  42.    'On Error Resume Next
  43.     'With Range("B5").Resize(K, 8) '绘制表格的问题2
  44.     With Range("B5").CurrentRegion
  45.         With Borders
  46.             LineStyle = xlContinuous
  47.             ColorIndex = 12
  48.         End With
  49.     End With
  50.     Debug.Print ThisWorkbook.Path & Application.PathSeparator & sWBName & ".xls"
  51.     ActiveWorkbook.SaveAs Filename:=ThisWorkbook.Path & Application.PathSeparator & sWBName, FileFormat:=xlExcel8
  52.     '如果在03里,fileformat:=xlexcel7
  53.     ActiveWorkbook.Close True
  54. End Sub
复制代码
回复

使用道具 举报

发表于 2012-10-22 12:20 | 显示全部楼层    本楼为最佳答案   
基础资料数据表.rar (15.54 KB, 下载次数: 12)
回复

使用道具 举报

 楼主| 发表于 2012-10-22 12:41 | 显示全部楼层
hwc2ycy 发表于 2012-10-22 12:20

老师,刚下载了您给的附件。

在我的EXCEL2007中运行,无任何反应。

反而删掉了    ActiveWorkbook.Close True这一句运行就正常啦

但添加线框的那一段代码还是没有用。

这里给传一个关于自带画线框的样件,老师就知道明白我说的线框是什么意思啦。

自动画表格.rar (10.25 KB, 下载次数: 9)
回复

使用道具 举报

发表于 2012-10-22 12:57 | 显示全部楼层
画线框的我确实没看。
改一下FileFormat:=xlExcel7
我是在10上试的,不好意思。
回复

使用道具 举报

 楼主| 发表于 2012-10-22 13:04 | 显示全部楼层
hwc2ycy 发表于 2012-10-22 12:57
画线框的我确实没看。
改一下FileFormat:=xlExcel7
我是在10上试的,不好意思。

哈哈···改成这样就可以啦{:11:}

    With Sheets(1).Range("B5").CurrentRegion
        With .Borders
                .LineStyle = xlContinuous
                .ColorIndex = 12
        End With
    End With   
谢谢老师帮忙~~!

老师我表格中的代码,能否帮我精简下?     {:34:}  
回复

使用道具 举报

发表于 2012-10-22 13:26 | 显示全部楼层
呵呵。
你都会用SQL了,我还不会了,
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|Archiver|Excel精英培训 ( 豫ICP备11015029号 )

GMT+8, 2024-4-19 12:23 , Processed in 0.939889 second(s), 13 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表