繁体   English   中英

将第1列的值与第2列匹配的代码

[英]Code to Match Value of Colum 1 against Column 2

我有一个带有一个组合框,文本框和附加了图像的命令按钮的小型用户窗体。 在此处输入图片说明

工作表1上有一张桌子,其图像附在下面。 在此处输入图片说明

我想要的是,当我按下用户窗体中的命令按钮时,它必须每次检查第1列中的值与第2列中的值。

例如,如果第1列的值为“非流动资产”,而第2列的值为“ PPE”,则msgbox将显示为“ Value Exist”。

代码每次必须将column1的value1与column2的value1进行比较,如果不存在,则msgbox将显示“ Value Not Exist”。

这是我的代码:

块引用

Private Sub CommandButton1_Click()
'Declaring the Variables
Dim ws As Worksheet, tbl As ListObject, row As ListRow
Dim value1 As String, value2 As String
Dim rng1 As Range, rng2 As Range

Set ws = Sheets("Sheet1")
Set tbl = ws.ListObjects("Table1")

ws.Unprotect Password:="google"

Set rng1 = tbl.ListColumns(1).DataBodyRange
Set rng2 = tbl.ListColumns(2).DataBodyRange

value1 = LCase(Me.ComboBox1.Value)
value2 = LCase(Me.TextBox1.Value)

'Loop to Check for the Group Head Existence
If Not rng1 Is Nothing Then
For Each rng1 In rng1
    If LCase(rng1.Value) = value1 Then
        For Each rng2 In rng2
            If LCase(rng2.Value) = value2 Then
            MsgBox ("Value Exist.")
            Exit Sub
        Else
            MsgBox ("Value not Exist")
            Unload Me
            Exit Sub
            End If
    Next rng2
End If
Next rng1
End If
End Sub

该代码未针对column2中的value1检查column1的value1。

好心提醒。

在If Not rng1 is Nothing Then语句中使用以下代码

For Each cell In rng1
        If LCase(cell.Value) = value1 and LCase(cell.offset(0,1).Value)=value2 Then
                MsgBox ("Value Exist.")
                Exit Sub
        Else
                MsgBox ("Value not Exist")
                Unload Me
                Exit Sub
        End If
    Next

你做不到

For Each rng1 In rng1

相反,您必须在rng1中循环抛出单元格,例如

dim cel as range

for each cel in rng1

同样的事情

For Each rng2 In rng2

尝试修改,看看会给你带来什么

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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