[英]copy paste one single value multiple times to another sheet
I have a Column "ID" starting from Column C3 in sheet1 where all the id's are listed with an interval of 4 blank cells between each unique id's, as shown below 我从sheet1的C3列开始有一个“ ID”列,其中列出了所有ID,每个唯一ID之间的间隔为4个空白单元格,如下所示
I want to copy the id's and paste it to Sheet2 five times as shown below 我想复制ID并将其粘贴到Sheet2五次,如下所示
Can anyone sugggest how can i get this automated with the help of VBA 任何人都可以建议我如何在VBA的帮助下实现自动化
Put this formula in Sheet2!B2, 将此公式放在Sheet2!B2中,
=vlookup("zzzz", sheet1!C$3:C3, 1)
Fill down as necessary. 根据需要填写。
Try this one: 试试这个:
Sub copyCell()
Dim idColumn As String
Dim startRow, lastRow, row As Integer
Dim sht1, sht2 As Worksheet
Set sht1 = Worksheets("Sheet1") 'Setting sheet1
Set sht2 = Worksheets("Sheet2") 'Setting sheet1
idColumn = "C" 'Setting column id value column
startRow = 1 'Setting the start row from sheet 1
'Getting the last used row from sheet 1
lastRow = sht1.Cells(sht1.Rows.Count, rowColumn).End(xlUp).row
For row = startRow To lastRow Step 1
'Check Id Value is empty or not
If sht1.Range(idColumn & row) <> "" Then
'If not empty, set value
sht2.Range(idColumn & row) = sht1.Range(idColumn & row)
Else
'If empty, set value from previous row of sheet 2
sht2.Range(idColumn & row) = sht2.Range(idColumn & (row - 1))
End If
Next row
End Sub
I already tested my code with your sample data. 我已经用您的示例数据测试了我的代码。 It works well for me. 这对我来说很有用。
I believe that will be helpful for you. 相信会对您有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.