Skip to main content

Conas colún a shórtáil de réir luacha in Excel?

Mar shampla, tá tábla ceannaigh agat mar a thaispeántar an scáileán thíos. Anois is mian leat an colún Praghas a shórtáil go huathoibríoch nuair a chuireann tú uimhreacha/praghsanna nua isteach sa cholún seo, conas a d'fhéadfá é a réiteach? Anseo tugaim isteach macra VBA chun cabhrú leat colún sonrach a shórtáil go huathoibríoch de réir luach in Excel.

Colún sórtála uathoibríoch de réir luacha le VBA


Colún sórtála uathoibríoch de réir luacha le VBA

Déanfaidh an macra VBA seo na sonraí go léir a shórtáil i gcolún ar leith go huathoibríoch a luaithe a iontrálann tú sonraí nua nó má athraíonn tú luach sa cholún in Excel.

1. Cliceáil ar dheis ar ainm reatha na bileoige sa Barra Tab Bileog, agus ansin cliceáil ar an Féach an cód ón roghchlár cliceáil ar dheis.

2. Sa bhosca dialóige tosaigh Microsoft Visual Basic for Application, greamaigh an macra-chód VBA seo a leanas isteach sa fhuinneog tosaigh.

VBA: Colún Sórtáil Auto in Excel

Private Sub Worksheet_Change(ByVal Target As Range)
On Error Resume Next
If Not Intersect(Target, Range("B:B")) Is Nothing Then
Range("B1").Sort Key1:=Range("B2"), _
Order1:=xlAscending, Header:=xlYes, _
OrderCustom:=1, MatchCase:=False, _
Orientation:=xlTopToBottom
End If
End Sub

nótaí:
1) Sa chód vba thuas, B: B. ciallaíonn sé go ndéanfaidh sé Colún B a shórtáil go huathoibríoch, B1 an chéad chill i gColún B, B2 an dara cill i gColún B, agus is féidir leat iad a athrú bunaithe ar do chuid riachtanas.
2) An Blúire Ceanntásc:=xlTá sa 5ú sraith insíonn Excel go bhfuil ceanntásc ag an raon a shórtáilfidh tú, ionas nach gcuirfear an chéad ró den raon san áireamh agus tú ag sórtáil. Mura bhfuil ceanntásc ann, athraigh go dtí Ceanntásc:=xlUimh; agus athrú Eochair1:=Raon("B2") sa 4ú sraith chun Eochair1:=Raon("B1").

3. Ansin téigh ar ais chuig an mbileog oibre, nuair a iontrálann tú uimhir nua sa cholún Praghsanna nó nuair a athraíonn tú aon phraghsanna atá ann cheana, sórtálfar an colún Praghsanna go huathoibríoch in ord ardaitheach.

nótaí: Nuair a chuireann tú uimhir nua isteach sa cholún Praghas, ní mór duit an uimhir a chur isteach sa chéad chill bhán faoi bhun na n-uimhreacha bunaidh. Má tá cealla bána idir an uimhir iontráilte nua agus na bunuimhreacha chomh maith le cealla bána idir na bunuimhreacha, ní dhéanfar an colún seo a shórtáil go huathoibríoch.


Taispeántas: Colún sórtála uathoibríoch de réir luacha le VBA in Excel


Kutools le haghaidh Excel: Breis is 300 uirlis láimhe ar do mhéar! Cuir tús le do thriail saor in aisce 30-lá gan aon srianta gné inniu. Íosluchtaigh Anois!

Déan é a shórtáil go héasca de réir mhinicíocht na dteagmhas in Excel

Kutools le haghaidh Excel's Ard-Sórtáil Tacaíonn fóntais le sonraí a shórtáil de réir faid téacs, ainm deiridh, luach absalóideach, minicíocht, srl. in Excel go tapa.


sórtáil ad de réir minicíochta 2

Uirlisí Táirgiúlachta Oifige is Fearr

🤖 Kutools AI Aide: anailís sonraí a réabhlóidiú bunaithe ar: Forghníomhú Chliste   |  Gin Cód  |  Cruthaigh Foirmlí Saincheaptha  |  Anailís a dhéanamh ar Sonraí agus Cairteacha a Ghin  |  Feidhmeanna Kutools a agairt...
Gnéithe Coitianta: Faigh, Aibhsigh nó Aithnigh Dúblaigh   |  Scrios Sraitheanna Bána   |  Comhcheangail Colúin nó Cealla gan Sonraí a Chailleadh   |   Babhta gan Foirmle ...
Cuardaigh Super: Ilchritéir VLookup    VLookup Illuachanna  |   VLookup Trasna Ilbhileoga   |   Amharc doiléir ....
Liosta anuas Casta: Go tapa Cruthaigh Liosta Anuas   |  Liosta anuas Cleithiúnach   |  Liosta Buail Isteach Ilroghnacha ....
Bainisteoir Colún: Cuir Líon Sonrach Colún leis  |  Colúin Bog  |  Scoránaigh Stádas Infheictheachta na gColún Ceilte  |  Déan comparáid idir Raonta & Colúin ...
Gnéithe Réadmhaoin: Fócas Eangaí   |  Amharc Dearaidh   |   Barra Mór na Foirmle    Leabhar Oibre & Bainisteoir Bileog   |  Leabharlann Acmhainní (Uaththéacs)   |  Piocálaí Dáta   |  Comhcheangail Bileoga Oibre   |  Criptigh/Díchriptigh Cealla    Seol Ríomhphost trí Liosta   |  Scagaire Super   |   Scagaire Speisialta (scagaire trom/iodálach/stailc tríd...) ...
Barr 15 Uirlisí12 Téacs uirlisí (Cuir Téacs, Bain Carachtair,...)   |   50 + Cairt cineálacha (Cairt Gantt,...)   |   40+ Praiticiúil Foirmlí (Ríomh aois bunaithe ar lá breithe,...)   |   19 Insertion uirlisí (Cuir isteach Cód QR, Ionsáigh Pictiúr ón gCosán,...)   |   12 Tiontú uirlisí (Uimhreacha le Focail, Comhshó Airgeadra,...)   |   7 Cumaisc & Scoilt uirlisí (Sraitheanna Comhcheangail Casta, Cealla Scoilt,...)   |   ... agus eile

Supercharge Do Scileanna Excel le Kutools le haghaidh Excel, agus Éifeachtúlacht Taithí Cosúil Ná Roimhe. Kutools le haghaidh Excel Tairiscintí Níos mó ná 300 Ardghnéithe chun Táirgiúlacht a Treisiú agus Sábháil Am.  Cliceáil anseo chun an ghné is mó a theastaíonn uait a fháil ...

Tuairisc


Tugann Tab Oifige comhéadan Tabbed chuig Office, agus Déan Do Obair i bhfad Níos Éasca

  • Cumasaigh eagarthóireacht agus léamh tabbed i Word, Excel, PowerPoint, Foilsitheoir, Rochtain, Visio agus Tionscadal.
  • Oscail agus cruthaigh cáipéisí iolracha i gcluaisíní nua den fhuinneog chéanna, seachas i bhfuinneoga nua.
  • Méadaíonn do tháirgiúlacht 50%, agus laghdaíonn sé na céadta cad a tharlaíonn nuair luch duit gach lá!
Comments (37)
Rated 5 out of 5 · 1 ratings
This comment was minimized by the moderator on the site
such a good information thanks a lot
This comment was minimized by the moderator on the site
Maravilhoso! Muito obrigada pela informação, amigo!
Rated 5 out of 5
This comment was minimized by the moderator on the site
I love this, but I'd like to know what I should do if I want it to be multiple different ranges in the same column. When I use the code above, it works for the first table that I have but the bottom two tables don't sort automatically. I tried changing the range, I also duplicated the code and changed the code to match the tables, but nothing is working.

For example:
Range("L8").Sort Key1:=Range("L37"),Range("L41").Sort Key1:=Range("L62") _

Or just duplicating the code like this:
Private Sub Worksheet_Change(ByVal Target As Range)
On Error Resume Next
If Not Intersect(Target, Range("L:L")) Is Nothing Then
Range("L8").Sort Key1:=Range("L37"), _
Order1:=xlAscending, Header:=xlYes, _
OrderCustom:=1, MatchCase:=False, _
Orientation:=xlTopToBottom
End If
End Sub

Private Sub Worksheet_Change(ByVal Target As Range)
On Error Resume Next
If Not Intersect(Target, Range("L:L")) Is Nothing Then
Range("L41").Sort Key1:=Range("L62"), _
Order1:=xlAscending, Header:=xlYes, _
OrderCustom:=1, MatchCase:=False, _
Orientation:=xlTopToBottom
End If
End Sub

Private Sub Worksheet_Change(ByVal Target As Range)
On Error Resume Next
If Not Intersect(Target, Range("L:L")) Is Nothing Then
Range("L66").Sort Key1:=Range("L100"), _
Order1:=xlAscending, Header:=xlYes, _
OrderCustom:=1, MatchCase:=False, _
Orientation:=xlTopToBottom

But then it will tell me that "Ambiguos name detected: Worksheet_Change" but it won't do that if I only have the code one. Would anyone be able to help me out?
Thank you!
This comment was minimized by the moderator on the site
Hi there,

You can use the vba below:
Private Sub Worksheet_Change(ByVal Target As Range)
    On Error Resume Next
    If Not Intersect(Target, Range("K:K")) Is Nothing Then
        Range("K32:K36").Sort Key1:=Range("K32"), _
        Order1:=xlAscending, Header:=xlNo, _
        OrderCustom:=1, MatchCase:=False, _
        Orientation:=xlTopToBottom
        
        Range("K38:K42").Sort Key1:=Range("K38"), _
        Order1:=xlAscending, Header:=xlNo, _
        OrderCustom:=1, MatchCase:=False, _
        Orientation:=xlTopToBottom        

        Range("K44:K46").Sort Key1:=Range("K44"), _
        Order1:=xlAscending, Header:=xlNo, _
        OrderCustom:=1, MatchCase:=False, _
        Orientation:=xlTopToBottom
    End If
End Sub


Amanda
This comment was minimized by the moderator on the site
01. I have sorted on Name data in Excel Worksheet as Sheet1.
02. I want the Names that are repeated in New Worksheet as Sheet2.
This comment was minimized by the moderator on the site
Hallo
Ich habe folgenden Code, aber die Sortierung klappt leider nicht.
evtl nur eine kleine Anpassung, aber ich verzweifle hier seit Tagen.
Danke im voraus.


Sub Eindeutige_Daten()

Dim rng As Range
Dim InputRng As Range, OutRng As Range

Set dt = CreateObject("Scripting.Dictionary")

xTitleId = "Eindeutige Daten"

Set InputRng = Application.Selection
Set InputRng = Application.InputBox("Range :", xTitleId, InputRng.Address, Type:=8)
Set OutRng = Application.InputBox("Out put to (single cell):", xTitleId, Type:=8)

For Each rng In InputRng
If rng.Value <> "" Then
dt(rng.Value) = ""
End If
Next

OutRng.Range("A1").Resize(dt.Count) = Application.WorksheetFunction.Transpose(dt.Keys)

'** Zelladresse in Spalten- und Zeilenangabe trennen
Dim wert() As String
wert = Split(OutRng.Address, "$")

letztezeile = ActiveSheet.Cells(1048576, wert(1)).End(xlUp).Row

With ActiveWorksheet.Sort
.SetRange Range(OutRng & ":" & wert(1) & letztezeile)
.Header = xlNo
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With

Calculate

End Sub
This comment was minimized by the moderator on the site
Hi there,

Please debug the below snipt of your code and see if there is a problem.
With ActiveWorksheet.Sort
.SetRange Range(OutRng & ":" & wert(1) & letztezeile)
.Header = xlNo
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With

Remember to check the values you entered.
If there is still questions, please don't hesitate to ask me.

Amanda
This comment was minimized by the moderator on the site
Love it. Works for me.

But when I repeat the exact same steps at another Excel file, and enter a number in the colomn for it to autosort, Excel closes.
This comment was minimized by the moderator on the site
Hi,
This is extremely useful.
Is there a way to expand the formula to cover multiple columns? For example, to sort data based on values on first, column B, and then column C?I would really appreciate any solutions!
This comment was minimized by the moderator on the site
Amazing thanks!!!
This comment was minimized by the moderator on the site
So this seems to work when the data is manually entered but doesn't work when it is a table that repopulates from another file....is there any way to do that?
This comment was minimized by the moderator on the site
Hi, it arrange the entire row but I have some link on cells into specific folder which is left behind after sort.
There are no comments posted here yet
Load More
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations