Excel精英培训网

 找回密码
 注册
数据透视表40+个常用小技巧,让你一次学会!
12
返回列表 发新帖
楼主: qinhuan66

[已解决]黄色区域输入月份能自动从Access数据库提取缴款人

[复制链接]
 楼主| 发表于 2013-4-2 18:47 | 显示全部楼层
hwc2ycy 发表于 2013-4-2 18:00

楼主经测试,发现一个问题提取时不会替换已生成的数据。每次提取都重复提取数据。谢谢
如图:
2013-04-02_184450.gif
回复

使用道具 举报

发表于 2013-4-2 18:53 | 显示全部楼层
回复

使用道具 举报

 楼主| 发表于 2013-4-2 18:55 | 显示全部楼层
hwc2ycy 发表于 2013-4-2 18:53
恩,不会替换已有的。都是追加。

你好老师这个能处理吗?谢谢
回复

使用道具 举报

 楼主| 发表于 2013-4-2 21:21 | 显示全部楼层
添加一句Range("B5:E54,G5:J54").ClearContents可以了谢谢
回复

使用道具 举报

 楼主| 发表于 2013-5-18 08:02 | 显示全部楼层
hwc2ycy 发表于 2013-4-2 18:00

老师您好!现在碰到一个问题。您写的这条代码我想查询整年的数据,(注:现在是输入年月份查询月的数据),如何修改呢?谢谢

  • Sub 查询Access()
  •     Dim AccessFile As String, Database As String
  •     Dim arr(), i&, arrTemp
  •     Dim AdoConn As Object, AdoRst As Object
  •     Dim StrConn$, strSql$
  •     '检查是否输入年月
  •     If Not (Len([c1]) = 0 Or IsDate([c1])) Then
  •         MsgBox "请在C1单元格内输入要查询的年月"
  •         Exit Sub
  •     End If
  •     On Error GoTo Errcheck
  •     AccessFile = ThisWorkbook.Path & "\收款收据.mdb"
  •     Database = "收款信息"
  •     If Dir(AccessFile) = "" Then
  •         MsgBox "ACCESS数据文件不存在"
  •         Exit Sub
  •     End If
  •     StrConn = "Provider= Microsoft.Jet.OLEDB.4.0;" & _
  •               "Data Source=" & AccessFile & ";"""
  •     Set AdoConn = CreateObject("ADODB.Connection")
  •     With AdoConn
  •         .CursorLocation = 3    '游标类型
  •         .CommandTimeout = 5    '超时
  •         .connectionTimeout = 5  '超时
  •         .Open StrConn       '打开
  •     End With
  •     '检测ADO状态
  •     If AdoConn.State <> 1 Then MsgBox "数据库连接失败", vbCritical + vbOKOnly: Exit Sub
  •     '查询的年,月
  •     Dim dateYear As Integer, dateMonth As Byte
  •     dateYear = Year([c1])
  •     dateMonth = Month([c1])
  •     strSql = "select 缴款人,成年人正常缴费,未成年人正常缴费,金额 from " & Database & " where year(委托日期)=" & dateYear & " and month(委托日期)=" & dateMonth
  •     Set AdoRst = AdoConn.Execute(strSql)
  •     If AdoRst.RecordCount = 0 Then
  •         MsgBox "无合乎条件的数据"
  •         Exit Sub
  •     Else
  •         Select Case AdoRst.RecordCount
  •             Case 1:
  •                 arrTemp = WorksheetFunction.Transpose(AdoRst.GetRows)
  •                 ReDim arr(1 To 1, 1 To UBound(arrTemp))
  •                 For i = LBound(arrTemp) To UBound(arrTemp)
  •                     arr(1, i) = arrTemp(i)
  •                 Next
  •                 arrTemp = arr
  •             Case Else:
  •                 arrTemp = WorksheetFunction.Transpose(AdoRst.GetRows)
  •         End Select
  •     End If
  •     AdoConn.Close
  •     Set AdoConn = Nothing
  •     '关刷屏
  •     Application.ScreenUpdating = False
  •     '列,行坐标
  •     Dim lCol&, lPos&, arr2
  •     lCol = 2
  •     For i = LBound(arrTemp) To UBound(arrTemp)
  •         lPos = Cells(Rows.Count, lCol).End(xlUp).Row + 1
  •         '前4行是标题行,因为有合并格,强制从5行开始
  •         If lPos < 5 Then lPos = 5
  •         '判断是否超过54行
  •         If lPos > 54 Then
  •             Do
  •                 lCol = lCol + 5
  •                 lPos = Cells(Rows.Count, lCol).End(xlUp).Row + 1
  •                 If lPos < 5 Then lPos = 5
  •             Loop Until lPos < 55
  •         End If
  •         '按行写入,考虑到要写满要换列的情况
  •         arr2 = WorksheetFunction.Index(arrTemp, i, 0)
  •         Cells(lPos, lCol).Resize(, UBound(arr2)) = arr2
  •     Next
  •     '开刷屏,退出
  •     Application.ScreenUpdating = True
  •     Exit Sub
  • Errcheck:
  •     MsgBox Err.Number & vbNewLine & _
  •            Err.Description
  • End Sub

回复

使用道具 举报

发表于 2013-7-18 16:56 | 显示全部楼层

43.    strSql = "select 缴款人,成年人正常缴费,未成年人正常缴费,金额 from " & Database & " where year(委托日期)=" & dateYear & " and month(委托日期)=" & dateMonth
改成
43.    strSql = "select 缴款人,成年人正常缴费,未成年人正常缴费,金额 from " & Database & " where year(委托日期)=" & dateYear

评分

参与人数 1 +3 收起 理由
qinhuan66 + 3 很给力!

查看全部评分

回复

使用道具 举报

发表于 2014-1-27 22:25 | 显示全部楼层
好厉害!
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-29 07:31 , Processed in 0.397879 second(s), 14 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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