Excel insists on trying to repair a super basic workbook and worksheet. What can I try or look for?

OaklandJim

Well-known Member
Joined
Nov 29, 2018
Messages
833
Office Version
  1. 365
Platform
  1. Windows
Team Excel. What am I doing wrong? This is not the first time I've gotten this type of error but this time it seems goofy. That said, there is almost always an explanation. Like I said in the title, the workbook I have is totally basic. It contains three very basic subs, a small amount of data, and nothing exotic. It is a workbook I saved so I don't have to remember how to do a sort (I'm old and my memory is not that great). The dialogs involved are shown in pictures.

_Sorting.xlsm
CDEFGHIJKL
2Sorted DataUnsorted Data
3CategoryTypeNumberCategoryTypeNumber
4CarsBody4StuffTruck8
5ColdBody2ToysFloor0
6MoreChair8MoreChair8
7WordsCow5GirlsDoor9
8JunkDeer2JunkDeer2
9GirlsDoor9WordsCow5
10ZooDoor6ZooDoor6
11AppleDoor1LessFood0
12ToysFloor0CarsBody4
13LessFood0BoysSeat6
14BoysSeat6AppleDoor1
15StuffTruck8ColdBody2
Sheet1


VBA Code:
Option Explicit

Sub ResetThreeColumns()

    With Worksheets("Sheet2")
        .Range("J4:L15").Copy .Range("C4")
    End With

End Sub

VBA Code:
Option Explicit

Sub TestCallerSort()

    Dim rSortRange As Range

    Set rSortRange = ThisWorkbook.Worksheets("Sheet2") _
        .Range("ThreeColumnRangeToSort_WithHeader")
        
    Call BasicSort(rSortRange, 2, 2)

End Sub


Sub BasicSort( _
    prSortRangeWithHeaders As Range, _
    piKey1Col As Long, _
    Optional piKey2Col As Long = 1, _
    Optional pbAscending As Boolean = True)
    
        Dim vAscending As Variant
        
        If pbAscending _
         Then
            vAscending = xlAscending
        Else
            vAscending = xlDescending
        End If

        prSortRangeWithHeaders.Parent.Sort.SortFields.Clear

        prSortRangeWithHeaders.Sort _
            Key1:=prSortRangeWithHeaders.Cells(2, piKey1Col), _
            Key2:=prSortRangeWithHeaders.Cells(2, piKey2Col), _
            Order1:=vAscending, _
            Header:=xlYes
End Sub
 

Attachments

  • Dialog1.png
    Dialog1.png
    5.8 KB · Views: 6
  • Dialog2.png
    Dialog2.png
    12 KB · Views: 6

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Do you have any other code in that workbook?
You should not get that error when using Range.Sort, only when using Worksheet.Sort.
Also the sheet2.xml has nothing to do with the sheet name (or code name) it's the sheet index number, so it means the 2nd sheet in the workbook.
 
Upvote 0
Thanks very much for the response!

That is the only code.

I guess that I misunderstand this

You should not get that error when using Range.Sort, only when using Worksheet.Sort.

If I change code to clear the sort by referring to the range and not the range's parent I get an "Unanble to get the Sort property of the Range Class error.

VBA Code:
prSortRangeWithHeaders.Parent.Sort.SortFields.Clear

I removed Sheet2 -- which had totally unexotic data -- and I got the same dialogs with only Sheet1. I deleted all rows and columns adjacent to the totally simple data and now the issue is resolved. I still wonder what I did in Sheet 2 to cause that error. I hope to avoid it in the future.
 
Upvote 0
This line of code is not needed as you are using Range.Sort
VBA Code:
prSortRangeWithHeaders.Parent.Sort.SortFields.Clear
Also as I said before the name of the sheet has nothing to do with sheet2.xml
 
Upvote 0

Forum statistics

Threads
1,215,109
Messages
6,123,137
Members
449,098
Latest member
Doanvanhieu

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