从工作表中选择单元格副本到另一个工作表

时间:2019-01-17 01:23:39

标签: excel vba

在如何将工作表1中的多个单元格(一张一张)复制到工作表2中的单个单元格方面,您需要您的建议和帮助。

每单击一次按钮,会将sheet1的单个单元格复制到sheet2的单元格“ B4”

这是我的示例代码:


Dim lRow, RowIndex As Long
Dim src As Worksheet, dest As Worksheet

Dim rng As Range

Set src = ThisWorkbook.Worksheets("sheet1")
Set dest = ThisWorkbook.Worksheets("sheet2")
lRow = src.Range("D8").End(xlDown).Row
RowIndex = 1

    For Each rng In src.Range("D8:D" & lRow)



    src.Range(rng & RowIndex).Copy Destination:=dest.Range("B4")

    Selection.Copy

    RowIndex = RowIndex + 1


    Next rng


Application.CutCopyMode = False

谢谢

2 个答案:

答案 0 :(得分:0)

尝试关注sub

Sub CopyToSingleCell()
Dim lRow, RowIndex As Long
Dim src As Worksheet, dest As Worksheet
Dim rng As Range

    Set src = ThisWorkbook.Worksheets("sheet1")
    Set dest = ThisWorkbook.Worksheets("sheet2")
    lRow = src.Range("D8").End(xlDown).Row

        For Each rng In src.Range("D8:D" & lRow)
            dest.Range("B4") = dest.Range("B4") & " " & rng.Value
        Next rng

    Set src = Nothing
    Set dest = Nothing
End Sub

答案 1 :(得分:0)

非常感谢您的分享。

我已经测试了代码,但似乎D8:D10的三个值(在工作表1中)同时出现在B4(在工作表2中)中,但不是一一出现。

请问是否还有其他可能的解决方法。

再次感谢!