VBA列值比较

发布于 2024-12-08 03:10:53 字数 380 浏览 0 评论 0原文

我有两张 Excel 表格。第一张工作表具有以下数据:

Column C
--------
101-AA-103  
101-AA-104  
101-AA-105  
101-BB-101  

第二张工作表具有以下数据:

Column A
--------
101-AA-100  
101-AA-101  
101-AA-102  
101-AA-103  

我想将第一张工作表中的 C 列与第二张工作表中的 A 列进行比较。例如,需要对照第二张纸上 A 列中的所有行检查第一张纸上 C 列的值 101-AA-103。如果找到该值,则应显示“可用”;否则,“不可用”。我如何编写 VBA 函数来执行此操作?

I have two excel sheets. The first sheet has this data:

Column C
--------
101-AA-103  
101-AA-104  
101-AA-105  
101-BB-101  

The second sheet has this data:

Column A
--------
101-AA-100  
101-AA-101  
101-AA-102  
101-AA-103  

I want to compare column C from the first sheet with column A from the second sheet. For example, value 101-AA-103 from column C on the first sheet needs to be checked against all the rows in column A on the second sheet. If the value is found, it should say "Available"; otherwise, "Not Available". How can I write a VBA function to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

记忆で 2024-12-15 03:10:53

试试这个:

Dim sh1 As Worksheet
Dim sh2 As Worksheet
Dim strToFind As String
Dim res As Range
dim maxrows as Integer

Set sh1 = ThisWorkbook.Sheets("Sheet1")
Set sh2 = ThisWorkbook.Sheets("Sheet2")
maxrows = 500

For i = 1 To maxrows
    strToFind = sh2.Cells(i, "A")
    With sh1
        Set res = .Columns("C").Find(What:=strToFind, After:=.Cells(1, "C"), LookIn:=xlValues, _
            LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
            MatchCase:=False, SearchFormat:=False)
        If res Is Not Nothing Then
          'Do here what you please
        End If
    End With
Next

请注意,您应该计算 maxrows 而不是使用 const 整数。

Try this:

Dim sh1 As Worksheet
Dim sh2 As Worksheet
Dim strToFind As String
Dim res As Range
dim maxrows as Integer

Set sh1 = ThisWorkbook.Sheets("Sheet1")
Set sh2 = ThisWorkbook.Sheets("Sheet2")
maxrows = 500

For i = 1 To maxrows
    strToFind = sh2.Cells(i, "A")
    With sh1
        Set res = .Columns("C").Find(What:=strToFind, After:=.Cells(1, "C"), LookIn:=xlValues, _
            LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
            MatchCase:=False, SearchFormat:=False)
        If res Is Not Nothing Then
          'Do here what you please
        End If
    End With
Next

Note that you should calculate maxrows and not use a const integer.

梦忆晨望 2024-12-15 03:10:53

您可以使用 Vlookup 轻松完成此操作:

=IF(ISERROR(VLOOKUP(C1,Sheet2!A:A,1,FALSE)),"Not Available","Available")

但是既然您要求使用 VBA,这里有一个函数可以完成此操作,利用字典对象和变体数组来提高效率和速度。

  • 将 C 列和 A 列转储到变体数组中
  • 制作 A 列值的字典
  • 搜索 C 列条目以查看它们是否存在于 A 中
  • 变量 i 也是行号,因此将文本放置在 D 列中相当简单 从技术上讲,
Sub TestAvailability()

Application.ScreenUpdating = False
Dim varrayC As Variant, varrayA As Variant
Dim lastRow As Long
Dim dict As Object
Set dict = CreateObject("scripting.dictionary")

lastRow = Sheets(2).range("A" & Rows.count).End(xlUp).Row
varrayA = Sheets(2).range("A1:A" & lastRow).Value

lastRow = Sheets(1).range("C" & Rows.count).End(xlUp).Row
varrayC = Sheets(1).range("C1:C" & lastRow).Value

On Error Resume Next
For i = 1 To UBound(varrayA, 1)
    dict.Add varrayA(i, 1), 1
Next

For i = 1 To UBound(varrayC, 1)
    If dict.exists(varrayC(i, 1)) = True Then
        Sheets(1).cells(i, 4).Value = "Available"
    Else
        Sheets(1).cells(i, 4).Value = "Not Available"
    End If
Next

Application.ScreenUpdating = True
End Sub

您可以创建一个新的可用性数组并将其转置到 D 列,但我不想让它变得过于复杂。

You could do this really easily using Vlookup:

=IF(ISERROR(VLOOKUP(C1,Sheet2!A:A,1,FALSE)),"Not Available","Available")

But since you asked for VBA, here's a function that will do it, utilizing a dictionary object and variant arrays for efficiency and speed.

  • Dump Column C and Column A into a variant array
  • Make a dictionary of Column A values
  • Search through the column C entries to see if they exist in A
  • The variable i is the row number as well, so it's rather simple to place text in column D.
Sub TestAvailability()

Application.ScreenUpdating = False
Dim varrayC As Variant, varrayA As Variant
Dim lastRow As Long
Dim dict As Object
Set dict = CreateObject("scripting.dictionary")

lastRow = Sheets(2).range("A" & Rows.count).End(xlUp).Row
varrayA = Sheets(2).range("A1:A" & lastRow).Value

lastRow = Sheets(1).range("C" & Rows.count).End(xlUp).Row
varrayC = Sheets(1).range("C1:C" & lastRow).Value

On Error Resume Next
For i = 1 To UBound(varrayA, 1)
    dict.Add varrayA(i, 1), 1
Next

For i = 1 To UBound(varrayC, 1)
    If dict.exists(varrayC(i, 1)) = True Then
        Sheets(1).cells(i, 4).Value = "Available"
    Else
        Sheets(1).cells(i, 4).Value = "Not Available"
    End If
Next

Application.ScreenUpdating = True
End Sub

Technically you could create a new array of availability and transpose it on column D, but I didn't want to make it overly complicated.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文