通过循环表中的单元格与第3范围中的另一个范围进行复制(连接)

时间:2018-02-02 16:44:10

标签: excel vba

我正在尝试遍历一个表并拉出单元格数据并将其与一个Range组合以创建一个平坦(第三)范围。

表1

  

样品A

     

样本B

表2

  

123

     

456

期望的结果

  

样品A 123

     

样品A 456

     

样本B 123

     

样本B 456

Sub LoopRange()
Dim MyRange As Range
Dim MyCell As Range
Dim lRow As Long
Set sht = Sheet25
lRow = sht.ListObjects("PlanName").Range.Rows.Count

Set MyRange = Sheet25.Range("i3:i10")
Set MyCell = Sheet25.Range(G3, "lRow")

For Each MyCell In MyRange
MyCell.Copy (k3) + MyRange.Copy(k3)
Next MyCell
End Sub

1 个答案:

答案 0 :(得分:0)

看看这个:我把输出放在第J列。

Sub LoopRange()

    Dim MyRange As Range
    Dim MyRange2 As Range
    Dim lRow As Long
    Dim sheet25 As Worksheet
    Dim currentRow As Integer



    Set sheet25 = ThisWorkbook.Worksheets("Sheet25")
    lRow = sht.ListObjects("PlanName").Range.Rows.Count
    currentRow = 12
    Set MyRange = sheet25.Range("i3:i10")
    Set MyRange2 = sheet25.Range("G3:G" & lRow)

    For Each mCell In MyRange

        For Each iCell In MyRange2
            Cells(currentRow, 10) = mCell.Value & " " & iCell.Value
            currentRow = currentRow + 1
        Next iCell

    Next mCell

End Sub