Copy range values one by one

 Question:

  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".




 Answers: 


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

Commentaires

Posts les plus consultés de ce blog

XAJAX with PHP – The future of web development

XAJAX with PHP – The future of web development

Database connection pooling in ADO.Net