Skip i'r prif gynnwys

Sut i ddidoli colofn yn ôl gwerth yn Excel?

Er enghraifft, mae gennych dabl prynu fel y dangosir y sgrinlun isod. Nawr rydych chi am i'r golofn Pris gael ei didoli'n awtomatig pan fyddwch chi'n nodi rhifau/prisiau newydd yn y golofn hon, sut allech chi ei datrys? Yma, rwy'n cyflwyno macro VBA i'ch helpu chi i ddidoli colofn benodol yn ôl gwerth yn Excel yn awtomatig.

Colofn didoli awto yn ôl gwerth gyda VBA


Colofn didoli awto yn ôl gwerth gyda VBA

Bydd y macro VBA hwn yn didoli'r holl ddata mewn colofn benodol yn awtomatig cyn gynted ag y byddwch yn mewnbynnu data newydd neu'n newid gwerth yn y golofn yn Excel.

1. De-gliciwch enw'r ddalen gyfredol yn y Bar Tab Dalen, ac yna cliciwch ar Gweld y Cod o'r ddewislen clicio ar y dde.

2. Yn y blwch deialog agoriadol Microsoft Visual Basic for Application, pastiwch y cod macro VBA canlynol i'r ffenestr agoriadol.

VBA: Colofn Trefnu Auto yn 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

Nodyn:
1) Yn y cod vba uchod, B: B. yn golygu y bydd yn didoli Colofn B yn awtomatig, B1 yw'r gell gyntaf yng ngholofn B, B2 yw'r ail gell yng Ngholofn B, a gallwch eu newid yn seiliedig ar eich anghenion.
2) Y pyt Pennawd:=xlIe yn y 5ed rhes yn dweud wrth Excel bod gan yr ystod y byddwch chi'n ei ddidoli bennawd, fel na fydd rhes gyntaf yr ystod yn cael ei chynnwys wrth ddidoli. Os nad oes pennawd, newidiwch ef i Pennawd:=xlNa; a newid Allwedd 1:=Amrediad("B2") yn y 4edd rhes i Allwedd 1:=Amrediad("B1").

3. Yna ewch yn ôl i'r daflen waith, pan fyddwch chi'n nodi rhif newydd yn y golofn Brisiau neu'n addasu unrhyw brisiau sy'n bodoli, bydd y golofn Brisiau yn cael ei didoli'n awtomatig yn nhrefn esgynnol.

Nodyn: Pan fyddwch chi'n nodi rhif newydd yn y golofn Pris, rhaid i chi nodi'r rhif yn y gell wag gyntaf o dan y rhifau gwreiddiol. Os oes celloedd gwag rhwng y rhif newydd a gofnodwyd a'r rhifau gwreiddiol yn ogystal â chelloedd gwag rhwng y rhifau gwreiddiol, ni fydd y golofn hon yn cael ei didoli'n awtomatig.


Demo: Colofn didoli awto yn ôl gwerth gyda VBA yn Excel


Kutools ar gyfer Excel: Dros 300 o offer defnyddiol ar flaenau eich bysedd! Dechreuwch eich treial am ddim 30 diwrnod heb unrhyw gyfyngiadau nodwedd heddiw. Lawrlwytho Nawr!

Didoli'n hawdd yn ôl amlder y digwyddiadau yn Excel

Kutools ar gyfer Excel's Trefnu Uwch mae cyfleustodau yn cefnogi didoli data yn ôl hyd testun, enw olaf, gwerth absoliwt, amlder, ac ati yn Excel yn gyflym.


didoli ad yn ôl amledd 2

Offer Cynhyrchiant Swyddfa Gorau

🤖 Kutools AI Aide: Chwyldro dadansoddi data yn seiliedig ar: Cyflawniad Deallus   |  Cynhyrchu Cod  |  Creu Fformiwlâu Personol  |  Dadansoddi Data a Chynhyrchu Siartiau  |  Invoke Swyddogaethau Kutools...
Nodweddion Poblogaidd: Darganfod, Amlygu neu Adnabod Dyblygiadau   |  Dileu Rhesi Gwag   |  Cyfuno Colofnau neu Gelloedd heb Colli Data   |   Rownd heb Fformiwla ...
Super-edrych: VLookup Meini Prawf Lluosog    VLookup Gwerth Lluosog  |   VLookup Ar Draws Taflenni Lluosog   |   Edrych Niwlog ....
Rhestr gwympo Uwch: Creu Rhestr Gollwng yn Gyflym   |  Rhestr Gollwng Dibynnol   |  Rhestr Gollwng Aml-ddewis ....
Rheolwr Colofn: Ychwanegu Nifer Penodol o Golofnau  |  Symud Colofnau  |  Toglo Statws Gwelededd Colofnau Cudd  |  Cymharwch Ystodau a Cholofnau ...
Nodweddion dan Sylw: Ffocws ar y Grid   |  Golwg Dylunio   |   Bar Fformiwla Mawr    Rheolwr Llyfr Gwaith a Thaflen   |  Llyfrgell Adnoddau (Testun Auto)   |  Dewiswr Dyddiad   |  Cyfuno Taflenni Gwaith   |  Amgryptio/Dadgryptio Celloedd    Anfon E-byst trwy Restr   |  Hidlo Super   |   Hidlo Arbennig (hidlo mewn print trwm/italig/strikethrough...) ...
15 Set Offer Gorau12 Testun offer (Ychwanegu Testun, Dileu Cymeriadau,...)   |   50 + Siart Mathau (Siart Gantt,...)   |   40+ Ymarferol Fformiwlâu (Cyfrifwch oedran yn seiliedig ar ben-blwydd,...)   |   19 mewnosod offer (Mewnosod Cod QR, Mewnosod Llun o'r Llwybr,...)   |   12 Trosi offer (Rhifau i Eiriau, Trosi arian cyfred,...)   |   7 Uno a Hollti offer (Rhesi Cyfuno Uwch, Celloedd Hollt,...)   |   ... a mwy

Supercharge Eich Sgiliau Excel gyda Kutools ar gyfer Excel, a Phrofiad Effeithlonrwydd Fel Erioed Erioed. Kutools ar gyfer Excel Yn Cynnig Dros 300 o Nodweddion Uwch i Hybu Cynhyrchiant ac Arbed Amser.  Cliciwch Yma i Gael Y Nodwedd Sydd Ei Angen Y Mwyaf...

Disgrifiad


Mae Office Tab yn dod â rhyngwyneb Tabbed i Office, ac yn Gwneud Eich Gwaith yn Haws o lawer

  • Galluogi golygu a darllen tabbed yn Word, Excel, PowerPoint, Cyhoeddwr, Mynediad, Visio a Phrosiect.
  • Agor a chreu dogfennau lluosog mewn tabiau newydd o'r un ffenestr, yn hytrach nag mewn ffenestri newydd.
  • Yn cynyddu eich cynhyrchiant 50%, ac yn lleihau cannoedd o gliciau llygoden i chi bob dydd!
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