代码之家  ›  专栏  ›  技术社区  ›  Yoona May

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

  •  2
  • Yoona May  · 技术社区  · 7 年前

    我需要转移或移动F列的值,直到最后一个单元格的值转移到D列,如果C列等于“RRR”。我无法突出显示或选择从“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 回复  |  直到 6 年前
        1
  •  4
  •   Wizhi    7 年前

    所以你可以建立你的范围:

    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))
    

    这应该可以做到:

    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
                lCol = Cells(x, Columns.Count).End(xlToLeft).Column 'Find the last column number
                Range(Cells(x, 6), Cells(x, lCol)).Cut Cells(x, 4) 'Cut from row x and Column F (Column F = 6) to row x and column "lCol". Then paste the range into row x and column 4.
            End If
        Next x
    End With
    
    End Sub
    
        2
  •  3
  •   Wizhi    7 年前

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

    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