Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba 计算差异量并返回带有Count的列名_Vba_Excel - Fatal编程技术网

Vba 计算差异量并返回带有Count的列名

Vba 计算差异量并返回带有Count的列名,vba,excel,Vba,Excel,我发现两张工作表之间存在差异,代码如下: For Each mycell In ActiveWorkbook.Worksheets(shtSheet2).UsedRange If Not mycell.Value = ActiveWorkbook.Worksheets(shtSheet1).Cells(mycell.Row, mycell.Column).Value Then mycell.Interior.Color = vbYellow difference = diffe

我发现两张工作表之间存在差异,代码如下:

For Each mycell In ActiveWorkbook.Worksheets(shtSheet2).UsedRange 
If Not mycell.Value = ActiveWorkbook.Worksheets(shtSheet1).Cells(mycell.Row, mycell.Column).Value Then
    mycell.Interior.Color = vbYellow
    difference = difference + 1
End If

If mycell.Value = ActiveWorkbook.Worksheets(shtSheet1).Cells(mycell.Row, mycell.Column).Value Then
   matches = matches + 1
End If
当单元格中的值不匹配时,它将突出显示该单元格的黄色并增加计数,以便知道差异的总量

我有标题部门,名称,销售,开始日期,结束日期。如何返回列的差异量

e、 g


差异
作为
数组
,并增加与当前
对应的分量。 如果需要,您可以对
匹配项执行类似操作。另外,如果
s,你可以缩写你的两个
If

Dim difference(1 To 5) As Long
Dim matches(1 To 5) As Long
For Each mycell In ActiveWorkbook.Worksheets(shtSheet2).UsedRange
Dim col as Long
col = mycell.Column
If Not mycell.Value = ActiveWorkbook.Worksheets(shtSheet1).Cells(mycell.Row, mycell.Column).Value Then
    mycell.Interior.Color = vbYellow
    difference(col) = difference(col) + 1
Else
    matches(col) = matches(col) + 1
End If

您可以查看。

谢谢!我在这方面遇到了问题,因为我是VB新手,我一直在获取错误对象变量,或者在“col=mycell.Column”@Tony行中未设置块变量-我跳过了第一行,但我知道您会保留它。现在我添加了它。请检查更新的代码。如果需要,执行一点调试。我花了一些时间尝试调试和学习阵列,但不幸的是,它似乎无法正常工作,还有什么建议吗?谢谢!但是,当我使用debug.print matches(col)时,这只会给我匹配的数量(12)和相同的差异,而不是在我的列表中列出每列中的匹配和错误question@Tony-当然,
difference
存储
Long
(整数)值。如果要打印标题,必须明确地执行,例如,
debug.print“Department:”&匹配(1)
Dim difference(1 To 5) As Long
Dim matches(1 To 5) As Long
For Each mycell In ActiveWorkbook.Worksheets(shtSheet2).UsedRange
Dim col as Long
col = mycell.Column
If Not mycell.Value = ActiveWorkbook.Worksheets(shtSheet1).Cells(mycell.Row, mycell.Column).Value Then
    mycell.Interior.Color = vbYellow
    difference(col) = difference(col) + 1
Else
    matches(col) = matches(col) + 1
End If