简体   繁体   English

VBA运行时错误1004 Object_Global的范围失败

[英]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.

相关问题 VBA运行时错误1004-object_Global的范围失败-IF公式 - VBA Run Time Error 1004 - Range of object_Global failed - IF Formula VBA运行时错误1004的object_global范围失败 - VBA Runtime error 1004 range of object_global failed 在带有命名范围的Excel中搜索时,object_global的运行时错误1004方法范围失败 - Run time error 1004 method range of object_global failed on a search in Excel with a named range 运行时错误'1004'-VBA中对象'_Global'的方法'Range'失败 - Run-time error '1004' - Method 'Range' of object'_Global' failed in VBA Excel VBA 运行时错误'1004'对象'_Global'的方法'范围'失败 - Excel VBA Run-Time Error '1004' Method 'Range' of object'_Global' Failed VBA-运行时错误'1004'-对象'_Global'的方法'Range'失败 - VBA - Run-time error '1004' - Method 'Range' of object'_Global' failed VBA运行时错误'1004':对象'_Global'的方法'Range'失败 - VBA Run-time error '1004': Method 'Range' of object '_Global' Failed 损坏的Excel VBA宏运行时错误'1004':对象'_Global'的方法'Range'失败 - Broken Excel VBA Macro Run-time error '1004': Method 'Range' of object '_Global' failed _Global的VBA错误1004对象范围失败 - VBA Error 1004 Object Range failed for _Global 运行时错误 '1004' 'Range' of object'_Global' 失败 - Run-time error '1004' 'Range' of object'_Global' failed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM