我有一个小型用户表单,其中包含一个组合框,文本框和命令按钮,其图像已附加。
我想要的是当我按下用户窗体中的命令按钮时,它必须每次检查第1列中的值与第2列中的值。
例如, 如果第1列具有值"非流动资产"反对它第2列有价值" PPE",然后msgbox将出现"价值存在"。
代码必须每次检查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
代码不会检查column1的value1对column2中的value1。
请告知。
答案 0 :(得分:2)
在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
答案 1 :(得分:0)
你做不到
For Each rng1 In rng1
相反,你必须在rng1中循环抛出单元格,如
dim cel as range
for each cel in rng1
同样的事情
For Each rng2 In rng2
尝试修改并查看它会给你什么