Macros set up to move line NOT WORKING

Ethel

New Member
Joined
Feb 2, 2023
Messages
14
Office Version
  1. 365
Platform
  1. Windows
Hi,

I currently have a Macros running that moves a line from one sheet to another thats worked for months, i must have done something because now its not working properly! The line is disappearing from the 'active' sheet but not appearing on the 'completed sheet'.

Macros Module is-
VBA Code:
Sub Cheezy()
'Updated by Kutools for Excel 2017/8/28
    Dim xRg As Range
    Dim xCell As Range
    Dim I As Long
    Dim J As Long
    Dim K As Long
    I = Worksheets("All Rug Orders").UsedRange.Rows.Count
    J = Worksheets("Completed Rug Orders").UsedRange.Rows.Count
    If J = 1 Then
       If Application.WorksheetFunction.CountA(Worksheets("Completed Rug Orders").UsedRange) = 0 Then J = 0
    End If
    Set xRg = Worksheets("All Rug Orders").Range("R1:R" & I)
    On Error Resume Next
    Application.ScreenUpdating = False
    For K = 1 To xRg.Count
        If CStr(xRg(K).Value) = "Yes" Then
            xRg(K).EntireRow.Copy Destination:=Worksheets("Completed Rug Orders").Range("A" & J + 1)
            xRg(K).EntireRow.Delete
            If CStr(xRg(K).Value) = "Yes" Then
                K = K - 1
            End If
            J = J + 1
        End If
    Next
    Application.ScreenUpdating = True
End Sub


VBA Code is-
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim z As Long
Dim xval As String
On Error Resume Next
If Intersect(Target, Range("R:R")) Is Nothing Then Exit Sub
Application.EnableEvents = False
For z = 1 To Target.Count
    If Target(z).Value > 0 Then
        Call Cheezy
    End If
Next
Application.EnableEvents = True

End Sub



1677061468965.png
 
Last edited by a moderator:

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
You are using the statement On Error Resume Next to turn off error checking for a large chunk of code. Personally, I am very careful about how and when I use that statement because errors are useful to tell you what & where the problem is (and you should turn error checking back on using On Error GoTo 0 as soon as possible).

Try temporarily removing statement On Error Resume Next, then re-run to get more information about the problem.
 
Upvote 0
Hi Ethel. There's a couple of things you could trial. Remove the On Error Resume Next and see if there are any errors. The row copy I think should look like this...
Code:
Rows(xRg(K)).EntireRow.Copy Destination:=Worksheets("Completed Rug Orders").Range("A" & J + 1)
Rows(xRg(K)).EntireRow.Delete
HTH. Dave
 
Upvote 0
Hi Ethel. There's a couple of things you could trial. Remove the On Error Resume Next and see if there are any errors. The row copy I think should look like this...
Code:
Rows(xRg(K)).EntireRow.Copy Destination:=Worksheets("Completed Rug Orders").Range("A" & J + 1)
Rows(xRg(K)).EntireRow.Delete
HTH. Dave
No errors when removing On Error Resume Next
But adding the Rows xRg(K)) etc. the entire row.copy has become highlighted in error
 
Upvote 0
Maybe..
Code:
Worksheets("All Rug Orders").Rows(xRg(K)).EntireRow.Copy Destination:=Worksheets("Completed Rug Orders").Range("A" & J + 1)
 Worksheets("All Rug Orders").Rows(xRg(K)).EntireRow.Delete
Dave
 
Upvote 0
You could also try something a bit different.
VBA Code:
Sub Cheezy()
    Dim xRg As Range, R As Range, rngDel As Range
    Dim J As Long, CopyCnt As Long, PasteCnt As Long
    
    J = Worksheets("Completed Rug Orders").UsedRange.Rows.Count

    If J = 1 Then
        If Application.WorksheetFunction.CountA(Worksheets("Completed Rug Orders").UsedRange) = 0 Then J = 0
    End If

    PasteCnt = J

    With Worksheets("All Rug Orders")
        Set xRg = .Range("R1", .Range("R" & .Rows.Count).End(xlUp))
    End With

    Application.ScreenUpdating = False
    For Each R In xRg
        If Trim(R.Value) = "Yes" Then
            R.EntireRow.Copy Destination:=Worksheets("Completed Rug Orders").Range("A" & J + 1)
            CopyCnt = CopyCnt + 1
            If rngDel Is Nothing Then
                Set rngDel = R.EntireRow
            Else
                Set rngDel = Application.Union(rngDel, R.EntireRow)
            End If
            J = J + 1
        End If
    Next R
    
    If Not rngDel Is Nothing Then
        rngDel.Delete
    End If
    
    ''' Debug code
    PasteCnt = Worksheets("Completed Rug Orders").UsedRange.Rows.Count - PasteCnt
    Debug.Print CopyCnt & " rows were copied" & vbCr & PasteCnt & " rows were pasted"
    'MsgBox CopyCnt & " rows were copied" & vbCr & PasteCnt & " rows were pasted"
    ''' End debug code
    
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
I keep re writing the code. Sometimes it works and the line disappears but it doesn’t come up on the other sheet?
I’m desperate for this to get fixed!
 
Upvote 0
What have you tried? My last code from #6? riv01's code? What were the results without the On error resume next? Dave
 
Upvote 0

Forum statistics

Threads
1,215,026
Messages
6,122,738
Members
449,094
Latest member
dsharae57

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top