简体   繁体   English

VBA引用一个名为range的变量来移动值

[英]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. 代码按预期工作,直到我尝试将值从一个范围移动到另一范围的最后一行,这时我遇到“运行时错误1004”,因此必须做错了什么。

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. 范围“ NewRng”确实会产生正确的字符串“ $ A $ 1883:$ R $ 2105”,如果手动将其输入到最后一行(替换“ NewRng”引用),它将产生正确的结果。

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

您的目标范围已作为Range -object,因此将最后一行更改为

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM