热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

vba:使用两个条件标识行,并从行返回其他数据-vba:Identifyrowusing2criteriaandreturnotherdatafromrow

Iamtryingtofindcodethatlooksattwocriteriainspreadsheet1andfindsarowthatcorresponds

I am trying to find code that looks at two criteria in spreadsheet1 and finds a row that corresponds in spreadsheet2 and returns a third piece of data in spreadsheet2 to spreadsheet1. I need to do this in vba because it loops, because I will be done it again and again, and because the data from spreadsheet2 imports from another database and will change over time. If possible it would be nice if the code also allowed for identifying a 3rd criteria on spreadsheet2.

我正在寻找查看spreadsheet1中的两个标准的代码,并在spreadsheet2中找到一个对应的行,并将spreadsheet2中的第三个数据返回到spreadsheet1。我需要在vba中这样做,因为它是循环的,因为我将一次又一次地这样做,而且因为来自spreadsheet2的数据从另一个数据库中导入,并且随着时间的推移会发生变化。如果可能的话,如果代码还允许识别电子表格2上的第三个标准,那就太好了。

Example is: Spreadsheeet 1

的例子是:Spreadsheeet 1

 Product ID  ActCode: A0003
 11111
 12345
 22222
 ...

Spreadheet 2

Spreadheet 2

ProductID  ActivityCode   DateDue
 11111     A0001          7/15/15
 11111     P7530          7/30/15
 11111     A0003          8/1/15
 12345     A0003          12/15/15
 12345     A0007          1/1/15
 22222     A0001          2/1/15 
 ...

Where I want Spreadsheet1 to end up:

我希望Spreadsheet1到此结束:

Spreadsheeet 1

Spreadsheeet 1

 Product ID  ActCode: A0003
 11111       8/1/15
 12345       12/15/15
 22222         - 
 ...

I have tried a ton of things over the past few days. 1) vlookup/index/match combos that have never really worked, 2) filtering spreadsheet2 by productID and activitycode and then copying to spreadsheet1 the visible cells - this works but is very slow. I will be doing this for many activity codes, so I need something faster (I can post the code if you want to see it). I am currently trying a loop within a loop. Not sure if this is the best way but here is the code I have so far. It does copy some dates over, but not the right ones - its also a bit slow.

在过去的几天里我尝试了很多东西。1)vlookup/index/match combos,它们从未真正起作用,2)通过productID和activitycode进行过滤,然后复制到可视单元——这是可行的,但速度非常慢。我将对许多活动代码执行此操作,因此我需要一些更快的东西(如果您想看的话,我可以发布代码)。我现在正在循环中尝试一个循环。不确定这是否是最好的方法,但这是我到目前为止的代码。它确实复制了一些日期,但不是正确的日期-它也有点慢。

Sub test()

Application.ScreenUpdating = False
  Sheets("Spreadsheet1").Select

  Range("A2").Select ' Select A = the column with the product ID in it
  ' Set Do loop to stop when an empty cell is reached.
  Do Until IsEmpty(ActiveCell)

    Dim ConceptAct  As String
    COnceptAct= "A0003"

    Dim ProductID
    ProductID = ActiveCell.Value
    Dim ConcDue

        Sheets("Spreadsheet2").Select
        Range("A2").Select 'The column with the ProductID in it

        Do Until IsEmpty(ActiveCell)

        If ActiveCell.Value = ProductID And ActiveCell.Offset(0, 1).Value = ConceptAct Then

            COncDue= ActiveCell.Offset(0, 2).Value

            Exit Do
        End If
           ActiveCell.Offset(1, 0).Select
        Loop

     Sheets("Spreadsheet1").Select

     ActiveCell.Offset(0, 1) = ConcDue

     ' Step down 1 row from present location.
     ActiveCell.Offset(1, 0).Select

  Loop

Application.ScreenUpdating = True

End Sub

2 个解决方案

#1


1  

Why won't Index/Match work? I was able to get, I think, your solution with an Index/Match formula entered as an array. Here's a screenshot of everything:

为什么不索引/匹配工作吗?我想,我能得到一个指数/匹配公式作为数组的解。以下是所有内容的截图:

enter image description here

Index/Match can use multiple criteria for looking things up, just connect these with &, both in the first and second parts of the Match(), and hit CTRL+SHIFT+ENTER to enter as an array. This formua will look at the product ID, then that ActCode, return the date.

Index/Match可以使用多个条件来查找内容,只需在Match()的第一部分和第二部分连接&,然后按CTRL+SHIFT+ENTER以数组的形式输入。这个formua将查看产品ID,然后是ActCode,返回日期。

Is this what you were looking for?

这就是你要找的吗?

#2


0  

I'm unclear on why a two-column criteria formula using native worksheet functions is not apprpriate but a User Defined Function (aka UDF) would be one avenue to pursue from a VBA point of view.

我不清楚为什么使用本机工作表函数的两列标准公式不是apprpriate的,但是从VBA的观点来看,用户定义函数(又名UDF)将是一种途径。

Function udf_Get_Two(sCode As Range, rCodes As Range, _
                     sProd As Range, rProds As Range, _
                     rDates As Range)
    Dim vCode As Variant, rw As Long, m As Long

    'quick check to see if there is anything to find
    If CBool(Application.CountIfs(rCodes.Columns(1), sCode.Value, _
      rProds.Resize(rCodes.Rows.Count, 1), sProd.Value)) Then
        rw = 0
        For m = 1 To Application.CountIf(rCodes.Columns(1), sCode.Value)
            rw = rw + Application.Match(sCode.Value, rCodes.Columns(1).Resize(rCodes.Rows.Count - rw, 1).Offset(rw, 0), 0)
            If rProds(rw, 1) = sProd Then
                udf_Get_Two = rDates.Cells(rw, 1).Value
                Exit Function
            End If
        Next m
    End If

End Function

Use like any other worksheet formula. Example:

像其他工作表公式一样使用。例子:

=udf_Get_Two(A2, Sheet2!$A$2:$A$7, $C$1, Sheet2!$B$2:$B$7, Sheet2!$C$2:$C$7)

    UDF for two column lookup

    

Note that the returned values are raw. The cells should be formatted as m/d/yy or as you prefer.

注意返回的值是原始的。单元格应该被格式化为m/d/yy或您喜欢的格式。


推荐阅读
  • 本文详细介绍了Akka中的BackoffSupervisor机制,探讨其在处理持久化失败和Actor重启时的应用。通过具体示例,展示了如何配置和使用BackoffSupervisor以实现更细粒度的异常处理。 ... [详细]
  • 本文深入探讨 MyBatis 中动态 SQL 的使用方法,包括 if/where、trim 自定义字符串截取规则、choose 分支选择、封装查询和修改条件的 where/set 标签、批量处理的 foreach 标签以及内置参数和 bind 的用法。 ... [详细]
  • 本文详细介绍如何使用Python进行配置文件的读写操作,涵盖常见的配置文件格式(如INI、JSON、TOML和YAML),并提供具体的代码示例。 ... [详细]
  • 深入理解 SQL 视图、存储过程与事务
    本文详细介绍了SQL中的视图、存储过程和事务的概念及应用。视图为用户提供了一种灵活的数据查询方式,存储过程则封装了复杂的SQL逻辑,而事务确保了数据库操作的完整性和一致性。 ... [详细]
  • 本文详细解析了Python中的os和sys模块,介绍了它们的功能、常用方法及其在实际编程中的应用。 ... [详细]
  • 本文详细介绍了 Apache Jena 库中的 Txn.executeWrite 方法,通过多个实际代码示例展示了其在不同场景下的应用,帮助开发者更好地理解和使用该方法。 ... [详细]
  • 本文介绍了如何通过 Maven 依赖引入 SQLiteJDBC 和 HikariCP 包,从而在 Java 应用中高效地连接和操作 SQLite 数据库。文章提供了详细的代码示例,并解释了每个步骤的实现细节。 ... [详细]
  • 本文详细介绍了Java中org.neo4j.helpers.collection.Iterators.single()方法的功能、使用场景及代码示例,帮助开发者更好地理解和应用该方法。 ... [详细]
  • Windows服务与数据库交互问题解析
    本文探讨了在Windows 10(64位)环境下开发的Windows服务,旨在定期向本地MS SQL Server (v.11)插入记录。尽管服务已成功安装并运行,但记录并未正确插入。我们将详细分析可能的原因及解决方案。 ... [详细]
  • 技术分享:从动态网站提取站点密钥的解决方案
    本文探讨了如何从动态网站中提取站点密钥,特别是针对验证码(reCAPTCHA)的处理方法。通过结合Selenium和requests库,提供了详细的代码示例和优化建议。 ... [详细]
  • Python自动化处理:从Word文档提取内容并生成带水印的PDF
    本文介绍如何利用Python实现从特定网站下载Word文档,去除水印并添加自定义水印,最终将文档转换为PDF格式。该方法适用于批量处理和自动化需求。 ... [详细]
  • 本章将深入探讨移动 UI 设计的核心原则,帮助开发者构建简洁、高效且用户友好的界面。通过学习设计规则和用户体验优化技巧,您将能够创建出既美观又实用的移动应用。 ... [详细]
  • 从 .NET 转 Java 的自学之路:IO 流基础篇
    本文详细介绍了 Java 中的 IO 流,包括字节流和字符流的基本概念及其操作方式。探讨了如何处理不同类型的文件数据,并结合编码机制确保字符数据的正确读写。同时,文中还涵盖了装饰设计模式的应用,以及多种常见的 IO 操作实例。 ... [详细]
  • 本文详细介绍了Java中的访问器(getter)和修改器(setter),探讨了它们在保护数据完整性、增强代码可维护性方面的重要作用。通过具体示例,展示了如何正确使用这些方法来控制类属性的访问和更新。 ... [详细]
  • 本文介绍如何使用阿里云的fastjson库解析包含时间戳、IP地址和参数等信息的JSON格式文本,并进行数据处理和保存。 ... [详细]
author-avatar
哗锅_348
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有