0

我不确定我的代码有什么问题。我以为我设置了正确的范围,但也许不是。我确实尝试设置多个范围。错误出现在代码“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
4

2 回答 2

2

您是否故意使用单行版本的If...Then语句?

线条下方的缩进If看起来好像您认为它与If某种方式有关。你有这个:

If .Value = ("CL") Then .EntireRow.Cut
    Range("CL_Paid").Paste

这与此具有相同的效果:

If .Value = ("CL") Then
    .EntireRow.Cut
End If

Range("CL_Paid").Paste

当我怀疑你真的想要这个时:

If .Value = ("CL") Then
    .EntireRow.Cut
    CL_Paid.Paste
End If

因此,您正在尝试Paste无论是否Cut曾经发生过。

有关不同版本If...Then声明的详细信息,请参见此处

于 2013-07-01T22:25:48.813 回答
1

CLPaid is already a Range so use Cl_Paid.Paste. However, Paste applies to a Worksheet, not a Range, so you might consider 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. They are all Variant's except for the last one. You would have to use:

Dim HR_Paid As Range, HR_Unpaid As Range, E_Paid As Range, etc
于 2013-07-01T21:55:40.467 回答