Advice to sort table after values have been pasted

ipbr21054

Well-known Member
Joined
Nov 16, 2010
Messages
5,720
Office Version
  1. 2007
Platform
  1. Windows
Hi,
Code in use is shown below.
The code cuts the values from its original location on the worksheet then pastes them in the columns P-Z
Currently the values in columns P-Z are a table where i manually click the down arrow to sort A-Z
Row 3 are headers.
Row 4 is hidden.
Row 5 is where the value start.

I added some code shown in Red below so once values are pasted to then sort A-Z without me doing it manually.
The code now fails when trying to sort.

I see the following RTE 1004
Sort method of range class failed





Rich (BB code):
Private Sub ListBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
  Dim i As Long, j As Long
  Dim lr As Long, lr2 As Long
  Dim sh As Worksheet
  Dim x As Long

  Set sh = Sheets("GRASS")
  With ListBox1
    lr2 = sh.Range("Q" & Rows.Count).End(xlUp).Row + 1
    If lr2 < 5 Then lr2 = 5
    j = .ListIndex + 5
  End With
  
  With sh.Range("P" & lr2).Resize(1, 11)
    .Value = sh.Range("A" & j).Resize(1, 11).Value
    
    .Font.Size = 16
    .Font.Bold = True
    .Font.Name = "Calibri"
    
  Application.ScreenUpdating = False
        With Sheets("GRASS")
        If .AutoFilterMode Then .AutoFilterMode = False
        x = .Cells(.Rows.Count, 1).End(xlUp).Row
            .Range("P4:Z" & x).Sort Key1:=Range("P5"), Order1:=xlAscending, Header:=xlGuess
  End With
  End With
  
  With ListBox1
    sh.Range("A" & j & ":K" & j).Delete Shift:=xlUp
    
    lr = sh.Range("B" & Rows.Count).End(xlUp).Row
    If lr > 4 Then .RowSource = sh.Name & "!A5:B" & lr
    For i = 0 To .ListCount - 1
      If .Selected(i) Then .Selected(i) = False
    Next
  End With
  Unload SOCD
  Range("A5").Select
End Sub
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Without seeing your data or knowing whether Column P-Z are all the columns in your table, my guess it:-
x is getting a row value based on Column A and that is giving a row number outside of the table range which is causing the table sort to error out.
You have one "WITH" inside of another "WITH" and they are incompatible. End the first With before starting the new one and be more specific about which column you want to use for working out the value for "x" eg:

Rich (BB code):
    With sh.Range("P" & lr2).Resize(1, 11)
        '.Value = sh.Range("A" & j).Resize(1, 11).Value
       
        .Font.Size = 16
        .Font.Bold = True
        .Font.Name = "Calibri"
    End With                                                ' Moved to here from below
   
    Application.ScreenUpdating = False
    With Sheets("GRASS")
        If .AutoFilterMode Then .AutoFilterMode = False
        x = .Cells(.Rows.Count, "P").End(xlUp).Row          ' Changed to P but change to suit - maybe Q
        .Range("P4:Z" & x).Sort Key1:=Range("P5"), Order1:=xlAscending, Header:=xlGuess
    End With
  'End With                                                 ' Deleted moved to before Application.ScreenUpdating
 
Upvote 0

Forum statistics

Threads
1,223,635
Messages
6,173,479
Members
452,516
Latest member
archcalx

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