Print out a simple range

  • I have data in each cell of "C5:E7"
    Error code:

    Quote

    "Application-defined or object-defined error"


    Code
    Private Sub CommandButton1_Click()
        Dim rng1 As Range, rng2 As Range
        string1 = "C5:E7"
        string2 = "C12:E14"
        Set rng1 = Range(string1)
        Set rng2 = Range(string2)
        Sheets(1).Range(rng2) = Sheets(1).Range(rng1)
    End Sub
  • Re: Print out a simple range


    rng1 and rng2 are Range Objects that you haven't qualified so they pertain to the active sheet at the time of initialization.


    This is what I believe you are trying to achieve:


    Code
    Private Sub CommandButton_Click()
        Dim rng1 As Range, rng2 As Range
        string1 = "C5:E7"     string2 = "C12:E14"
        Set rng1 = Sheets(2).Range(string1)
        Set rng2 = Sheets(1).Range(string2)
        rng2.Value = rng1.Value
    End Sub


    also, this:

    Code
    Sheets(1).Range(rng1)

    will never work.


    At this point rng1 is already a range object, so you wouldn't qualify it with a sheet object or refer to it using the Range() method.

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!