Nothing has change so why do I get : Sort method of range class failed.

Romano_odK

Active Member
Joined
Jun 4, 2020
Messages
380
Office Version
  1. 365
Platform
  1. Windows
Good afternoon,

The below code was working fine last year and in the meanwhile nothing has changed expect the domain where I am working in. Can someone tell me why I get this error? "Sort method of range class failed"

Thank you for your time and all the best wishes for 2024.


VBA Code:
Private Sub FilterOK_Click()
  Dim a As Variant, b As Variant
  Dim nc As Long, i As Long, k As Long
 
  nc = Range("ItemsImport").Columns.Count + 1
  a = Range("ItemsImport[[#All],[Afwijking %" & Chr(10) & "Opslag]]").Value
  ReDim b(1 To UBound(a), 1 To 1)
  For i = 1 To UBound(a)
    If LCase(a(i, 1)) = "ok" Or Len(a(i, 1)) = 0 Then
      b(i, 1) = 1
      k = k + 1
    End If
  Next i
  If k > 0 Then
    Application.ScreenUpdating = False
    With Range("ItemsImport[#All]").Resize(, nc)
      .Columns(nc).Value = b
      .Sort Key1:=.Columns(nc), Order1:=xlAscending, Header:=xlYes
      .Offset(1).Resize(k).EntireRow.Delete
      .Columns(nc).EntireColumn.Delete
    End With
    Application.ScreenUpdating = True
  End If
End Sub
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Are you sure nothing has changed? Is the named range ItemsImport pointing to where you expect it to point to?
 
Upvote 0
Works for me.

But try this other approach:

VBA Code:
Private Sub FilterOK_Click()
  Dim tbl As ListObject
  Dim col As ListColumn
  Dim fnd As Range
 
  Set tbl = ActiveSheet.ListObjects("ItemsImport")
  Set col = tbl.ListColumns("Afwijking %" & Chr(10) & "Opslag")
  Set fnd = col.Range.Find("ok", , xlValues, xlWhole, , , False)
 
  Application.ScreenUpdating = False
  If Not fnd Is Nothing Then
    tbl.Range.AutoFilter col.Index, "ok"
    tbl.DataBodyRange.EntireRow.Delete
    ActiveSheet.ShowAllData
  End If
  Application.ScreenUpdating = True
End Sub

😇
 
Upvote 0
Works for me.

But try this other approach:

VBA Code:
Private Sub FilterOK_Click()
  Dim tbl As ListObject
  Dim col As ListColumn
  Dim fnd As Range
 
  Set tbl = ActiveSheet.ListObjects("ItemsImport")
  Set col = tbl.ListColumns("Afwijking %" & Chr(10) & "Opslag")
  Set fnd = col.Range.Find("ok", , xlValues, xlWhole, , , False)
 
  Application.ScreenUpdating = False
  If Not fnd Is Nothing Then
    tbl.Range.AutoFilter col.Index, "ok"
    tbl.DataBodyRange.EntireRow.Delete
    ActiveSheet.ShowAllData
  End If
  Application.ScreenUpdating = True
End Sub

😇
Now I get script out of range.
Works for me.

But try this other approach:

VBA Code:
Private Sub FilterOK_Click()
  Dim tbl As ListObject
  Dim col As ListColumn
  Dim fnd As Range
 
  Set tbl = ActiveSheet.ListObjects("ItemsImport")
  Set col = tbl.ListColumns("Afwijking %" & Chr(10) & "Opslag")
  Set fnd = col.Range.Find("ok", , xlValues, xlWhole, , , False)
 
  Application.ScreenUpdating = False
  If Not fnd Is Nothing Then
    tbl.Range.AutoFilter col.Index, "ok"
    tbl.DataBodyRange.EntireRow.Delete
    ActiveSheet.ShowAllData
  End If
  Application.ScreenUpdating = True
End Sub

Good afternoon., unfortunately I get another error now, subscript out of range. I can't find it,but I added the sheet for small part so maybe that helps.

Artikelen beheren 6.03.xlsm
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAEAFAGAHAIAJAKALAMANAOAPAQARASATAUAVAWAXAYAZBABBBCBDBEBFBGBHBIBJBKBLBMBNBOBPBQBRBSBTBUBVBWBXBYBZCACBCCCDCECFCGCHCICJCKCLCMCNCO
1Importdatum11/01/2024C:\TEMP\ART051_2024-01-11.XMLVERKOOPKOSTPRIJSLEVERANCIERINKOOPVOORRAAD/INKOOPVRIJE VELDEN
2Resource1005Opslag
3ValutaEUR3%
4Teller51Prijslijst filter APrijslijst filter BPrijslijst filter CPrijslijst filter DVerpakkingseenheidKleurSpecifiekVawa voorraadKostprijs factorOmverpakking EAN VerkoopeenheidEAN OmverpakkingMaatvoering schapkaart
5ArtikelcodeZoekcodeOmschrijvingStatusStartdatumVerkoopprijsVerkoopprijs nieuwVkp bedragVkp + opslagKp +opslagKp +margeEenheidKostprijsKostprijs nieuwIkp +opslag%Afwijking% opslagInkoopprijs BTW_VAssortiment01Assortiment02Assortiment03Assortiment04Assortiment05Assortiment06Assortiment07Assortiment08Assortiment09Assortiment10TekstArtDeelbaarVerkoopKortingExplodInkoopBatchSerienrCrdnrCrdNaamHoofdLevArtLevInkoopprijsInkoopprijs nieuwIkp bedragIkp% opslagValuta_IInkoopeenheidBTW_IInkFactorBstgrLevertijdVoorraadBestel_beleidBestelniveauMax_voorraadNetto_gewichtTelfreqArtgroepMagazijnLocatieGTIN_LevUserField_01UserField_02UserField_03UserField_04UserField_05UserField_06UserField_07UserField_08UserField_09UserField_10UserNumber_01UserNumber_02UserNumber_03UserNumber_04UserNumber_05UserNumber_06UserNumber_07UserNumber_08UserNumber_09UserNumber_10UserNumber_11UserNumber_12UserNumber_13UserDate_01UserDate_02UserDate_03UserDate_04UserDate_05UserYesNo_01UserYesNo_02UserYesNo_03UserYesNo_04UserYesNo_05
61000008712759037825Vito Glaserfix 111 6x2 mm wit - 10x25 mA24/09/20192,00 pak5,00 -26,08%6,501CELBANDPEVITOGLASERFIXWIT--13160010010010027Vito GmbH105902500602001286,50 EURpackICV196211F191150011P02B8712759037825871275903782540440410141586x200000000000005/10/202311000
71000018712759037832Vito Glaserfix 111 6x2 mm zwart - 10x25 mA24/09/20194,00 pak6,00 -10,69%6,501CELBANDPEVITOGLASERFIXZWART--113160110010010027Vito GmbH105902500602001206,50 EURpackICV196211F121080011P04B8712759037832871275903783240440410141656x200000000000009/10/202311000
81000028712759037849Vito Glaserfix 111 6x3 mm wit - 10x25 mA24/09/20195,00 pak7,00 -4,41%7,101CELBANDPEVITOGLASERFIXWIT--303160010010010027Vito GmbH11002500603005287,10 EURpackICV196211F301260011P02B8712759037849871275903784940440410141726x300000000000011000
91000038712759037856Vito Glaserfix 111 6x3 mm zwart - 10x25 mA24/09/20199,00 pak8,00 9,68%7,101CELBANDPEVITOGLASERFIXZWART--3160110010010027Vito GmbH11002500603005207,10 EURpackICV196211F71030011P04B8712759037856871275903785640440410141896x300000000000011000
101000048712759037863Vito Glaserfix 111 6x4 mm wit - 10x25 mA24/09/201910,00 pak9,00 -9,25%9,601CELBANDPEVITOGLASERFIXWIT--293160110010010027Vito GmbH105902500604001289,60 EURpackICV196210L000011P02B8712759037863871275903786340440410141966x400000000000017/08/202311000
Items
Cell Formulas
RangeFormula
B1B1=TODAY()
C1C1=TEXT("C:\TEMP\ART",0)&TEXT($B$4,"000")&"_"&TEXT($B$1,"JJJJ")&"-"&TEXT($B$1,"MM")&"-"&TEXT($B$1,"DD")&".XML"
P6:P10P6=IFERROR(IFS(ROUND((((Q6*$P$3)+Q6)-M6),2)=0,"ok",Q6<>0,(100%-((M6/Q6)))*-100%-$P$3),"")
Q6:Q10Q6=IFERROR([@Inkoopprijs]/[@InkFactor],"")
G6:G10G6=IFERROR(IFS(K6>0,M6/(1-K6),J6>0,M6*(1*J6)+M6,I6>0,F6*(1*I6)+F6,H6>0,H6),"")
N6:N10N6=IFERROR(IFS(O6>0,AO6*(1*O6)+AO6)/AV6,"")
AP6:AP10AP6=IFERROR(IFS(AQ6>0,AQ6,AR6>0,AO6*(1*AR6)+AO6),"")
Cells with Conditional Formatting
CellConditionCell FormatStop If True
F6:F10228,M6:M10228,AO6:AO10228Expression=G6textNO
P6:P10228Cell Value="ok"textNO
P6:P10228Cell Valuecontains "inkoopprijs"textNO
C6:C10228Expression=LEN(C6)>60textNO
AY6:AZ10228Expression=AND($AY6=1;$AZ6="L")textNO
BI6:BJ10228Cell ValueduplicatestextNO
BH6:BH10228Cell ValueduplicatestextNO
B6:B10228Cell ValueduplicatestextNO
Cells with Data Validation
CellAllowCriteria
AA1:AA2Whole numberbetween 0 and 1
AA3Text lengthbetween 0 and 13
 
Upvote 0
Now I get script out of range.
In which line do you have the error?


Then check the table name: "ItemsImport" and the column name "Afwijking %" & Chr(10) & "Opslag"

In your original code:

1704983550812.png


Now on the sheet you put you no longer have the space:
1704983675324.png



Then, try this:

Rich (BB code):
Private Sub FilterOK_Click()
  Dim tbl As ListObject
  Dim col As ListColumn
  Dim fnd As Range
  
  Set tbl = ActiveSheet.ListObjects("ItemsImport")
  Set col = tbl.ListColumns("Afwijking%" & Chr(10) & "Opslag")
  Set fnd = col.Range.Find("ok", , xlValues, xlWhole, , , False)
  
  Application.ScreenUpdating = False
  If Not fnd Is Nothing Then
    tbl.Range.AutoFilter col.Index, "ok"
    tbl.DataBodyRange.EntireRow.Delete
    ActiveSheet.ShowAllData
  End If
  Application.ScreenUpdating = True
End Sub

🧙‍♂️
 
Upvote 0
Solution
In which line do you have the error?


Then check the table name: "ItemsImport" and the column name "Afwijking %" & Chr(10) & "Opslag"

In your original code:

View attachment 104796

Now on the sheet you put you no longer have the space:
View attachment 104797


Then, try this:

Rich (BB code):
Private Sub FilterOK_Click()
  Dim tbl As ListObject
  Dim col As ListColumn
  Dim fnd As Range
 
  Set tbl = ActiveSheet.ListObjects("ItemsImport")
  Set col = tbl.ListColumns("Afwijking%" & Chr(10) & "Opslag")
  Set fnd = col.Range.Find("ok", , xlValues, xlWhole, , , False)
 
  Application.ScreenUpdating = False
  If Not fnd Is Nothing Then
    tbl.Range.AutoFilter col.Index, "ok"
    tbl.DataBodyRange.EntireRow.Delete
    ActiveSheet.ShowAllData
  End If
  Application.ScreenUpdating = True
End Sub

🧙‍♂️
Oh wow, that was it, just a space. Ok I am not that good (yet) in VBA but try to learn from every piece I get in front of me. Thank you for your help, it works.

Have a nice evening.

Romano
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,179
Members
453,021
Latest member
Justyna P

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