EXCEL(VBA)~SQL 经典写法范本汇集(三)
时 间:2012-04-09 08:58:12
作 者:欢乐小爪 ID:20149 城市:杭州
摘 要:EXCEL(VBA)~SQL 经典写法范本汇集(三)
正 文:
回答 27019170网友的问题
1.提取本文件夹内除本工作簿以外的工作簿的一个人的姓名,货号,序号等
| 请输入姓名 | 何芝全 | ||||||
| 姓名 | 货号 | 序号 | 流程名称 | 单价 | 数量 | 金额 | 组别 |
| 何芝全 | 852152 | 21 | 上鞋舌 | 50 | 0 | A组 | |
| 何芝全 | 852361 | 12 | 车大面假线(2) | 180 | 0 | A组 | |
| 何芝全 | 852361 | 21 | 压扣 | 0.01 | 755 | 7.55 | A组 |
| 何芝全 | 852361 | 32 | 车网脚及修建 | 0.02 | 740 | 14.8 | A组 |
| 何芝全 | 852152 | 21 | 上鞋舌 | 50 | 0 | B组 | |
| 何芝全 | 852361 | 12 | 车大面假线(2) | 180 | 0 | B组 | |
| 何芝全 | 852361 | 21 | 压扣 | 0.01 | 755 | 7.55 | B组 |
| 何芝全 | 852361 | 32 | 车网脚及修建 | 0.02 | 740 | 14.8 | B组 |
| 何芝全 | 852152 | 21 | 上鞋舌 | 50 | 0 | C组 | |
| 何芝全 | 852361 | 12 | 车大面假线(2) | 180 | 0 | C组 | |
代码如下:
Sub 提取工资()
Dim adoConn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim FilePath As String
Dim sql As String
Dim FileName As String
Dim MaxRow As Long
On Error GoTo 10000
Sheets("按扭界面").Select
MaxRow = Range("a65536").End(xlUp).Row
Range("a6:h" & MaxRow + 1).ClearContents
FilePath = ThisWorkbook.Path
FileName = Dir(FilePath & "\*.xls")
Do While FileName <> "" And FileName <> ThisWorkbook.Name
Set adoConn = New ADODB.Connection
adoConn.Open "Provider=Microsoft.Jet.OleDb.4.0;Extended Properties=Excel 8.0;Data Source=" & FilePath & "\" & FileName
sql = "select 姓名,货号,序号,流程名称,单价,数量,金额,'" & Mid(FileName, 1, InStr(1, FileName, ".") - 1) & "' as 组别 " & _
" from [计算结果$] " & _
" where 姓名= '" & [b1] & "'" ' " where 姓名='" & EmployeeName & "'"
Set rs = New ADODB.Recordset
rs.Open sql, adoConn, adOpenKeyset, adLockOptimistic
Range("a" & MaxRow + 1).CopyFromRecordset rs
MaxRow = MaxRow + rs.RecordCount + 1
FileName = Dir
Loop
Set rs = Nothing
Set adoConn = Nothing
Range("a6:h" & MaxRow).HorizontalAlignment = xlCenter
Exit Sub
10000: MsgBox Error()
End Sub
2.查询本文件夹内除本工作簿以外的工作簿的姓名重名情况
先不重复---》后重复
关于不重复用sql ;重复用循环
Sub 查询重复姓名()
Dim adoConn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim FilePath As String
Dim sql As String
Dim FileName As String
Dim I&
Dim MaxRow As Long
On Error GoTo 10000
Sheets("查验重姓名").Select
MaxRow = Range("a65536").End(xlUp).Row + 1
Range("a2:b" & MaxRow).ClearContents
FilePath = ThisWorkbook.Path
FileName = Dir(FilePath & "\*.xls")
Do While FileName <> "" And FileName <> ThisWorkbook.Name
Set adoConn = New ADODB.Connection
adoConn.Open "Provider=Microsoft.Jet.OleDb.4.0;Extended Properties=Excel 8.0;Data Source=" & FilePath & "\" & FileName
sql = "select distinct 姓名,'" & Mid(FileName, 1, InStr(1, FileName, ".") - 1) & "' as 组别 " & _
"from [计算结果$] where 姓名 is not null"
Set rs = New ADODB.Recordset
rs.Open sql, adoConn, adOpenKeyset, adLockOptimistic
Range("a" & MaxRow).CopyFromRecordset rs
MaxRow = MaxRow + rs.RecordCount
FileName = Dir
Loop
Set rs = Nothing
Set adoConn = Nothing
Range("A2:B" & MaxRow).Sort Key1:=Range("A2"), Order1:=xlAscending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False
MaxRow = Range("a65536").End(xlUp).Row
For I = MaxRow To 2 Step -1
If Cells(I, 1) = Cells(I - 1, 1) Then
Cells(I - 1, 2) = Cells(I - 1, 2) & "/" & Cells(I, 2)
Rows(I).EntireRow.Delete
ElseIf Len(Cells(I, 2)) < 5 Then
Rows(I).EntireRow.Delete
End If
Next
MsgBox "完成", 1 + 64, "i love you"
Exit Sub
10000: MsgBox Error()
End Sub
Sub 清空()
Sheets("按扭界面").Range("a6:h65536").ClearContents
End Sub
Access软件网官方交流QQ群 (群号:54525238) Access源码网店
常见问答:
技术分类:
源码示例
- 【源码QQ群号19834647...(12.17)
- Access怎么按每个年度里面...(07.03)
- Access自定义消息盒Msg...(04.28)
- Access怎么按每个月份里面...(03.09)
- Access怎么按季度来统计人...(02.26)
- Access怎么按年龄段来统计...(01.26)
- 【Access高效办公】上一年...(12.29)
- 用Access连续窗体制作的树...(11.03)
- 【Access高效办公】上一年...(10.30)
- Access制作的RGB转CM...(09.22)
学习心得
最新文章
- 运行时错误3052文件共享锁定数溢...(07.10)
- Access怎么按每个年度里面的周...(07.03)
- Access数据库备份不求人——用...(06.29)
- 如何更改快速开发平台上的窗体按钮(06.20)
- 用Excel模仿Access开发《...(06.17)
- Access快速开发平台导航箭头改...(06.15)
- Access2024官方下载,Ac...(06.11)
- Access快速开发平台--后台A...(06.03)
- 将bit字段的Null值更新为Fa...(06.01)
- 用VBA批量更改SQL SERVE...(05.30)


.gif)
