Search code examples
excelvbanamednamed-ranges

VBA refering to a variable named range to move values


Code works as expected until the last line where I attempt to move values from one range to another at which point I'm getting a "run time error 1004", so must be doing something wrong.

the range "NewRng" does produce the correct string "$A$1883:$R$2105" which if entered manually into the last line (replacing the "NewRng" reference) it produces the correct results.

Thanks in advance

    Dim NevwebLR As String
    With Sheets("NevWeb file")
    NevwebLR = .Cells(.Rows.Count, "A").End(xlUp).Row
    End With

    Dim DropShipLR As String
    With Sheets("Drop Shipments")
    DropShipLR = .Cells(.Rows.Count, "A").End(xlUp).Row
    End With

    Dim NevwebLR1 As String
    NevwebLR1 = NevwebLR + 1

    Dim dropshipglue As Long
    dropshipglue = Val(NevwebLR) + Val(DropShipLR)

    Dim rng1 As Range, rng2 As Range
    Dim NewRng As Range

        With ThisWorkbook.Sheets("Results")
            Set rng1 = .Range("A" & NevwebLR1)
            Set rng2 = .Range("R" & dropshipglue)

            Set NewRng = .Range(rng1, rng2)

            Debug.Print NewRng.Address
        End With



    Sheets("results").Range(NewRng).Value = Sheets("Drop Shipments").Range("A1:R" & DropShipLR).Value

Solution

  • You have your destination range already as Range-object, so change the last line to

    NewRng.Value = Sheets("Drop Shipments").Range("A1:R" & DropShipLR).Value