Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/17.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_Vba_Excel - Fatal编程技术网

子写入匹配值vba

子写入匹配值vba,vba,excel,Vba,Excel,所以我正在研究一个子程序,它在给定的范围内找到匹配的值并写出它们。 问题是,我的版本会写出范围内的所有值,因此我希望能得到一些帮助,对其进行更改,以便只写出匹配的值。 提前谢谢 Sub azonos2a() Dim rng As range Set rng = Application.InputBox("range", , , , , , , 8) Dim a Dim c Dim d With ActiveSheet For Each c In rng.Cells For Ea

所以我正在研究一个子程序,它在给定的范围内找到匹配的值并写出它们。 问题是,我的版本会写出范围内的所有值,因此我希望能得到一些帮助,对其进行更改,以便只写出匹配的值。 提前谢谢

Sub azonos2a()
Dim rng As range
Set rng = Application.InputBox("range", , , , , , , 8)

Dim a

Dim c
Dim d

With ActiveSheet


For Each c In rng.Cells
    For Each d In rng.Cells
        If c.Value = d.Value Then
            MsgBox c.Value
        End If
    Next
Next

End With

End Sub

在嵌套循环中,您正在将范围内的每个单元格与范围内的每个单元格(包括其自身)进行比较。您需要一个嵌套循环,其中内部循环传递的值与外部循环的值不同,但其方式是比较所有不同的单元格对。最简单的方法是使用索引:

Sub azonos2a()
Dim rng As Range
Set rng = Application.InputBox("range", , , , , , , 8)

Dim i As Long, j As Long, n As Long
n = rng.Cells.Count

For i = 1 To n - 1
    For j = i + 1 To n
        If rng.Cells(i).Value = rng.Cells(j).Value Then
            MsgBox rng.Cells(i).Value
        End If
    Next j
Next i

End Sub

带有ActiveSheet的
似乎没有点,c和d也变暗了。从rng中移除细胞。