简体   繁体   中英

Check if one Cell Matches Another

I have a code that generates a sheet based on the entry of a specific cell on a control sheet. Right now, I have coding that checks to see if the sheet name exists, if it contains illegal characters, and if the entry has too many characters.

What I need to do now, is to see if Cell I16 matches the contents of I17, 18, 21, 22,or 23. I would prefer if it checked the cells individually, instead of as a range, so that I can swap out specific cells as needed. I simply don't know of the code to check if the cells string value matches one another.

How about a simple loop:

Dim Loop1 as Long

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

Use select case. It checks a certain input to the cases (in this case the mentioned input I16 to the value of the looked at cells I17 - I20 something).

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM