数据的一致性检查

我有数据存储在三列Excel中。

Column A: Product no, Column B: Production site Column C: Sales code 

我需要检查每个产品编号的销售代码中前6位的一致性。

因此,例如,所有产品编号。 1,我需要检查销售代码中的前6位数字是否相等。 如果所有销售代码为产品编号 1是相等的,那么程序必须在D列中写Y代表Yes。如果销售代码不同,程序必须在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的初学者,所以我不知道如何做到这一点。

你有什么build议吗?

我们需要一个帮助列D1 = Product_SaleCode6

 D2=A2&"_"&LEFT(C2,6) 

然后,列E将是您的testing列,E1 =testing

 E2=IF(COUNTIF($A$2:$A$35,A2)=COUNTIF($D$2:$D$35,D2),"Y","N") 

填写D2以上的所有行的E2公式。

我想要做的是,检查产品的计数是否与该产品组的销售代码的6位数相同。

这是你可以做的:

  • 逐一扫描行

  • 当您确定一个新组时,将存储其产品编号和销售短代码(前6个字符)以及起始范围

  • 检查组的每个后续行,看看代码是否一致

  • 如果不将该组标记为已损坏并继续

  • 在组的最后回顾组的第一行并为组的每一行写入组标志“Y”或“N”

  • 标记行后检查当前行是否为空,是停止扫描

  • 否则重置下一组的值并继续扫描

这是一个快速而且不太脏的实现(用小数据集进行testing,但是在使用之前尽可能进行尽职调查)):

 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 

这是一个非常适合数据透视表的应用程序。 例如,使用公式=LEFT(C2,6)和透视表布局添加一列(“销售”),如下所示立即标识您的样本Y将适用于所有(每种情况下计数为1)(假设网站可能会有所不同):

SO16926030的例子

其他validation可以用不同的公式进行,以适应不同的列。