ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 Excel Home精品图文教程库
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
300集Office 2010微视频教程 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
查看: 678|回复: 7

[求助] 恳请高手出手帮忙,条件比较复杂,数据量也比较大,感激不尽!

[复制链接]

TA的精华主题

TA的得分主题

发表于 2020-1-2 11:50 | 显示全部楼层 |阅读模式
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
本帖最后由 xvbwily 于 2020-1-2 11:56 编辑

示例文件上传,相关条件要求在说明里,再次感谢!

门架交易信息.zip

446.32 KB, 下载次数: 20

TA的精华主题

TA的得分主题

发表于 2020-1-2 12:27 | 显示全部楼层

TA的精华主题

TA的得分主题

发表于 2020-1-2 13:34 | 显示全部楼层
  1. Option Explicit

  2. Sub Test()
  3.     Dim sh As Worksheet, arrData As Variant, arrResult As Variant, lngRow As Long
  4.     Dim rgData As Range, rgSortA As Range, rgSortB As Range
  5.     Dim strGantryID As String, strPreGantryID As String '门架编号
  6.     Dim strCarID As String, strPreCarID As String '车牌号码
  7.     Dim dateTradeTime As Date '交易时间
  8.     Dim datePreTime As Date '前一个交易时间
  9.     Dim dblMoney As Double '交易金额
  10.     Dim strState As String '交易状态
  11.     Dim lngAllowableDiff As Long '容许的时间差
  12.     Dim lngDiff As Long '时间差
  13.         
  14.     Set sh = Sheets("原始数据") ' 操作的表名
  15.     '排序
  16.     Set rgData = sh.UsedRange '排序区域,全表
  17.     Set rgSortA = sh.Range("D1") '第一个排序字段,车牌号码
  18.     Set rgSortB = sh.Range("I1") '第二个排序字段,交易时间
  19.     rgData.Sort key1:=rgSortA, order1:=xlAscending, key2:=rgSortB, order2:=xlAscending, Header:=xlYes

  20.     lngAllowableDiff = 10 '容许的时间差 为 10分钟

  21.     arrData = rgData '区域转数组
  22.     ReDim arrResult(LBound(arrData) To UBound(arrData), 1 To 1) As String '定义结果数组
  23.     Set rgData = Nothing:    Set rgSortA = Nothing:    Set rgSortB = Nothing
  24.    
  25.     Set rgData = sh.Range("P1") '结果返回列
  26.    
  27.     '从第2行开始判断
  28.     For lngRow = LBound(arrData) + 1 To UBound(arrData)
  29.         strGantryID = Trim(arrData(lngRow, 2)) '门架编号
  30.         
  31.         strCarID = arrData(lngRow, 4) '车牌号码
  32.         
  33.         dateTradeTime = CDate(arrData(lngRow, 9)) '交易时间
  34.         
  35.         dblMoney = Val(arrData(lngRow, 7))  '交易金额
  36.         strState = arrData(lngRow, 10) '交易状态
  37.         
  38.         If lngRow > LBound(arrData) + 1 Then
  39.             strPreGantryID = Trim(arrData(lngRow - 1, 2)) '前一个门架编号
  40.             strPreCarID = arrData(lngRow - 1, 4) '上一行车牌号码
  41.             datePreTime = CDate(arrData(lngRow - 1, 9)) '前一个交易时间
  42.         End If
  43.         
  44.         lngDiff = Abs(DateDiff("n", dateTradeTime, datePreTime)) '时间差
  45.         
  46.         '如果交易状态 为 失败,则处理
  47.         If strState = "交易失败" Then
  48.             arrResult(lngRow, 1) = CheckState(strCarID, strPreCarID, strGantryID, strPreGantryID, dblMoney, lngAllowableDiff, lngDiff)
  49.         End If
  50.     Next
  51.    
  52.    
  53.     rgData.Resize(UBound(arrResult), 1) = arrResult
  54. End Sub


  55. Function CheckState(strCarID As String, strPreCarID As String, strGantryID As String, strPreGantryID As String, dblMoney As Double, lngAllowableDiff As Long, lngDiff As Long) As String
  56.     Dim strGantryID_15 As String, strGantryID_14 As String '门架编号前15位 为 通道,前14位为 出入口
  57.     Dim strPreGantryID_15 As String, strPreGantryID_14 As String
  58.    
  59.     '所有车牌号码为“默A00000”的,返回:“无车牌”
  60.     If strCarID = "默A00000" Then
  61.         CheckState = "无车牌"
  62.         Exit Function
  63.     End If
  64.    
  65.     strGantryID_15 = Mid(strGantryID, 1, 15) '门架编号前15位 为 通道
  66.     strGantryID_14 = Mid(strGantryID, 1, 14)  '门架编号前14位为 出入口
  67.     strPreGantryID_15 = Mid(strPreGantryID, 1, 15) '门架编号前15位 为 通道
  68.     strPreGantryID_14 = Mid(strPreGantryID, 1, 14)  '门架编号前14位为 出入口
  69.     '车牌号码相同的连续两条数据,一条交易成功,一条交易失败,且门架名称是相反的,时间在
  70.     '返回“反向感应”
  71.     If strCarID = strPreCarID Then '上下两条记录,车号相同
  72.         If strGantryID_15 = strPreGantryID_15 Then '通道相同
  73.             If lngDiff <= lngAllowableDiff Then '在容许的时间差内
  74.                 CheckState = "重复读取"
  75.                 Exit Function
  76.             End If
  77.         Else
  78.             '出入口相同,并在容许的时间差内
  79.             If strGantryID_14 = strPreGantryID_14 And lngDiff <= lngAllowableDiff Then
  80.                 CheckState = "反向感应"
  81.                 Exit Function
  82.             End If
  83.         End If
  84.     End If
  85.    
  86.     '有交易金额,但显示交易失败,返回:“扣款失败”
  87.     If dblMoney > 0 Then
  88.         CheckState = "扣款失败"
  89.         Exit Function
  90.     End If
  91.    
  92.     CheckState = "****原因未知***"
  93.    
  94. End Function
复制代码

评分

1

查看全部评分

TA的精华主题

TA的得分主题

发表于 2020-1-2 13:42 | 显示全部楼层
microyip 发表于 2020-1-2 12:27
这种伸手党的忙……

  这种他就是找代工 别人估计都要开高价。判断条件太繁琐

TA的精华主题

TA的得分主题

发表于 2020-1-2 14:06 | 显示全部楼层
要求很明确,值得鼓励,坐等大佬分享结果

TA的精华主题

TA的得分主题

 楼主| 发表于 2020-1-2 15:22 | 显示全部楼层

TA的精华主题

TA的得分主题

 楼主| 发表于 2020-1-2 15:23 | 显示全部楼层

TA的精华主题

TA的得分主题

 楼主| 发表于 2020-1-2 15:49 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助

感谢您 的热情相助,正是我想要的结果。
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

关闭

最新热点上一条 /1 下一条

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2024-4-23 22:11 , Processed in 0.033000 second(s), 12 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

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