如何移动细胞

时间:2019-07-02 10:43:28

标签: excel vba

我想将值从Column I移到Column RColumn I中的值在2到174范围内的任意位置,所以我想知道一个方法其中可以检查所有行的值,找到该值后,将其移到Column R的第一行。

如果可能,该方法可以用于Column I中的多个随机值

我尝试使用嵌入的FOR loops,该命令检查表中的每个值,但这不起作用。

1 个答案:

答案 0 :(得分:1)

代码:

Option Explicit

Sub test()

    Dim arrValues As Variant, arrColumnI As Variant
    Dim i As Long, j As Long

    'Set an array with the values we are looking for
    arrValues = Array("Apples", "Orange", "Banana")

    'Change if needed
    With ThisWorkbook.Worksheets("Sheet1")
        'Set an array including all the values from 2 to 174
        arrColumnI = .Range("I2:I174").Value

        'Loop arrValues
        For i = LBound(arrValues) To UBound(arrValues)
            'Loop column Values
            For j = LBound(arrColumnI) To UBound(arrColumnI)

                If arrValues(i) = arrColumnI(j, 1) Then

                    .Cells(j + 1, 18).Value = arrColumnI(j, 1)
                    .Cells(j + 1, 9).Clear

                End If

            Next j

        Next i

    End With

End Sub

结果:

enter image description here