我将数据存储在Excel的三列中。
Column A: Product no,
Column B: Production site
Column C: Sales code我需要检查每个产品编号的销售代码的前6位是否一致。
例如,对于所有产品编号为1的产品,我需要检查销售代码中的前6位是否相等。如果1号产品的所有销售代码都相等,则程序必须在D列中为“是”写Y。如果销售代码不同,程序必须在D列中为“否”写N。
Product;Site;Sales code
1;A;86451001
1;B;864510.3
1;C;86451004
1;D;86451001
1;E;864510.3
1;F;86451004
1;G;86451001
1;H;864510.3
1;I;86451004
1;J;86451001
1;K;874507.3
1;L;87450704
1;M;87450701
1;N;885656.3
1;O;88565604
2;A;86451001
2;B;864510.3
2;C;86451004
2;D;86451001
2;E;864510.3
2;F;88565604
2;G;88565601
2;H;864510.3
2;I;86451004
2;J;86451001
2;K;874507.3
2;L;87450704
2;M;87450701
2;N;885656.3
3;A;88565604
3;B;86451001
3;C;864510.3
3;D;86451004
3;E;87450704我需要这种一致性检查,因为我的数据集很大。我是VBA的初学者,所以我不知道怎么做。
你有什么建议吗?
发布于 2013-06-05 18:55:34
我们需要一个帮助列,D1=Product_SaleCode6
D2=A2&"_"&LEFT(C2,6)然后,E列将成为您的测试列E1=Test
E2=IF(COUNTIF($A$2:$A$35,A2)=COUNTIF($D$2:$D$35,D2),"Y","N")在上面填写所有行的D2,E2公式。
我尝试做的是,检查产品计数是否与该产品组的销售代码的6位数计数相同。
发布于 2013-06-05 06:50:37
下面是你可以做的:
检查该组的每一后续行代码是否一致
< code >H19在该组的末尾,回看该组的第一行,并为该组的每一行写上组标志"Y“或"N”
这里是一个快速且不太脏的实现(使用您的小数据集进行测试,但当然在使用它之前要进行尽职调查;):
Sub check()
Dim sh As Worksheet
Set sh = Sheets(1)
Dim r As Range
Set r = sh.Range("A1")
Dim currentProduct As Integer
Dim currentProductSalesCode As String
Dim currentProductStart As Range
Dim ok As Boolean
currentProduct = -1
Do
' Are we changing of product group?
If r.Value2 <> currentProduct Then
' Check that this is not the beginning
If currentProduct <> -1 Then
Dim i As Integer
i = 0
' Apply the flag to all the rows in the current group
Do
If currentProductStart.Offset(i, 0) <> currentProduct Then
Exit Do
End If
Dim flagOutput As Range
Set flagOutput = currentProductStart.Offset(i, 3)
If ok Then
flagOutput = "Y"
Else
flagOutput = "N"
End If
i = i + 1
Loop
If IsEmpty(r) Then
Exit Do
End If
End If
'Reset the values for the current group
currentProduct = r.Value2
currentProductSalesCode = Left(r.Offset(0, 2).Text, 6)
Set currentProductStart = r
ok = True
Else
' If the current row code is not equal to the first row of the group code
If Left(r.Offset(0, 2).Text, 6) <> currentProductSalesCode Then
ok = False
End If
End If
Set r = r.Offset(1, 0)
Loop
End Sub发布于 2013-06-05 06:51:29
这是一个非常适合PivotTables的应用程序。例如,添加一个具有公式=LEFT(C2,6)的列("Sales ")和如下所示的数据透视表布局,可以立即确定示例中的Y将应用于所有(count在任何情况下都是1)(假设站点可能不同):

其他验证可以在不同的列中使用不同的公式进行,以适应不同的列。
https://stackoverflow.com/questions/16926030
复制相似问题