本文介绍了逐个复制范围值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两张表,其中Sheet1的L6将具有从表格2 B:B的范围内的数据。我需要一个宏,一个命令就是选择B:B,然后将第一个单元格值(B1)复制到sheet1的L6,下一次单击它将选择B2并将值复制到L6,达到最后一个值或空白单元格将显示一条消息no value。

解决方案
  Sub MyMacro b $ b静态curr As Range 
如果curr is Nothing然后设置curr = Worksheets(Sheet2)。范围(B1)

如果curr.Value = vbNullString然后
MsgBoxNo Value
Exit Sub
End If

'虽然Select在VBA中未被启用,但由于在问题陈述中需要...
工作表(Sheet2)。激活
curr.Select
工作表(Sheet1)。Range(L6)。Value = curr.Value
设置curr = curr.Offset(1 ,0)
End Sub


I have two sheets, in which L6 of Sheet 1 will have a data from a range in sheet 2 B:B. I need a macro, upon a command it shall select B:B, then copy first cell value (B1) to L6 of sheet1, upon next click it shall select B2 and copy the value to L6, upon reaching the last value or a blank cell it shall display a message "no value".

解决方案
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

这篇关于逐个复制范围值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-21 11:59