VBA:如何为每个后续值移动值和偏移量
VBA: How to move value and offset for each subsequent value
我正在尝试将不同工作表上循环中的每个值粘贴到某个单元格,然后为我要移动的每个值偏移 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
这应该能满足您的需求。
我删除了不需要的嵌套循环。我还使用了不同的方法来获取最后一行。
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
我正在尝试将不同工作表上循环中的每个值粘贴到某个单元格,然后为我要移动的每个值偏移 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
这应该能满足您的需求。
我删除了不需要的嵌套循环。我还使用了不同的方法来获取最后一行。
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