将单元格的值剪切并粘贴到vba中的另一个单元格

时间:2018-10-23 14:27:50

标签: excel vba excel-vba

如果列C等于'RRR',我需要将列F的值转移或移动到最后一个具有值的单元格到列D。我无法突出显示或选择从“ RRR”的位置开始到最后一个值为“ SSS”的单元格的范围。而是从C4:C9中选择范围,这是错误的。

    Dim ws As Worksheet, lRow As Long

Set ws = ThisWorkbook.ActiveSheet
lRow = ws.Cells(Rows.Count, 1).End(xlUp).Row

Dim lCol As Long

With ws
    For x = 1 To lRow
        If .Cells(x, 3).Value = "RRR" Then
            lCol = Cells(x, Columns.Count).End(xlToLeft).Column
            Range("C" & x & ":C" & lCol).Select
        End If
    Next x
End With

示例: enter image description here

预期:

enter image description here

任何人都可以在我的代码中告诉我这个问题。

2 个答案:

答案 0 :(得分:4)

您距离很近,只有应该修改的选择范围。

因此您可以建立自己的范围:

decimal(18,6)

这应该可以解决问题:

Range(A1:D1) -> Range(Cells(A1), Cells(D1)) -> 

Range(Cells(row number, column number), Cells(row number, column number)) -> 

Range(Cells(1, 1), Cells(1, 4))

答案 1 :(得分:3)

另一种方法是删除DE列中的单元格

Dim ws As Worksheet, lRow As Long
Dim x As Long

    Set ws = ThisWorkbook.ActiveSheet
    lRow = ws.Cells(Rows.Count, 1).End(xlUp).Row

    Dim lCol As Long

    With ws
        For x = 1 To lRow
            If .Cells(x, 3).Value = "RRR" Then .Range("C" & x & ":D" & x).Delete Shift:=xlToLeft
            End If
        Next x
    End With

    End Sub