Excel 将行复制到不同的列,行数增加21
我想将数据从不同的行复制到其他列。 例如 这里的行数增加了21行 如何在excel中执行此操作?转到VBA并:Excel 将行复制到不同的列,行数增加21,excel,ms-office,vba,Excel,Ms Office,Vba,我想将数据从不同的行复制到其他列。 例如 这里的行数增加了21行 如何在excel中执行此操作?转到VBA并: Sub sdgfsa() Dim i As Integer Dim j As Integer j = 129 For i = 4 To 1000 Step 21 'put the end number you want, the start is 4 like your example Plan1.Cells(j, 8).Value
Sub sdgfsa()
Dim i As Integer
Dim j As Integer
j = 129
For i = 4 To 1000 Step 21 'put the end number you want, the start is 4 like your example
Plan1.Cells(j, 8).Value = Plan1.Cells(i, 2).Value 'Here, plan1 represents the sheet, 2 is B column, 8 is H column
'or you can use KazJaw's Copy here
j = j + 1 'j gets incremented
Next
End Sub
当您需要复制所有(即,值和格式)时,请按以下方式执行:
Range("B4").Copy Range("H129")
使用@Daniel代码,您可能需要在循环中使用类似的内容
'...see Daniel answer for beginning
Plan1.Cells(i, 2).Copy Plan1.Cells(j, 8)
'... and so on
在单元格H129中输入以下公式:
=INDIRECT("b"&(ROW(H129)-129)*21+4)
然后使用“填充”将公式从此单元格复制到其下方的单元格中
这是因为“H129”引用在复制时将更改为H130、H131等,因此ROW()函数将依次返回更高的数字
编辑:
已更正公式以匹配问题中的示例。转到VBA编辑器(使用Alt+F11从Excel打开)。将其粘贴到屏幕左侧资源管理器的“ThisWorkbook”中。然后从VBA编辑器中运行它。稍后,如果用户需要多次运行,您可以将代码分配给按钮。您是对的。我更正了公式,以便它能与正确的单元格一起工作。
=INDIRECT("b"&(ROW(H129)-129)*21+4)