逐个复制范围值

时间:2015-11-06 08:08:40

标签: excel vba excel-vba

我有两张纸,其中纸张1的L6将具有来自纸张2 B:B中的范围的数据。我需要一个宏,根据命令选择B:B,然后将第一个单元格值(B1)复制到sheet1的L6,下次单击时应选择B2,并在达到最后一个值或空白时将值复制到L6它应该显示一条消息"没有值"。

1 个答案:

答案 0 :(得分:0)

Sub MyMacro()
    Static curr As Range
    If curr Is Nothing Then Set curr = Worksheets("Sheet2").Range("B1")

    If curr.Value = vbNullString Then
       MsgBox "No Value"
       Exit Sub
    End If

    ' although Select is unrecommneded in VBA, but since it is required in the problem statement...
    Worksheets("Sheet2").Activate
    curr.Select
    Worksheets("Sheet1").Range("L6").Value = curr.Value
    Set curr = curr.Offset(1, 0)
End Sub