Excel精英培训网

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

[已解决]求VBA多条件筛选求和.感谢帮助

[复制链接]
发表于 2021-12-11 14:49 | 显示全部楼层
本帖最后由 excel用户1116 于 2021-12-11 14:56 编辑
军3008 发表于 2021-12-11 14:43
感谢感谢!这个码可以用的,只是没有上面的汇总合计 ...


求助VBA多条件多列筛选求和.zip (29.57 KB, 下载次数: 34)

评分

参与人数 1学分 +2 收起 理由
军3008 + 2 我和小伙伴都惊呆了

查看全部评分

excel精英培训的微信平台,每天都会发送excel学习教程和资料。扫一扫明天就可以收到新教程
回复

使用道具 举报

发表于 2021-12-11 14:52 | 显示全部楼层
军3008 发表于 2021-12-11 14:43
感谢感谢!这个码可以用的,只是没有上面的汇总合计 ...

看来你是多地求助啊!!EH论坛上也看到了你的求助!!
回复

使用道具 举报

 楼主| 发表于 2021-12-11 15:09 | 显示全部楼层

完美!!!!我想在这里面加入框线等,以下代码怎么加进去:            .Borders.LineStyle = xlContinuous          '添加边框线            .BorderAround xlContinuous, xlMedium       '外边框加粗
            .Borders.ColorIndex = 4                    '内边框为绿色
            .Characters.Font.Size = 12                 '字体12号
            .Font.ColorIndex = 5                       '字体蓝色
            .BorderAround xlContinuous, xlMedium       '外边框加粗
            .Borders(7).ColorIndex = 3                 '四周边框为红色
            .Borders(8).ColorIndex = 3                 '四周边框为红色
            .Borders(9).ColorIndex = 3                 '四周边框为红色
            .Borders(10).ColorIndex = 3                '四周边框为红色


回复

使用道具 举报

 楼主| 发表于 2021-12-11 15:12 | 显示全部楼层
excel用户1116 发表于 2021-12-11 14:52
看来你是多地求助啊!!EH论坛上也看到了你的求助!!

是的,那上面求了,只是没弄成我想要的。所以,就来这里了
回复

使用道具 举报

发表于 2021-12-11 15:18 | 显示全部楼层    本楼为最佳答案   
军3008 发表于 2021-12-11 15:09
完美!!!!我想在这里面加入框线等,以下代码怎么加进去:            .Borders.LineStyle = xlContinu ...

求助VBA多条件多列筛选求和.zip (30.46 KB, 下载次数: 11)
回复

使用道具 举报

 楼主| 发表于 2021-12-11 15:51 | 显示全部楼层

太完美了,都不知道怎么感谢你了

帮我看一下下面的代码,我不知道哪里错了,那个月日怎么都转换不来呢,到我表里就成如图片上的那种显示了
  1. Private Sub cmd查询_Click()
  2.     Application.ScreenUpdating = False
  3.     Dim endrow As Long
  4.     Dim i As Long, j As Long, k As Long
  5.     Dim dt1 As Date, dt2 As Date
  6.     Dim qc, balance
  7.     Dim r As Long, rr As Long
  8.     Dim ljj, ljd, ljc
  9.     Dim jf, df, yf
  10.     If TextBox1.Text = "" Then
  11.         MsgBox "请选择项目"
  12.     Else
  13.         Range("A5:G" & Rows.Count).ClearContents
  14.         Range("A5:G" & Rows.Count).Borders.LineStyle = xlNone
  15.         Range("A5:G" & Rows.Count).Interior.ColorIndex = xlNone
  16.         Sheet13.Range("A:E").ClearContents
  17.         dt1 = CDate(Me.txt开始日)
  18.         dt2 = CDate(Me.txt终了日)
  19.         Range("C2") = Me.TextBox1.Text
  20.         Range("D2") = Format(dt1, "yyyy/m/d") & "~" & Format(dt2, "yyyy/m/d")
  21.         Sheet13.Range("A1:E1") = Array("日期", "往来单位", "摘要", "收入金额", "支出金额")
  22.         '数据
  23.         '****************************************************************************
  24.         With Sheet19
  25.             endrow = .Cells(.Rows.Count, "A").End(xlUp).Row
  26.             For i = 2 To endrow
  27.                 If .Cells(i, "A") = Me.TextBox1.Text Then
  28.                     qc = qc + .Cells(i, "C")
  29.                     Exit For
  30.                 End If
  31.             Next
  32.         End With
  33.         '****************************************************************************
  34.         r = 1
  35.         With Sheet2
  36.             endrow = .Cells(.Rows.Count, "A").End(xlUp).Row
  37.             For i = 2 To endrow
  38.                 If .Cells(i, "C") = Me.TextBox1.Text Then
  39.                     If .Cells(i, "A") < dt1 Then
  40.                         qc = qc + .Cells(i, "I") - .Cells(i, "J")
  41.                     ElseIf .Cells(i, "A") >= dt1 And .Cells(i, "A") <= dt2 Then
  42.                         r = r + 1
  43.                         Sheet13.Cells(r, "A") = .Cells(i, "A") '日期
  44.                         Sheet13.Cells(r, "B") = .Cells(i, "C") '往来单位
  45.                         Sheet13.Cells(r, "C") = .Cells(i, "E") '摘要
  46.                         Sheet13.Cells(r, "D") = .Cells(i, "I") '收入金额
  47.                         Sheet13.Cells(r, "E") = .Cells(i, "J") '支出金额
  48.                     End If
  49.                 End If
  50.             Next
  51.         End With
  52.         '****************************************************************************
  53.         Sheet13.Range("A:E").Sort key1:=Sheet13.Range("A1"), Order1:=xlAscending, Header:=xlGuess, _
  54.             OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, SortMethod _
  55.             :=xlPinYin, DataOption1:=xlSortNormal
  56.         Range("C5") = "上期结转"
  57.         Range("G5") = qc
  58.         balance = Application.Round(qc, 2)
  59.         rr = 5
  60.         If r > 1 Then
  61.             For i = 2 To r
  62.                 rr = rr + 1
  63.                 Cells(rr, "A") = Month(Sheet13.Cells(i, "A")) '月
  64.                 Cells(rr, "B") = Day(Sheet13.Cells(i, "A")) '日
  65.                 Cells(rr, "C") = Sheet13.Cells(i, "B") '往来单位
  66.                 Cells(rr, "D") = Sheet13.Cells(i, "C") '摘要
  67.                 Cells(rr, "E") = Sheet13.Cells(i, "D") '收入金额
  68.                 Cells(rr, "F") = Sheet13.Cells(i, "E") '支出金额
  69.                 balance = Application.Round(balance + Cells(rr, "E") - Cells(rr, "F"), 2)
  70.                 Cells(rr, "G") = balance
  71.                 ljj = ljj + Cells(rr, "E")
  72.                 ljd = ljd + Cells(rr, "F")
  73.                 jf = jf + Cells(rr, "E")
  74.                 df = df + Cells(rr, "F")
  75.                 If Year(Sheet13.Cells(i, "A")) <> Year(Sheet13.Cells(i + 1, "A")) Or (Year(Sheet13.Cells(i, "A")) = Year(Sheet13.Cells(i + 1, "A")) And Month(Sheet13.Cells(i, "A")) <> Month(Sheet13.Cells(i + 1, "A"))) Or Sheet13.Cells(i + 1, "A") = "" Then
  76.                     rr = rr + 1
  77.                     Cells(rr, "A") = VBA.Month(Sheet13.Cells(i, "A")) & "月"
  78.                     Cells(rr, "C") = "本月合计"  '
  79.                     Cells(rr, "G") = balance
  80.                     Cells(rr, "E") = jf ' 借方
  81.                     Cells(rr, "F") = df ' 贷方
  82.                     temps = "a" & rr & ":" & "g" & rr
  83.                     Range(temps).Interior.ColorIndex = 34   '本月合计颜色
  84.                     rr = rr + 1
  85.                     Cells(rr, "A") = VBA.Month(Sheet13.Cells(i, "A")) & "月"
  86.                     Cells(rr, "C") = "本年累计" '   4  摘要
  87.                     Cells(rr, "G") = balance
  88.                     Cells(rr, "E") = ljj ' 借方
  89.                     Cells(rr, "F") = ljd ' 贷方
  90.                     temps = "a" & rr & ":" & "g" & rr
  91.                     Range(temps).Interior.ColorIndex = 6   '本年累计颜色
  92.                     jf = 0
  93.                     df = 0
  94.                     yf = 0
  95.                 End If
  96.             Next
  97.         End If
  98.         Range("A5:G" & rr).Borders.LineStyle = xlContinuous
  99.         Sheet13.Range("A:E").ClearContents
  100.         Unload Me
  101.         Application.ScreenUpdating = True
  102.     End If
  103. End Sub
  104. Sub 汇总人民币() '正则用法二
  105. Dim 数据源 As String, Item As Double '声明变量
  106. 数据源 = "美元:123元  人民币:44元 英磅:100元 美元:44元 人民币:300.06元" '待计算的字符串
  107. With CreateObject("VBSCRIPT.REGEXP")  '引用正则表达式
  108.   .Global = True     '全局匹配
  109.   .Pattern = "人民币:(\d+.(\d+)?)(?=元)"  '指定匹配条件
  110.   Set Matches = .Execute(数据源)  '执行匹配
  111.   For Each Match In Matches    '遍历匹配的结果
  112.     Item = Item + Replace(Match.Value, "人民币:", "")  '将“人民币:”替换成空,然后逐一累加
  113.   Next
  114.   MsgBox "人民币合计:" & Item '报告合计结果
  115. End With
  116. End Sub
  117. Function 去除不可见字符(rng As Range)
  118.     Dim ar, i
  119.     ar = Array(9, 10, 13, 28, 29, 30, 31, 32, 127)
  120.     For i = 0 To UBound(ar)
  121.        rng.Replace ChrW(ar(i)), ""
  122.     Next
  123.     For i = 129 To 254
  124.        rng.Replace ChrW(i), ""
  125.     Next
  126.     rng.Replace " ", ""
  127.    
  128. End Function
  129. Sub 去重2() '调用系统去重功能 提取不重复项
  130.     Sheet1.Cells.Clear
  131.     Sheet3.Range("A:D").Copy Sheet1.Range("A1")
  132.     Sheet1.UsedRange.RemoveDuplicates _
  133.         Columns:=Array(1, 3), Header:=xlYes
  134. End Sub
  135. Private Sub cmd退出_Click()
  136.     Unload Me
  137. End Sub
  138. Private Sub CommandButton1_Click()
  139.     rtnRow = 0
  140.     commTableName = "往来单位"
  141.     frmInPut.Show
  142.     If rtnRow > 0 Then
  143.         Me.TextBox1 = Sheet19.Cells(rtnRow, "B")
  144.     End If
  145. End Sub
  146. Private Sub txt开始日_AfterUpdate()
  147.     Dim wEndymd As Date
  148.     If IsDate(Me.txt开始日) Then
  149.         Me.txt开始日 = Format(Me.txt开始日, "yyyy-m-d")
  150.     Else
  151.         MsgBox "日期(年/月/日)请输入", vbExclamation + vbOKOnly, "Input Error!"
  152.         Me.txt开始日 = ""
  153.     End If
  154. End Sub
  155. Private Sub txt终了日_AfterUpdate()
  156.     If IsDate(Me.txt终了日) Then
  157.         Me.txt终了日 = Format(Me.txt终了日, "yyyy-m-d")
  158.     Else
  159.         MsgBox "日期(年/月/日)请输入", vbExclamation + vbOKOnly, "Input Error!"
  160.         Me.txt终了日 = ""
  161.     End If
  162. End Sub
  163. Private Sub cmd开始日Calendar_Click()
  164.     commParamDate = Me.txt开始日
  165.     frmCalendar.Show vbModal
  166.     If IsNull(rtnDate) = False Then
  167.         Me.txt开始日 = rtnDate
  168.         Call txt开始日_AfterUpdate
  169.     End If
  170. End Sub
  171. Private Sub cmd终了日Calendar_Click()
  172.     commParamDate = Me.txt终了日
  173.     frmCalendar.Show vbModal
  174.     If IsNull(rtnDate) = False Then
  175.         Me.txt终了日 = rtnDate
  176.         Call txt终了日_AfterUpdate
  177.     End If
  178. End Sub
  179. Private Sub UserForm_Activate()
  180.     Me.txt开始日 = Format(DateSerial(Year(Date), Month(Date), 1), "yyyy-m-d")
  181.     Me.txt终了日 = Format(DateSerial(Year(Date), Month(Date) + 1, 0), "yyyy-m-d")
  182. End Sub
复制代码


回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-15 11:01 , Processed in 0.702075 second(s), 15 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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