Macro VBA code failing to execute a section of the code without an error

0 votes

Hello, I've used a macro to record a VBA code. Up to the point when the code is supposed to sort two columns, everything runs flawlessly. Please take note that I obtained this portion of the code by recording a macro for it. I want to apply this code to various worksheets.

Sub testSort()
'
' testSort Macro
'

'
    Range(Selection, Selection.End(xlToRight)).Select
    Range(Selection, Selection.End(xlDown)).Select
 
 ActiveWorkbook.Worksheets("Analysis 1").Sort.SortFields.Clear
 ActiveWorkbook.Worksheets("Analysis 1").Sort.SortFields.Add2 Key:=Range( _
         "E5:E661325"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
 xlSortNormal
 With ActiveWorkbook.Worksheets("Analysis 1").Sort
         .SetRange Range("A4:F661325")
         .Header = xlYes
         .MatchCase = False
         .Orientation = xlTopToBottom
         .SortMethod = xlPinYin
         .Apply
 End With
End Sub

I tried to run the code by sub categories I first created a macro that just select the portion that I want to sort I took that bit of my code and join it to the main code and it worked just great. I copied that portion of my code to my main code as before and recorded a macro that uses Excel's data sort method to sort the data, but it is failing.

Mar 24, 2023 in Others by narikkadan
• 63,600 points
730 views

1 answer to this question.

0 votes

You get your code to make a choice, but your sort doesn't sort the choice; instead, it scans a particular range. Try this if you want to sort your selection (but replace A1 and B1 with the header cells you wish to sort by):

    Sub trytthis()

    Range(Selection, Selection.End(xlToRight)).Select
    Range(Selection, Selection.End(xlDown)).Select

    Selection.Sort Key1:=Range("a1"), order1:=xlAscending, key2:=Range("b1"), order2:=xlAscending, Header:=xlYes

    End Sub
answered Mar 24, 2023 by Kithuzzz
• 38,000 points

Related Questions In Others

0 votes
1 answer

VBA code to select only a table. I am getting a Run-time error '1004'; Method 'Range' of object'_Global' failed

No copy/paste, just direct assignment use.Value Sub Final_Report() ...READ MORE

answered Jan 13, 2023 in Others by narikkadan
• 63,600 points
1,168 views
0 votes
0 answers

how to list the contents of a asset into an event

May 29, 2019 in Others by anonymous
679 views
0 votes
1 answer
0 votes
1 answer

Creating a function in excel VBA to calculate the average point in a circular set of numbers

I used the following code to determine ...READ MORE

answered Oct 28, 2022 in Others by narikkadan
• 63,600 points
1,159 views
0 votes
1 answer

Retrieve epay.info Balance with VBA and Excel

This code should log you in, provided ...READ MORE

answered Sep 5, 2018 in Blockchain by digger
• 26,740 points
1,260 views
0 votes
1 answer

How to load file to Excel Power query from SFTP site

Currently, I don't think there is a ...READ MORE

answered Dec 3, 2018 in Power BI by Upasana
• 8,620 points
3,698 views
0 votes
1 answer

Using VBA Excel to create a gramatically correct list

The Excel AND function is a logical ...READ MORE

answered Feb 9, 2022 in Others by gaurav
• 23,260 points
978 views
0 votes
2 answers

How to copy a formula horizontally within a table using Excel VBA?

Hi so basically, create an adjacent column ...READ MORE

answered Feb 16, 2022 in Others by Edureka
• 13,690 points
1,091 views
0 votes
1 answer
0 votes
1 answer

How to make an error flagging array in VBA and translate all array elements as a string message?

In my opinion, using an array in ...READ MORE

answered Mar 17, 2023 in Others by Kithuzzz
• 38,000 points
542 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP