【问题标题】:VBA: How to move value and offset for each subsequent valueVBA:如何移动每个后续值的值和偏移量
【发布时间】:2021-04-18 19:12:43
【问题描述】:

我正在尝试将不同工作表上循环中的每个值粘贴到某个单元格,然后为我要移动的每个值偏移 5 行。

Dim counter As Variant, carModel As Variant 
Dim theRange As Range, row As Range, cell As Range
counter = Sheets("Sheet1").UsedRange.Rows.Count

Set theRange = Sheets("Sheet1").Range(Sheets("Sheet1").Cells(2, 1), Sheets("Sheet1").Cells(counter, 7))
moveDest = Sheets("Destination").Cells(1, 1)


For Each row In theRange.Rows
    For Each cell In row
        carModel = cell.Cells(1)
        'paste this value into moveDest and move down 5 rows for the next cell loop
    Next
Next

【问题讨论】:

    标签: excel vba loops range offset


    【解决方案1】:

    这应该可以满足您的需求。

    我删除了您不需要的嵌套循环。我还使用了不同的方法来获取最后一行。

        Dim lastrow As Long
        Dim i As Long
        Dim moveDest As Range
        
        Set moveDest = Sheets("Destination").Cells(1, 1)
        
        With Sheets("Sheet1")
            lastrow = .Cells(.Rows.Count, 1).End(xlUp).row
            For i = 2 To lastrow
                .Cells(i, 1).Copy moveDest
                Set moveDest = moveDest.Offset(5, 0)
            Next i
        End With
    

    【讨论】:

    • 完美运行!我确实认为我设置范围的方法有点笨拙,还有我的循环啊哈
    猜你喜欢
    • 1970-01-01
    • 2013-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多