excel 循环复制数据到同一工作表中的偏移单元格

uyhoqukh  于 2023-01-03  发布在  其他
关注(0)|答案(1)|浏览(180)

我的目标是获取值并复制它,然后用一个额外的字符命名它。
例如,在我的B列中,我将值命名为Example1到Example4

我试图将它们复制到O列中的几个单元格下。我不能每次只复制并向下偏移四行,因为它们之间的数据行数不同。
我的目标是使用C列作为参考点来抵消它并收集信息。这可能不是最好的做法。

Sub Test()

    Dim addExample As String
    
    Set rngCC = Range("C1:C3000")
    
    addExample = "addEx"
    
    For Each cell In rngCC
    
        If cell = addExample Then
        
            ActiveCell.Select
            
            ActiveCell.Offset(0, -1).Select
            
            Selection.End(xlUp).Select
            
            Selection.Copy
            
            ActiveCell.Offset(0, 1).Select
            
            Cells.Find(What:="addEx", After:=ActiveCell, LookIn:=xlFormulas2, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False).Activate
            
            ActiveCell.Offset(0, -1).Select
            
            ActiveSheet.Paste
            
            ActiveCell.Select
            
            ActiveCell.Value = ActiveCell.Value & "b"
        
        End If
    
    Next

End Sub
t98cgbkg

t98cgbkg1#

这将为您提供所需的结果:

Sub Test()

    Dim r as Long, LastRow as Long
    With ActiveSheet
    
        LastRow = .Cells(.Rows.Count, "C").End(xlUp).Row
        
        For r = 1 To LastRow
            If .Cells(r, "C") = "AddEx" Then
                .Cells(r, "B").Value = .Cells(r, "B").End(xlUp).Value & "b"
                .Cells(r, "B").End(xlUp).Value = .Cells(r, "B").End(xlUp).Value & "a"
                .Cells(r, "C").ClearContents
            End If
        Next
    
    End With
    
End Sub

相关问题