Macro Crashing Excel, Never used to until I added range part

domiereavron

New Member
Joined
Apr 11, 2013
Messages
22
Every time I run this macro it causes my Excel to crash. I am trying to take data from one sheet and re-arrange the data on a different sheet. I want to be able to clear all the data on the second sheet before it starts copying the data over.

Any ideas on how I can fix this?

Code:
Sub ConvertSingleLine()
      Dim x As Long
      Dim Sheet1TotalRows As Long
      Dim Sheet2TotalRows As Long
      Dim Sheet2Row As Long
      
      
      Sheet1TotalRows = Sheets(1).Cells.SpecialCells(xlCellTypeLastCell).Row
      Sheet2TotalRows = Sheets(2).Cells.SpecialCells(xlCellTypeLastCell).Row
      Sheets(2).Range("A2:AH" & Sheet2TotalRows).Clear
      Sheet2Row = 1
  
      For x = 1 To Sheet1TotalRows
        
        Select Case Sheets(1).Cells(x, 1).Value
            Case "   Tier: Core Certified", "   Tier: SPG Lite", "   Tier: SPG Classic", "   Tier: Service Lite"
            Sheet2Row = Sheet2Row + 1
            Sheets(2).Cells(Sheet2Row, 2).Value = Sheets(1).Cells(x, 1).Value
            Sheets(2).Cells(Sheet2Row, 1).Value = Sheets(1).Cells(x - 1, 1).Value
            Sheets(2).Cells(Sheet2Row, 3).Value = Sheets(1).Cells(x - 1, 2).Value
            Sheets(2).Cells(Sheet2Row, 4).Value = Sheets(1).Cells(x - 1, 3).Value
            Sheets(2).Cells(Sheet2Row, 5).Value = Sheets(1).Cells(x - 1, 4).Value
            Sheets(2).Cells(Sheet2Row, 6).Value = Sheets(1).Cells(x - 1, 5).Value
            Sheets(2).Cells(Sheet2Row, 7).Value = Sheets(1).Cells(x, 2).Value
            Sheets(2).Cells(Sheet2Row, 8).Value = Sheets(1).Cells(x, 3).Value
            Sheets(2).Cells(Sheet2Row, 9).Value = Sheets(1).Cells(x, 4).Value
            Sheets(2).Cells(Sheet2Row, 10).Value = Sheets(1).Cells(x, 5).Value
            Sheets(2).Cells(Sheet2Row, 11).Value = Sheets(1).Cells(x + 1, 2).Value
            Sheets(2).Cells(Sheet2Row, 12).Value = Sheets(1).Cells(x + 1, 3).Value
            Sheets(2).Cells(Sheet2Row, 13).Value = Sheets(1).Cells(x + 1, 4).Value
            Sheets(2).Cells(Sheet2Row, 14).Value = Sheets(1).Cells(x + 1, 5).Value
            Sheets(2).Cells(Sheet2Row, 15).Value = Sheets(1).Cells(x + 2, 2).Value
            Sheets(2).Cells(Sheet2Row, 16).Value = Sheets(1).Cells(x + 2, 3).Value
            Sheets(2).Cells(Sheet2Row, 17).Value = Sheets(1).Cells(x + 2, 4).Value
            Sheets(2).Cells(Sheet2Row, 18).Value = Sheets(1).Cells(x + 2, 5).Value
            Sheets(2).Cells(Sheet2Row, 19).Value = Sheets(1).Cells(x + 3, 2).Value
            Sheets(2).Cells(Sheet2Row, 20).Value = Sheets(1).Cells(x + 3, 3).Value
            Sheets(2).Cells(Sheet2Row, 21).Value = Sheets(1).Cells(x + 3, 4).Value
            Sheets(2).Cells(Sheet2Row, 22).Value = Sheets(1).Cells(x + 3, 5).Value
            Sheets(2).Cells(Sheet2Row, 23).Value = Sheets(1).Cells(x + 4, 2).Value
            Sheets(2).Cells(Sheet2Row, 24).Value = Sheets(1).Cells(x + 4, 3).Value
            Sheets(2).Cells(Sheet2Row, 25).Value = Sheets(1).Cells(x + 4, 4).Value
            Sheets(2).Cells(Sheet2Row, 26).Value = Sheets(1).Cells(x + 4, 5).Value
            Sheets(2).Cells(Sheet2Row, 27).Value = Sheets(1).Cells(x + 5, 2).Value
            Sheets(2).Cells(Sheet2Row, 28).Value = Sheets(1).Cells(x + 5, 3).Value
            Sheets(2).Cells(Sheet2Row, 29).Value = Sheets(1).Cells(x + 5, 4).Value
            Sheets(2).Cells(Sheet2Row, 30).Value = Sheets(1).Cells(x + 5, 5).Value
            Sheets(2).Cells(Sheet2Row, 31).Value = Sheets(1).Cells(x + 6, 2).Value
            Sheets(2).Cells(Sheet2Row, 32).Value = Sheets(1).Cells(x + 6, 3).Value
            Sheets(2).Cells(Sheet2Row, 33).Value = Sheets(1).Cells(x + 6, 4).Value
            Sheets(2).Cells(Sheet2Row, 34).Value = Sheets(1).Cells(x + 6, 5).Value
                
        End Select
      
      Next

End Sub

Thanks,
 

Some videos you may like

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.

rjwebgraphix

Well-known Member
Joined
May 25, 2010
Messages
588
Duplicated data based on code, ran and didn't crash on me. However.....

I would use .clearcontents instead of .clear

.clear will clear formats and everything where .clearcontents only clears the contents of a cell, not anything else attached to the cell. Could possibly have an issue with .clear however....

There has been an issue I've seen before where something becomes corrupt in the workbook and code starts doing funky things. If the above does not fix the issue then....

My suggestions fwiw....

1. copy code off to notepad and save it.
2. reboot PC
3. start a fresh new workbook and bring your data in.
4. Copy your code in from notebook

Run it and see if that resolves the issue. Don't bring anything in directly from the old workbook. If need be, save the old workbook as a csv and import it from the csv to the new workbook.

If it is still crashing after that. Uninstall and reinstall Excel.

If it is still crashing after that it's probably time to find a new job. :) Just kidding.
 

Watch MrExcel Video

Forum statistics

Threads
1,122,691
Messages
5,597,562
Members
414,156
Latest member
WDMix

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
Top