Code runs in Module not in sheet code

mahhdy

Board Regular
Joined
Sep 15, 2016
Messages
86
Hi,
I have a custom sort code, which runs perfectly when I am calling it from a module, but when I copy it inside the my codes in the sheet, and address it, it returns error.
Can somebody help and explain?

here is my code:
Code:
Sub custom_sort()


Application.calculation = xlManual
Application.EnableEvents = False
Application.ScreenUpdating = False
Call identification


Dim Customlist(250) As String
Dim ii As Double, c As Range
ii = 0
For Each c In Selection
    If Not c.Rows.Hidden Then
        Customlist(ii) = c
        Debug.Print "ii = "; ii & " " & Customlist(ii)
        ii = ii + 1
    Else
    End If
Next
Application.AddCustomList ListArray:=Customlist
    tb.Sort.SortFields.Clear
    tb.Sort.SortFields.Add Key _
        :=Range("tbl[Work Book]"), SortOn:=xlSortOnValues, Order:=xlAscending, _
        CustomOrder:=Application.CustomListCount, DataOption:=xlSortNormal
    ws.Activate
    With tb.Sort
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
Application.DeleteCustomList Application.CustomListCount
Application.calculation = xlCalculationAutomatic
Application.EnableEvents = True
End Sub
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
What is the error and on which line? The first thing that comes to mind is that if the tbl Table is not on the sheet you put the code in, you need to use:

Code:
Application.Range("tbl[Work Book]")

in the sort key, and not just:

Code:
Range("tbl[Work Book]")

It's not clear what tb is in your code, so I can't say if that might also be an issue.
 
Upvote 0
Great Help..
Thanks it fixed by adding application.range

the error was on the same line.
Yours,
M

What is the error and on which line? The first thing that comes to mind is that if the tbl Table is not on the sheet you put the code in, you need to use:

Code:
Application.Range("tbl[Work Book]")

in the sort key, and not just:

Code:
Range("tbl[Work Book]")

It's not clear what tb is in your code, so I can't say if that might also be an issue.
 
Upvote 0

Forum statistics

Threads
1,215,001
Messages
6,122,648
Members
449,092
Latest member
peppernaut

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