将范围中的单元格复制到列

时间:2011-02-09 18:29:57

标签: excel excel-vba named-ranges vba

我有MyRange = A1:C3

范围内的当前值如下:

A1 = a,B1 = d,C1 = f A2 = b,B2 = e,C2 =“” A3 = c,B3 =“”,C3 =“”

范围内的空白细胞可以变化。

如何从MyRange复制(使用vba)非空值并将它们全部粘贴在一起让我们对AA列说?

例如: AA1 =一 AA2 = B AA3 = C AA4 = d AA5 = E AA6 = F

再次感谢你们:-) 保罗

1 个答案:

答案 0 :(得分:0)

遍历MyRange中的所有单元格,如果单元格不是“”将值复制到下一个目标单元格

Sub test()
    Dim MyRange as Range
    Dim TargetCell as Range
    Dim rw as Range
    Dime cl as Range

    Set MyRange = ActiveWorkbook.Names("MyRange").RefersToRange
    Set TargetCell = Range("AA1") 
    For each rw in MyRange.Columns
        For each cl in rw.Cells
            If cl.value <> "" then
                TargetCell = cl.value
                Set TargetCell = TargetCell.Offset(1,0)
            End If
        Next
    Next

End Sub