VBA 引用名为 range 的变量来移动值
VBA refering to a variable named range to move values
代码按预期工作,直到最后一行我尝试将值从一个范围移动到另一个范围,此时我得到 "run time error 1004",所以一定是做错了什么。
范围 "NewRng" 确实产生了正确的字符串“$A$1883:$R$2105”,如果手动输入到最后一行(替换 "NewRng" 引用)它会产生正确的结果。
提前致谢
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
代码按预期工作,直到最后一行我尝试将值从一个范围移动到另一个范围,此时我得到 "run time error 1004",所以一定是做错了什么。
范围 "NewRng" 确实产生了正确的字符串“$A$1883:$R$2105”,如果手动输入到最后一行(替换 "NewRng" 引用)它会产生正确的结果。
提前致谢
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