简体   繁体   中英

excel vba copy and paste to fix “#REF” cells

I'm trying to make a checkbook register on excel 365. I am trying to add the feature of deleting a row(transaction) but when I run my vba code it errors out with the following: Run-time error '1004': Paste method of worksheet class failed.

When I run my code to delete a row the balance column fills up with "#REF". My effort to fix this by copying the balance column and pasting it back after the row is deleted isn't working. Balance column still fills up with "#REF"s. Here is the code i'm using to copy and paste the balance column:

Sub DeleteTransactionRectangle_Click()
    Dim deletedRow

    Dim rng As Range
    Set rng = Selection

    deletedRow = rng.Row

    MsgBox (deletedRow)
    Worksheets("Register").Range("I:I").Copy 
    rng.EntireRow.Delete

    ActiveSheet.Paste Destination:=Worksheets("Register").Range("I:I")

End Sub

Anyone have any ideas to make this work.

You have dim 'rngg' but set 'rng' - should these not both be the same?

When you delete the row, it seems the copied information is lost from the clipboard. Instead, you could copy the formula from column I in the preceding row, and paste it back into the row you deleted:

rng.EntireRow.Delete
ActiveSheet.Range("I" & deletedRow - 1).Copy
ActiveSheet.Range("I" & deletedRow).Select
ActiveSheet.Paste

Deleting Rows Containing Formulas

在此处输入图像描述

Tip

In design mode, right-click on the command button and select Properties . Change TakeFocusOnClick to False , so when you click the command button it doesn't get selected (focus) but the selection on your sheet stays the same.

Formula

Your formula turns out to be bad when deleting rows. You could change it to:

=SUM(H$1:H2)-SUM(F$1:F2)

which is 'deleting columns proof'.

Improve Code

To Study

Change the values in the constants section to fit your needs.

Option Explicit

Sub DeleteTransactionRectangle_Click()

    Const rowFR As Long = 2       ' First Row of Data
    Const colFR As Long = 9       ' Formula Column

    Dim rowLR As Long             ' Last Row of Data
    Dim rng As Range              ' Current Area (For Each Control Variable)
    Dim rngTransAction As Range   ' Transaction (Column) Range
    Dim rngUnion As Range         ' Union Range
    Dim rngFinal As Range         ' Intersection (Final) Range

    ' Calculate Last Row of Data.
    rowLR = Columns(colFR).Find(What:="*", LookIn:=xlFormulas, _
      SearchDirection:=xlPrevious).Row

    ' From the selection, create Union Range which only contains cells
    ' of Formula Column, which will later be compared
    ' to Transaction (Column) Range.
    For Each rng In Selection.Areas
        If Not rngUnion Is Nothing Then
            Set rngUnion = Union(rngUnion, Cells(rng.Row, colFR) _
              .Resize(rng.Rows.Count))
        Else
            Set rngUnion = Cells(rng.Row, colFR).Resize(rng.Rows.Count)
        End If
    Next

    ' Define Transaction (Column) Range.
    Set rngTransAction = Cells(rowFR, colFR).Resize(rowLR - rowFR + 1)

    ' Create Intersection (Final) Range which will 'eliminate'
    ' all 'non-valid' areas of the Selection Range. 'Non-valid' areas are
    ' the ones before First Row of Data and after Last Row of Data.
    If Not rngUnion Is Nothing Then Set rngFinal _
      = Intersect(rngUnion, rngTransAction)

    ' Delete rows of Intersection (Final) Range.
    If Not rngFinal Is Nothing Then rngFinal.EntireRow.Delete

End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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