Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/24.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_Find - Fatal编程技术网

在VBA中查找值并插入行

在VBA中查找值并插入行,vba,excel,find,Vba,Excel,Find,所以我要寻找的是它找到第一个值并添加上面的行,然后继续搜索下一个值并添加一行 它所做的是在第一个值之前插入行,直到达到600范围,然后宏结束 如何将其更改为只运行“查找并插入”一次,然后移动到下一个要查找的值?问题在于您的工作方向错误。例如,在循环中,如果它在第5行(x=5)中找到值,它将添加一行并将找到值的行向下移动(在本例中,从5移动到6)。当FOR循环迭代时,它将移动到下一行(x=6),并找到我们刚才移动的同一行。 要解决这个问题,您需要反向工作。在执行这样的函数或删除特定行时,这始终是一

所以我要寻找的是它找到第一个值并添加上面的行,然后继续搜索下一个值并添加一行

它所做的是在第一个值之前插入行,直到达到600范围,然后宏结束


如何将其更改为只运行“查找并插入”一次,然后移动到下一个要查找的值?

问题在于您的工作方向错误。例如,在循环中,如果它在第5行(x=5)中找到值,它将添加一行并将找到值的行向下移动(在本例中,从5移动到6)。当FOR循环迭代时,它将移动到下一行(x=6),并找到我们刚才移动的同一行。
要解决这个问题,您需要反向工作。在执行这样的函数或删除特定行时,这始终是一种良好的做法。要反向工作,只需对FOR循环进行一个小的更改。尝试将其更改为:
对于x=600到1步-1


这告诉Excel从600变为1,一次迭代-1,避免您看到的问题。

值的顺序与在工作表中找到的顺序相同,但输入数据将根据条目的数量改变找到它们的行。
Sub o()
Dim x As Integer
Dim y As Integer

For y = 2 To 2
For x = 1 To 600
    If Cells(x, y).Value = "CD Sector Average" Then
        Cells(x, y).EntireRow.Select
        Selection.Insert Shift:=xlDown
        Cells(x + 2, y).Select
    End If
Next x
Next y

For y = 2 To 2
For x = 1 To 600
    Next x
    If Cells(x, y).Value = "CS Sector Average" Then
        Cells(x, y).EntireRow.Select
        Selection.Insert Shift:=xlDown
        Cells(x + 2, y).Select
    End If
Next x
Next y


For y = 2 To 2
For x = 1 To 600
    Next x
    If Cells(x, y).Value = "E Sector Average" Then
        Cells(x, y).EntireRow.Select
        Selection.Insert Shift:=xlDown
        Cells(x + 2, y).Select
    End If
Next x
Next y
End Sub