VBA-将一个工作表中的两列与另一工作表中的两列进行比较

如何解决VBA-将一个工作表中的两列与另一工作表中的两列进行比较

将一个工作表中的两列与另一工作表中的两列进行比较,并从其他一些具有匹配列的列中获取相应的值。

我的第一张纸上有一个唯一的ID和行ID。我在第二张工作表中以随机顺序具有相同的唯一ID和行ID。我想将第一张纸中的唯一ID和行ID匹配到第二张纸。如果唯一ID和行ID都匹配,则第二张纸上有一些日期可以复制到第一张纸上。只要有匹配项,就应从第二张工作表中复制相应的日期,并将其粘贴到匹配的唯一ID和行ID旁边的第一张工作表上。

由于机密文件,我无法上传文件。

我不擅长VBA,非常感谢您解决此问题。

解决方法

非VBA方法

如果可以的话,我倾向于避免使用VBA-因此,如果这不需要自动化并且可以在工作表中添加列,那么最简单的解决方案就是根本不使用VBA。

在两个电子表格中插入一个新列,这是唯一ID和行ID串联在一起的键,然后只需使用vlookup取回值即可。

  • 如果您不希望它保持动态链接,请复制并粘贴值以摆脱公式。
  • 如果有时ID和行不匹配,只需将vloopup包装到IFERROR函数中,以在不匹配时使用默认值(例如空白)

第1张纸

// App compat and UI things
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.2.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.1'
implementation 'androidx.core:core-ktx:1.3.1'`enter code here`
implementation 'androidx.preference:preference:1.1.1'

第2张纸

ID  line  +Key+  Pseudo formula
X    13   X13   vlookup X13 in Key column of sheet 2  
Y    27   Y27   vlookup Y27 in Key column of sheet 2

+ Key + =要插入的新字段,只是= ID和行

,

VBA方法

以下是获取值的非常简单的方法。我可以肯定还有很多其他更有效的方法,但是如果这种方法太慢,我只会打扰他们。

要使用-根据评论配置前几个设置,希望您走了!

Sub RetrieveValues()

    Set ws1 = Sheets("Sheet1")      'Sheet we are going to copy results into
    Set ws2 = Sheets("Sheet2")      'Sheet we are going to copy results from

    'Identify the columns to use in the sheet we are copying into
    ws1UniqueIDCol = "B"         'Is the column with the first part of our lookup key - the unique id
    ws1LineIdCol = "C"           'Is the column with the second part of our lookup key - the line id
    ws1ValToWriteCol = "D"       'Is the column we want to put the found value into
    
    ws1StartRow = 3              'The row we want to start processing first,we can calc the last row automatically
    ws1EndRow = ws1.UsedRange.Rows(ws1.UsedRange.Rows.Count).Row
    
    
    'Identify the columns to use in the sheet we are copying from
    ws2UniqueIDCol = "B"         'Is the column with the first part of our lookup key - the unique id
    ws2LineIdCol = "C"           'Is the column with the second part of our lookup key - the line id
    ws2ValToCopyCol = "D"        'Is the column we want to copy the value from
    
    ws2StartRow = 3              'The row we want to start search first,we can calc the last row automatically
    ws2EndRow = ws2.UsedRange.Rows(ws2.UsedRange.Rows.Count).Row
    
    'iterate through search terms
    For i = ws1StartRow To ws1EndRow        '
        searchKey = ws1.Range(ws1UniqueIDCol & i) & ws1.Range(ws1LineIdCol & i)
        
         'if we have a non blank search term then iterate through possible matches
        If (searchKey <> "") Then
            For j = ws2StartRow To ws2EndRow
                 foundKey = ws2.Range(ws2UniqueIDCol & j) & ws2.Range(ws2LineIdCol & j)
                  
                  'if we have a match then copy the result and end this search (so we will take the first match found only)
                 If (searchKey = foundKey) Then
                    ws1.Range(ws1ValToWriteCol & i).Value2 = ws2.Range(ws2ValToCopyCol & j).Value2
                    Exit For
                 End If
            Next
        End If
    Next

End Sub

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?