|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
用我的方法已经可以实现需要的功能,但是不方便后期的维护,且代码太累赘。希望大神能帮简化下,且方便维护1、要对表cx1的数据进行统计,就是统计一个段里面有多少个
cx1 如图
2、统计结果写入fsd表,如图
3、统计具体要按统计的最高线,最低线,及间隔来进行统计
如图:
我写的代码下:
Private Sub Command0_Click()
If Me.zg <> "" And Me.xx <> "" And Me.bc <> "" Then
SQL = "DELETE fsd.fsd FROM fsd;"
CurrentDb.Execute (SQL)
MsgBox "清除成功!开始统计"
Dim rs As Recordset
Dim k1 As Long
Dim k2 As Long
Dim n As Long
n = Me.bc
zgx = Me.zg
k2 = Me.zg
k1 = k2 - n
Set rs = CurrentDb.OpenRecordset("fsd")
rs.AddNew
For i = Me.zg To Me.xx + n Step -n
n01lj = DCount("*", "cx1", "hczf>=" & k1 & " and hczf<" & k2 & " And bm = 'bm1'")
n22lj = DCount("*", "cx1", "hczf>=" & k1 & " and hczf<" & k2 & " And bm = 'bm2'")
n23lj = DCount("*", "cx1", "hczf>=" & k1 & " and hczf<" & k2 & " And bm = 'bm3'")
n24lj = DCount("*", "cx1", "hczf>=" & k1 & " and hczf<" & k2 & " And bm = 'bm4'")
n25lj = DCount("*", "cx1", "hczf>=" & k1 & " and hczf<" & k2 & " And bm = 'bm5'")
n26lj = DCount("*", "cx1", "hczf>=" & k1 & " and hczf<" & k2 & " And bm = 'bm6'")
n27lj = DCount("*", "cx1", "hczf>=" & k1 & " and hczf<" & k2 & " And bm = 'bm7'")
n28lj = DCount("*", "cx1", "hczf>=" & k1 & " and hczf<" & k2 & " And bm = 'bm8'")
n29lj = DCount("*", "cx1", "hczf>=" & k1 & " and hczf<" & k2 & " And bm = 'bm9'")
rs.AddNew
rs![bm1] = n01lj
rs![bm2] = n22lj
rs![bm3] = n23lj
rs![bm4] = n24lj
rs![bm5] = n25lj
rs![bm6] = n26lj
rs![bm7] = n27lj
rs![bm8] = n28lj
rs![bm9] = n29lj
rs![fsd] = k1 & "—" & k2 - 1
rs.Update
k2 = k1
k1 = k1 - n
Next
MsgBox "统计完成"
rs.Close
Else
MsgBox "请填写完整"
End If
End Sub
4、功能已经能全部实现,但是我想通过循环引用表bmdm里面的部门代码“bm”来简化这不部分代码,
希望bmdm这个表中的bm有增加的时候,只需要在fsd表中增加相应字段就可以完成
n01lj = DCount("*", "cx1", "hczf>=" & k1 & " and hczf<" & k2 & " And bm = 'bm1'")
n22lj = DCount("*", "cx1", "hczf>=" & k1 & " and hczf<" & k2 & " And bm = 'bm2'")
n23lj = DCount("*", "cx1", "hczf>=" & k1 & " and hczf<" & k2 & " And bm = 'bm3'")
n24lj = DCount("*", "cx1", "hczf>=" & k1 & " and hczf<" & k2 & " And bm = 'bm4'")
n25lj = DCount("*", "cx1", "hczf>=" & k1 & " and hczf<" & k2 & " And bm = 'bm5'")
n26lj = DCount("*", "cx1", "hczf>=" & k1 & " and hczf<" & k2 & " And bm = 'bm6'")
n27lj = DCount("*", "cx1", "hczf>=" & k1 & " and hczf<" & k2 & " And bm = 'bm7'")
n28lj = DCount("*", "cx1", "hczf>=" & k1 & " and hczf<" & k2 & " And bm = 'bm8'")
n29lj = DCount("*", "cx1", "hczf>=" & k1 & " and hczf<" & k2 & " And bm = 'bm9'")
rs.AddNew
rs![bm1] = n01lj
rs![bm2] = n22lj
rs![bm3] = n23lj
rs![bm4] = n24lj
rs![bm5] = n25lj
rs![bm6] = n26lj
rs![bm7] = n27lj
rs![bm8] = n28lj
rs![bm9] = n29lj
rs![fsd] = k1 & "—" & k2 - 1
rs.Update
5、附件在这,希望那个大神帮解决下,拜求
分段统计.rar
(37.58 KB, 下载次数: 4)
|
|