Automation Error/Excel Crashes during Cut Copy mode

adr0427

New Member
Joined
Apr 21, 2016
Messages
9
Office Version
  1. 365
Platform
  1. Windows
Hello,

After debugging the code I was able to find where the automation error/excel crashes. I believe it is due to the numerous CutCopy modes in the code snippet. How can I make this snippet of code more efficient. I am basically moving a cell from one column to another based on a value. Thank you in advance for the help!

VBA Code:
For i = 2 To MLastRow               'Categorizing p, e and m codes to the right columns.
Rsrc = Sheets("Resources Budget").Cells(i, "A").Value
    For j = 2 To MLastRow1
    If Rsrc = Sheets("Rsrccodes").Cells(j, "G").Value And Sheets("Rsrccodes").Cells(j, "F").Value <> "29910099" Then
    Sheets("Resources Budget").Activate
    Sheets("Resources Budget").Range(Cells(i, "A"), Cells(i, "G")).Copy
    Sheets("Budget Upload").Activate
    Sheets("Budget Upload").Range(Cells((MLastRow2 + i), "A"), Cells((MLastRow2 + i), "G")).Select
    ActiveSheet.Paste
    Application.CutCopyMode = False
        If InStr(Sheets("Rsrccodes").Cells(j, "F").Value, "2B04") > 0 Then
            Sheets("Budget Upload").Cells((MLastRow2 + i), "G").Cut
            Sheets("Budget Upload").Cells((MLastRow2 + i), "J").Select
            ActiveSheet.Paste
            Application.CutCopyMode = False
            'Below attempts not working, going blank in Column J :(
            'Sheets("Budget Upload").Range(Cells((MLastRow2 + i), "G"), Cells((MLastRow2 + i), "G")).Value = Sheets("Budget Upload").Range(Cells((MLastRow2 + i), "J"), Cells((MLastRow2 + i), "J")).Value
            'Sheets("Budget Upload").Cells((MLastRow2 + i), "G").Value = Sheets("Budget Upload").Cells((MLastRow2 + i), "J").Value
 
        Else
            Sheets("Budget Upload").Cells((MLastRow2 + i), "G").Cut
            Sheets("Budget Upload").Cells((MLastRow2 + i), "N").Select
            ActiveSheet.Paste
            Application.CutCopyMode = False
        End If
    ElseIf Rsrc = Sheets("Rsrccodes").Cells(j, "G").Value And Sheets("Rsrccodes").Cells(j, "F").Value = "29910099" Then
    Sheets("Resources Budget").Activate
    Sheets("Resources Budget").Range(Cells(i, "A"), Cells(i, "G")).Copy
    Sheets("Budget Upload").Activate
    Sheets("Budget Upload").Range(Cells((MLastRow2 + i), "A"), Cells((MLastRow2 + i), "G")).Select
    ActiveSheet.Paste
    Sheets("Budget Upload").Cells((MLastRow2 + i), "G").Cut
    Sheets("Budget Upload").Cells((MLastRow2 + i), "K").Select
    ActiveSheet.Paste
    Application.CutCopyMode = False
    End If
    Next j
    Application.CutCopyMode = False
    
Next i
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
You have a bunch of 'Select's and 'Activate's in there. Try replacing the code you provided with the following to see if that helps you out:

VBA Code:
For i = 2 To MLastRow               'Categorizing p, e and m codes to the right columns.
Rsrc = Sheets("Resources Budget").Cells(i, "A").Value
    For j = 2 To MLastRow1
    If Rsrc = Sheets("Rsrccodes").Cells(j, "G").Value And Sheets("Rsrccodes").Cells(j, "F").Value <> "29910099" Then
    Sheets("Resources Budget").Range(Cells(i, "A"), Cells(i, "G")).Copy Sheets("Budget Upload").Range(Cells((MLastRow2 + i), "A"), Cells((MLastRow2 + i), "G"))
    Application.CutCopyMode = False
        If InStr(Sheets("Rsrccodes").Cells(j, "F").Value, "2B04") > 0 Then
        Sheets("Budget Upload").Cells((MLastRow2 + i), "G").Cut Sheets("Budget Upload").Cells((MLastRow2 + i), "J")
            Application.CutCopyMode = False
            'Below attempts not working, going blank in Column J :(
            'Sheets("Budget Upload").Range(Cells((MLastRow2 + i), "G"), Cells((MLastRow2 + i), "G")).Value = Sheets("Budget Upload").Range(Cells((MLastRow2 + i), "J"), Cells((MLastRow2 + i), "J")).Value
            'Sheets("Budget Upload").Cells((MLastRow2 + i), "G").Value = Sheets("Budget Upload").Cells((MLastRow2 + i), "J").Value
 
        Else
        Sheets("Budget Upload").Cells((MLastRow2 + i), "G").Cut Sheets("Budget Upload").Cells((MLastRow2 + i), "N")
            Application.CutCopyMode = False
        End If
    ElseIf Rsrc = Sheets("Rsrccodes").Cells(j, "G").Value And Sheets("Rsrccodes").Cells(j, "F").Value = "29910099" Then
    Sheets("Resources Budget").Range(Cells(i, "A"), Cells(i, "G")).Copy Sheets("Budget Upload").Range(Cells((MLastRow2 + i), "A"), Cells((MLastRow2 + i), "G"))
    Sheets("Budget Upload").Cells((MLastRow2 + i), "G").Cut Sheets("Budget Upload").Cells((MLastRow2 + i), "K")
    Application.CutCopyMode = False
    End If
    Next j
    Application.CutCopyMode = False
    
Next i

I have not tested this, but I think I got it right as far as the replacements I did. Let us know how it goes.
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,207
Members
452,618
Latest member
Tam84

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