检查一个Cell是否与另一个Cell匹配

问题描述 投票:0回答:2

我有一个代码,可以根据控件表上特定单元格的条目生成工作表。现在,我有编码检查工作表名称是否存在,是否包含非法字符,以及条目是否包含太多字符。

我现在需要做的是查看Cell I16是否与I17,18,21,22或23的内容匹配。我更喜欢它是单独检查单元格而不是作为范围,以便我可以换出特定细胞根据需要。我根本不知道检查单元格字符串值是否相互匹配的代码。

excel vba excel-vba
2个回答
0
投票

一个简单的循环怎么样:

Dim Loop1 as Long

For Loop1= 17 to 23
    If Cells(16,9).value = Cells(Loop1,9).value
    'Do something
    End If
Next loop1

0
投票

使用选择案例。它检查案例的某个输入(在这种情况下,提到的输入I16到查看单元格I17-I20的值)。

Sub selectcase()

Dim var As Range
Dim wSheet As Worksheet

Set wSheet = ActiveSheet
Set var = wSheet.Range("I16")

    Select Case var.Value 'insert variable (or range) to test I16 in this case
        Case wSheet.Range("I17") 'check to see if it matches the value in I17
             MsgBox "It's I17"  'output, modify this to your use
        Case wSheet.Range("I18")
             MsgBox "It's I18"
        Case wSheet.Range("I19")
             MsgBox "It's I19"
        Case Else
             MsgBox "It's none"
    End Select

End Sub
© www.soinside.com 2019 - 2024. All rights reserved.