[英]VBA Run Time Error 1004 Range of Object_Global Failed
I am unsure what is wrong with my code. 我不确定我的代码有什么问题。 I thought that I set the range correctly but maybe not.
我以为我正确设置了范围,但也许没有。 I did try to set multiple ranges.
我确实尝试设置多个范围。 The error is on the last main row of code "Range("CL_Paid").Paste"
错误出现在代码“ Range(“ CL_Paid”)。Paste“的最后主行上
Sub test()
Dim HR_Paid, HR_Unpaid, E_Paid, E_Unpaid, SE_Paid, SE_Unpaid, CL_Paid, CL_Unpaid As Range
Set HR_Paid = Sheets("Data").Range("A1:L49")
Set HR_Unpaid = Sheets("Data").Range("A50:L99")
Set E_Paid = Sheets("Data").Range("A100:L149")
Set E_Unpaid = Sheets("Data").Range("A150:L199")
Set SE_Paid = Sheets("Data").Range("A200:L249")
Set SE_Unpaid = Sheets("Data").Range("A250:L299")
Set CL_Paid = Sheets("Data").Range("A300:L349")
Set CL_Unpaid = Sheets("Data").Range("A350:L399")
With Sheets("Data")
.Select
ViewMode = ActiveWindow.View
ActiveWindow.View = xlNormalView
.DisplayPageBreaks = False
Firstrow = .UsedRange.Cells(1).Row
Lastrow = .UsedRange.Rows(.UsedRange.Rows.Count).Row
For Lrow = Lastrow To Firstrow Step -1
With .Cells(Lrow, "D")
If Not IsError(.Value) Then
If .Value = ("CL") Then .EntireRow.Cut
Range("CL_Paid").Paste
End If
End With
Next Lrow
End With
End Sub
Did you intentionally use the one-line version of the If...Then
statement? 您是否有意使用
If...Then
语句的单行版本?
The indentation of the line below the If
looks as though you think it relates to the If
in some way. If
下方的行的缩进看起来好像您认为它在某种程度上与If
有关。 You have this: 你有这个:
If .Value = ("CL") Then .EntireRow.Cut
Range("CL_Paid").Paste
which has the same effect as this: 具有与此相同的效果:
If .Value = ("CL") Then
.EntireRow.Cut
End If
Range("CL_Paid").Paste
when I suspect that you really want this: 当我怀疑您真的想要这个:
If .Value = ("CL") Then
.EntireRow.Cut
CL_Paid.Paste
End If
You are therefore trying to Paste
regardless of whether or not the Cut
ever happened. 因此,无论
Cut
是否发生,您都尝试Paste
。
Details on the different versions of the If...Then
statement are here 有关
If...Then
语句不同版本的详细信息,请参见此处
CLPaid is already a Range
so use Cl_Paid.Paste
. CLPaid已经是一个
Range
因此请使用Cl_Paid.Paste
。 However, Paste applies to a Worksheet
, not a Range
, so you might consider PasteSpecial
. 但是,Paste适用于
Worksheet
,而不适用于Range
,因此您可以考虑PasteSpecial
。 Alternatively, 或者,
.EntireRow.Cut CL_Paid
will Cut and Paste (to a Destination) in one step, without using the clipboard. 只需一步就可以剪切和粘贴(到目标),而无需使用剪贴板。
Note also that 另请注意
Dim HR_Paid, HR_Unpaid, E_Paid, etc As Range
does not (in VBA) define all these are Range references. 没有(在VBA中)定义所有这些都是Range引用。 They are all Variant 's except for the last one.
除了最后一个,它们都是Variant 。 You would have to use:
您将必须使用:
Dim HR_Paid As Range, HR_Unpaid As Range, E_Paid As Range, etc
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.