|
- '*********************************
- '******* 北极狐工作室出品 ******
- '******* QQ:14885553 ******
- '*********************************
- Sub opiona()
- Application.ScreenUpdating = False '//关闭屏幕刷新
- Application.DisplayAlerts = False '//关闭系统提示
- t = Timer '//开始时间
- Set sh1 = Sheets("Sheet1")
- For Each sh In ThisWorkbook.Sheets
- If sh.Name <> sh1.Name Then sh.Delete
- Next sh
- Str_coon = "Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties='Excel 12.0;HDR=yes';data source=" & ThisWorkbook.FullName
- StrSQL = "SELECT DISTINCT 人员姓名 FROM [" & sh1.Name & "$A2:M65536] where len(人员姓名)>0"
- CRR = GET_SQLCoon(StrSQL, Str_coon, False)
- For I = 0 To UBound(CRR, 1)
- ThisWorkbook.Worksheets.Add(AFTER:=Worksheets(Worksheets.Count)).Name = CRR(I, 0) '//建立新表
- Set NEWSH = Sheets(CRR(I, 0))
- StrSQL1 = "SELECT * FROM [" & sh1.Name & "$A2:M65536] where 人员姓名='" & CRR(I, 0) & "'"
- StrSQL1 = StrSQL1 & " ORDER BY 日报单号,派工单号,订单号"
- ARR = GET_SQLCoon(StrSQL1, Str_coon, True)
- NEWSH.Range("A1").Resize(UBound(ARR, 1) + 1, UBound(ARR, 2) + 1) = ARR
- Next I
- Application.ScreenUpdating = True '//恢复屏幕刷新
- Application.DisplayAlerts = True '//恢复系统提示
- MsgBox "用时:" & Format(Timer - t, "#0.0000") & " 秒", , "温馨提示!!" '//提示所用时间
- End Sub
- '*****************************************************************************************
- '函数名: GET_SQLCoon
- '函数功能: 获得指定SQL的查询结果,自定义连接字符串,可以连接各种数据库
- '返回值: 返回一个二维数组
- '参数1: StrSQL 字符类型 SQL查询语句
- '参数2: Str_coon 字符类型 数据库连接语句
- '参数3: Biaoti 可参数选 是否输出标题,默认带有标题
- '使用方法: Arr = GET_SQLCoon(StrSQL,Str_coon,true)
- ' Arr(0,1) '//数组第一行为标题行,从i=1 开始是数据
- ' Sh2.Range("A2").Resize(UBound(ARR, 1) + 1, UBound(ARR, 2) + 1) = ARR
- '*****************************************************************************************
- Public Function GET_SQLCoon(ByVal StrSQL As String, ByVal Str_coon As String, Optional Biaoti As Boolean = True) As Variant()
- On Error Resume Next ' 改变错误处理的方式。
- Dim Cn, RS
- Err.Clear
- Set Cn = CreateObject("Adodb.Connection") '//新建一个ADO连接
- Set RS = CreateObject("adodb.recordset")
- Cn.Open Str_coon
- RS.Open StrSQL, Cn, 1, 3
- ' If RS.RecordCount > 0 Then '//如果找到数据
- If Biaoti = True Then
- ReDim ARR(0 To RS.RecordCount, 0 To RS.Fields.Count - 1)
- For A = 0 To RS.Fields.Count - 1 '//导入标题
- ARR(0, A) = RS.Fields(A).Name
- Next
- For I = 0 To RS.RecordCount - 1 '//导入数据
- For A = 0 To RS.Fields.Count - 1
- ARR(I + 1, A) = RS.Fields(A).Value
- Next A
- RS.MoveNext
- Next
- Else
- ReDim ARR(0 To RS.RecordCount - 1, 0 To RS.Fields.Count - 1)
- For I = 0 To RS.RecordCount - 1 '//导入数据
- For A = 0 To RS.Fields.Count - 1
- ARR(I, A) = RS.Fields(A).Value
- Next A
- RS.MoveNext
- Next
- End If
- ' Else '//如果没有找到数据
- ' ReDim Arr(1, 1)
- ' Arr(0, 0) = ""
- ' End If
- GET_SQLCoon = ARR
- Cn.Close '//关闭ADO连接
- Set RS = Nothing
- Set Cn = Nothing '//释放内存
- End Function
- '*****************************************************************************************
复制代码 |
|