将一个单一值复制粘贴到另一个工作表

时间:2015-11-14 12:05:23

标签: excel vba excel-vba

我有一个专栏" ID"从sheet1中的C3列开始,列出所有id,每个唯一ID之间有4个空白单元格,如下所示

enter image description here

我想复制id并将其粘贴到Sheet2五次,如下所示

enter image description here

任何人都可以提出如何在VBA的帮助下实现自动化

2 个答案:

答案 0 :(得分:1)

将此公式放在Sheet2!B2,

=vlookup("zzzz", sheet1!C$3:C3, 1)

根据需要填写。

答案 1 :(得分:0)

试试这个:

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

我已经使用您的示例数据测试了我的代码。它适用于我。

我相信这会对你有所帮助。