Excel Double-click the VBA code for automatic sorting of column headings

Source: Internet
Author: User

By using the following VBA code, you can drop it in a double-click event on a worksheet to get to the column header of the worksheet (double-click the name of the column), and the current column is sorted automatically.

For example, place your code in the following event:

Private Sub Worksheet_selectionchange (ByVal Target as Range)

End Sub

The code is as follows:

Dim Yourg as Range

If target.column <= me.cells (1,1). CurrentRegion.Columns.Count and Target.Row = 1 Then

If target.column <> Mncolumn Then

Mncolumn = Target.Column

Mndirection = xlascending

Else

If mndirection = xlascending Then

Mndirection = xldescending

Else

Mndirection = xlascending

End If

End If

Set Yourg = me.cells (1, 1). CurrentRegion

Yourg. Sort Key1:=yourg. Cells (1, mncolumn), Order1:=mndirection,header:=xlyes

Set Yourg = Nothing

Cancel = True

End If

The above code is sorted in ascending order, and you can sort by descending order.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.