Want to short column by searching the heading with vba

Abhimannu

New Member
Joined
Apr 22, 2019
Messages
7
Can someone help me to write a vba code to search column heading and then sort it
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Search for what? in what row? Sort what (If you are sorting just the heading it just stays there)?

Basically supply the relevant details.
 
Upvote 0
i have tried one, mentioned below, but getting error " Run-time error '1004': Method 'Range' of object '_worksheet' failed."

Dim rangecount As Integer
Dim colcount As Integer
Dim ra As Range


Set ra = Worksheets("After Removing Duplicates").Cells.Find("Start time", LookAt:=xlWhole)
If ra Is Nothing Then
MsgBox "Name was not found."
Else
MsgBox "Name found at :" & ra.Address
MsgBox ("value of ra variable is " & ra.Value)
End If

ActiveWorkbook.Worksheets("After Removing Duplicates").Sort.SortFields.Add2 Key _
:=Range(ra.Column & "2:" & ra.Column & rangecount), SortOn:=xlSortOnValues, Order:=xlAscending, _
DataOption:=xlSortNormal
 
Upvote 0
First of all you haven't answered the questions asked.
Second on what line is the error occurring?
 
Upvote 0
Search for what? in what row? Sort what (If you are sorting just the heading it just stays there)?

Basically supply the relevant details.

I want to search for "Start Time" column, then want to sort the rows of table, based on values in column heading "Start Time". Since the actual code is to big I am providing part of it only.

Dim rangecount As Integer
Dim colcount As Integer
Dim ra As Range


Sheets("After Removing Duplicates").Select
rangecount = ActiveSheet.UsedRange.Rows.Count
colcount = ActiveSheet.UsedRange.Columns.Count


Set ra = Worksheets("After Removing Duplicates").Cells.Find("Start time", LookAt:=xlWhole)
If ra Is Nothing Then
MsgBox "Name was not found."
Else
MsgBox "Name found at :" & ra.Address
MsgBox ("value of ra variable is " & ra.Value)
End If


ActiveWorkbook.Worksheets("After Removing Duplicates").Sort.SortFields.Add2 Key _
:=Range(ra.Column & "2:" & ra.Column & rangecount), SortOn:=xlSortOnValues, Order:=xlAscending, _
DataOption:=xlSortNormal
 
Upvote 0
First of all you haven't answered the questions asked.
Second on what line is the error occurring?

I want to search for "Start Time" column, then want to sort the rows of table, based on values in column heading "Start Time".

getting error message of " Run-time error '1004': Method 'Range' of object '_worksheet' failed." at below line

ActiveWorkbook.Worksheets("After Removing Duplicates").Sort.SortFields.Add2 Key _
:=Range(ra.Column & "2:" & ra.Column & rangecount), SortOn:=xlSortOnValues, Order:=xlAscending, _
DataOption:=xlSortNormal
 
Upvote 0
Post your full code in code tags (paste your code, select it and click the # icon).

Or try

Code:
Sub XXXX()
    Dim rangecount As Integer
    Dim colcount As Integer
    Dim ra As Range


    Set ra = Worksheets("After Removing Duplicates").Cells.Find("Start time", LookAt:=xlWhole)
    If ra Is Nothing Then
        MsgBox "Name was not found."
    Else
        MsgBox "Name found at :" & ra.Address
        MsgBox ("value of ra variable is " & ra.Value)
    End If

    ra.Sort Key1:=ra.EntireColumn, Order1:=xlAscending, Header:=xlYes
End Sub
 
Last edited:
Upvote 0
Post your full code in code tags (paste your code, select it and click the # icon).

Or try

Code:
Sub XXXX()
    Dim rangecount As Integer
    Dim colcount As Integer
    Dim ra As Range


    Set ra = Worksheets("After Removing Duplicates").Cells.Find("Start time", LookAt:=xlWhole)
    If ra Is Nothing Then
        MsgBox "Name was not found."
    Else
        MsgBox "Name found at :" & ra.Address
        MsgBox ("value of ra variable is " & ra.Value)
    End If

    ra.Sort Key1:=ra.EntireColumn, Order1:=xlAscending, Header:=xlYes
End Sub

Thanks a lot. it is working fine.
actually i am trying to prepare report of 16 different file using single vba code, for that i need to sort all files based on three different column, then need to remove duplicate entries and then respective 16 pivot tables are need to refreshed.

Till now 1 file is done with help of your code. Thanks again.
 
Upvote 0

Forum statistics

Threads
1,214,522
Messages
6,120,022
Members
448,939
Latest member
Leon Leenders

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