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.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.