用于比较的VBA代码
在工作表中我有以下列名
In a sheet I have following column name
Table1                            比较字段                 Table2
Table1 Compare Field Table2
字段名称 数据类型注册    结果           字段名称 数据类型注册
Field Name Datatype Reg Result Field Name Datatype Reg
Akshay       int       是          真的             xyz       flot     no
Akshay int Yes True xyz flot no
xyz           int          不,           错误           Akshay int  是
xyz int No False Akshay int Yes
我必须将table1所有三列数据与table2所有三个colum数据进行比较。 Table2数据可以存在于我必须搜索和比较的任何行。
I have to compare table1 all three column data with table2 all three colum data. Table2 data can be present at any row I have to search and compare it.
如果所有列数据匹配,我必须在Table1数据后的比较字段列中打印状态为真
If all the column data matches I have to print status True in Compare Field Column after Table1 data
代码将在之后开始执行点击比较按钮
Code will start executing after clicking on Compare Button
您好Akshay Chavan 07,
Hi Akshay Chavan 07,
您可以尝试调整以下您需要的代码。
You could try and adjust below code for your need.
Private Sub CommandButton1_Click()
Dim tbl1Rng As Range
Dim tbl2Rng As Range
Set tbl1Rng = ActiveSheet.ListObjects(1).Range
Set tbl2Rng = ActiveSheet.ListObjects(2).Range
For i = 2 To tbl1Rng.Rows.Count
name1 = tbl1Rng.Cells(i, 1)
type1 = tbl1Rng.Cells(i, 2)
reg1 = tbl1Rng.Cells(i, 3)
For j = 2 To tbl2Rng.Rows.Count
name2 = tbl2Rng.Cells(j, 1)
type2 = tbl2Rng.Cells(j, 2)
reg2 = tbl2Rng.Cells(j, 3)
If name1 = name2 And type1 = type2 And reg1 = reg2 Then
tbl1Rng.Cells(i, 1).Offset(0, 3) = "True"
GoTo NextRow
End If
Next j
tbl1Rng.Cells(i, 1).Offset(0, 3) = "False"
NextRow:
Next i
End Sub
最好的问候,
Terry