Excel 我如何使用类似于偏移量的东西。不使用它就选择并设置选择?
我正在尝试将行从Sheet1复制到Sheet2。我从Sheet1复制的每个单元格都需要粘贴9次到Sheet2。所以看起来像这样 表1 - A. 1. 日期/时间 2. 3/3/21 00:00Excel 我如何使用类似于偏移量的东西。不使用它就选择并设置选择?,excel,vba,Excel,Vba,我正在尝试将行从Sheet1复制到Sheet2。我从Sheet1复制的每个单元格都需要粘贴9次到Sheet2。所以看起来像这样 表1 - A. 1. 日期/时间 2. 3/3/21 00:00 如果我必须这样做,我会这样做: Option Explicit Sub Test() 'Use with Block to refer to sheet1 without writting the path With ThisWorkbook.Sheets("Sheet
如果我必须这样做,我会这样做:
Option Explicit
Sub Test()
'Use with Block to refer to sheet1 without writting the path
With ThisWorkbook.Sheets("Sheet1")
'find the last row with data
Dim i As Long: i = .Cells(.Rows.Count, 1).End(xlUp).Row
'input the whole range into an array
Dim dat As Variant: dat = .Range("A2:A" & i).Value
End With
'resize another array equal to dat * 9 times (as you need each cell 9 times)
ReDim dest(1 To UBound(dat) * 9, 1 To 1) As Variant
'start a count variable with value 1
Dim x As Long: x = 1
Dim y As Long
'loop through the dat array to fill the dest array
For i = 1 To UBound(dat)
'for each value in dat, input 9 times into dest
For y = 1 To 9
dest(x, 1) = dat(i, 1)
x = x + 1
Next y
Next i
'find the first row without data in sheet2 and paste the dest array
With ThisWorkbook.Sheets("Sheet2")
i = .Cells(.Rows.Count, 1).End(xlUp).Row + 1
.Range("A" & i).Resize(UBound(dest), UBound(dest, 2)).Value = dest
End With
End Sub
设置rng2=rng2.偏移量(9,0);value=rng2.value这是一个很好的解决方案,我没有考虑从一个数组复制到另一个数组,然后粘贴该数组。非常棒的解决方案,只需几秒钟,谢谢!