ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

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

[求助] 对range使用sort方法后,无法继续使用Find方法

[复制链接]

TA的精华主题

TA的得分主题

发表于 2018-3-21 22:07 | 显示全部楼层 |阅读模式
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
因公司业务需要,我在开发自己的程序的时候,对原先的数据只需要在交易日列数据中找到最后一个交易日的行数即可,但是新的需求是,如果交易日列的数据中的交易日是乱序的,我需要先进行排序,然后找到最后一个交易日所在行。
问题出现在,我在对表格中的数据按时间排序完成后,无法对交易时间数据所在列使用Find方法,提示错误:运行时错误“1004”,应用程序定义或对象定义错误。
代码如下:
  1. With Workbooks(fileName).Worksheets(wst)

  2.             .Range(sourceCol & 1).Sort key1:=.Range(tradingDateCol & 1).Value2, Order1:=xlAscending, Header:=xlYes

  3.             lastRow = .Range(tradingDateCol).Find(What:=lastTradingDay, SearchOrder:=xlByRows, LookAt:=xlWhole).row '程序运行到这里出错

  4.             For firstRow = 2 To lastRow

  5.                 If Not sourceDict.Exists(.Range(sourceCol & firstRow).Value2) Then

  6.                     GoTo continue

  7.                 End If

  8.                     GJDataFromProductionReport = .Range(GJDataCol & firstRow).Value2

  9.                     NmDataFromProductionReport = .Range(NmDataCol & firstRow).Value2

  10.                     '检查每天出入库数据

  11.                     If (GJDataFromProductionReport > 0 And NmDataFromProductionReport <= 0) Or (GJDataFromProductionReport <= 0 And NmDataFromProductionReport > 0) Then


  12.                         errorMessage = errorMessage & "逻辑有误。生产日报中" & .Range(sourceCol & firstRow).Value2 & "在" & .Range(tradingDateCol & firstRow).Value2 & "日,吉焦或立方米数据为0,但对应的吉焦数据不为0" & "/"

  13.                     ElseIf NmDataFromProductionReport <> 0 And .Range(sourceCol & firstRow).Value2 <> "xxx" Then

  14.                         coefficient = .Range(coefficientCol & firstRow).Value2

  15.                         If GJDataFromProductionReport / (NmDataFromProductionReport * 10) - coefficient > 0.015 Or GJDataFromProductionReport / (NmDataFromProductionReport * 10) - coefficient < -0.015 Then

  16.                                 errorMessage = errorMessage & "在生产日报中" & Application.WorksheetFunction.Text(.Range(tradingDateCol & firstRow).Value2, "yyyy-mm-dd") & .Range(sourceCol & firstRow).Value2 & "万方转吉焦数不一致,两者相差" & NmDataFromProductionReport * 10000 * coefficient / 1000 - GJDataFromProductionReport & "/"

  17.                         End If

  18.                     End If

  19. continue:

  20.             Next firstRow

  21.     End With
复制代码


TA的精华主题

TA的得分主题

发表于 2018-3-22 07:11 | 显示全部楼层
lastRow = .Range(tradingDateCol).Find(What:=lastTradingDay, SearchOrder:=xlByRows, LookAt:=xlWhole).row '程序运行到这里出错
因为区域内没有符合要求的值导致的吧
set rng= .Range(tradingDateCol).Find(What:=lastTradingDay, SearchOrder:=xlByRows, LookAt:=xlWhole)
if not rng is nothing then lastRow =rng.row '程序运行到这里出错

TA的精华主题

TA的得分主题

 楼主| 发表于 2018-3-22 10:41 | 显示全部楼层
liulang0808 发表于 2018-3-22 07:11
lastRow = .Range(tradingDateCol).Find(What:=lastTradingDay, SearchOrder:=xlByRows, LookAt:=xlWhole). ...

谢谢!
我重新调整代码,代码如下:
  1. Set rng = .Range(tradingDateCol & 2 & ":" & tradingDateCol & .Range(tradingDateCol & 2).End(xlDown).row).Find(what:=lastTradingDay, LookIn:=xlValues, SearchOrder:=xlByRows, LookAt:=xlWhole)
复制代码
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

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

GMT+8, 2024-5-2 03:08 , Processed in 0.034856 second(s), 10 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

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

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

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