Solved

VBA WorksheetChange Event To sort Data based on a specific column

Posted on 2015-01-15
4
88 Views
Last Modified: 2015-01-15
Hello,

please see attached file.
i need help with VBA on worksheet changeevent that whenever any new data or any change made on sheet1, the VBA auto sorts in ascending order one column that contains the header label "SP Name"



thanks.
EE.xlsm
0
Comment
Question by:Flora
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 51

Expert Comment

by:Rgonzo1971
ID: 40551150
HI,

pls try

Private Sub Worksheet_Change(ByVal Target As Range)
    ActiveSheet.Range(Range("A1"), Range("K" & Cells.Rows.Count).End(xlUp)).Sort _
            key1:=Range("D1"), order1:=xlAscending, Header:=xlYes
End Sub

Open in new window

Regards
0
 
LVL 6

Author Comment

by:Flora
ID: 40551165
Thanks very much Rgonzo1971

if you are away from your machine, i can wait, the above codes works, but there are two issues here.

A) my data varies time to time, so my end of data to the left is not column K, so it could expand to further columns.
is there any possibility that to use somthign that works dynamic instead of fixed column K?

B) if the sort column could also be set dynamically. for example  trigger sort only in a column where its header input name is "SP Name"     so for example. if my next data varies and the SP Name is in Column C then the above code will not work unless i manually change it key1:=Range("C1"),   so would it be possible to put a seach on key1:= search"Sp Name", in row 1 ? somthing like this?
0
 
LVL 51

Accepted Solution

by:
Rgonzo1971 earned 500 total points
ID: 40551191
Then try

Private Sub Worksheet_Change(ByVal Target As Range)
    lngCol = Range("1:1").Find("SP Name").Column
    ActiveSheet.Range(Range("A1"), Range("K" & Cells.Rows.Count).End(xlUp)).Sort _
            key1:=Cells(1, lngCol), order1:=xlAscending, Header:=xlYes

End Sub

Open in new window

Regards
0
 
LVL 6

Author Closing Comment

by:Flora
ID: 40551238
you are my hero :-)
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
This article describes a serious pitfall that can happen when deleting shapes using VBA.
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question