Salt la conținutul principal

Cum se sortează automat data când data este introdusă sau modificată în Excel? 

În Excel, fișierul Sortează funcția vă poate ajuta să sortați data în ordine crescătoare sau descendentă după cum aveți nevoie. Dar nu este dinamic, dacă ați sortat data și apoi adăugați o dată nouă, ar trebui să o sortați din nou. Există modalități bune și rapide de a sorta automat data când introduceți data nouă de fiecare dată într-o foaie de lucru?

Sortează automat data când data este introdusă sau modificată cu formula

Sortați automat data când data este introdusă sau modificată cu codul VBA


săgeată albastru dreapta balon Sortează automat data când data este introdusă sau modificată cu formula

De exemplu, data originală din coloana A, următoarea formulă vă poate ajuta să sortați automat data sau orice alte șiruri de text într-o nouă coloană de asistență pe baza coloanei pe care doriți să o sortați, vă rugăm să faceți următoarele:

1. Introduceți această formulă:

=INDEX($A$2:$A$15,MATCH(ROWS($A$2:A2),COUNTIF($A$2:$A$15,"<="&$A$2:$A$15),0)) într-o celulă goală lângă coloana dată, C2, de exemplu, și apoi apăsați Ctrl + Shift + Enter tastele împreună și veți obține o secvență numerică, apoi trageți mânerul de umplere în jos în celulele pe care doriți să le utilizați, consultați captura de ecran:

notițe: În formula de mai sus: A2: A15 este intervalul dvs. original de date pe care doriți să îl sortați automat.

doc autosort după data 1

2. Apoi formatați numerele ca format de dată făcând clic pe Data scurtă de la General lista derulantă sub Inițial fila, vezi captura de ecran:

doc autosort după data 2

3. Apoi, numerele de ordine au fost convertite în formatul de dată și data originală a fost sortată, de asemenea, a se vedea captura de ecran:

doc autosort după data 3

4. De acum înainte, când introduceți o dată nouă sau modificați data în coloana A, data din coloana C va fi sortată automat în ordine crescătoare, a se vedea captura de ecran:

doc autosort după data 4


săgeată albastru dreapta balon Sortați automat data când data este introdusă sau modificată cu codul VBA

Următorul cod VBA vă poate ajuta să sortați automat data în coloana originală atunci când introduceți o dată nouă sau modificați data după cum aveți nevoie.

1. Accesați foaia de lucru pe care doriți să o sortați automat data când introduceți sau modificați o dată.

2. Faceți clic dreapta pe fila foaie și selectați Afișați codul din meniul contextual, în fereastra pop-out Microsoft Visual Basic pentru aplicații fereastra, copiați și lipiți următorul cod în spațiul gol Module fereastră, vezi captura de ecran:

Cod VBA: sortare automată la introducerea sau modificarea datei:

Private Sub Worksheet_Change(ByVal Target As Range)
'Updateby Extendoffice
    On Error Resume Next
    If Application.Intersect(Target, Application.Columns(1)) Is Nothing Then Exit Sub
    If Target.Count > 1 Then Exit Sub
    Range("A1").Sort Key1:=Range("A2"), Order1:=xlAscending, Header:=xlYes, _
                                        OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
End Sub

doc autosort după data 6

notițe: În codul de mai sus, data introdusă va fi sortare automată în coloana A, pe care o puteți modifica A1 și A2 către propriile celule după cum aveți nevoie.

3. De acum înainte, când introduceți data în coloana A, data va fi sortată crescând automat.

Cele mai bune instrumente de productivitate de birou

🤖 Kutools AI Aide: Revoluționați analiza datelor pe baza: Execuție inteligentă   |  Generați codul  |  Creați formule personalizate  |  Analizați datele și generați diagrame  |  Invocați funcțiile Kutools...
Caracteristici populare: Găsiți, evidențiați sau identificați duplicatele   |  Ștergeți rândurile goale   |  Combinați coloane sau celule fără a pierde date   |   Rundă fără Formula ...
Super căutare: VLookup cu mai multe criterii    VLookup cu valori multiple  |   VLookup pe mai multe foi   |   Căutare fuzzy ....
Listă derulantă avansată: Creați rapid o listă derulantă   |  Listă drop-down dependentă   |  Listă derulantă cu selectare multiplă ....
Manager de coloane: Adăugați un număr specific de coloane  |  Mutați coloanele  |  Comutați starea vizibilității coloanelor ascunse  |  Comparați intervale și coloane ...
Caracteristici prezentate: Focus pe grilă   |  Vedere de proiectare   |   Big Formula Bar    Manager registru de lucru și foi   |  Biblioteca de resurse (Text automat)   |  Data Picker   |  Combinați foi de lucru   |  Criptare/Decriptare celule    Trimiteți e-mailuri după listă   |  Super Filtru   |   Filtru special (filtrează bold/italic/barat...) ...
Top 15 seturi de instrumente12 Text Instrumente (Adăuga text, Eliminați caractere,...)   |   50+ Diagramă Tipuri de (Gantt Chart,...)   |   40+ Practic Formule (Calculați vârsta pe baza zilei de naștere,...)   |   19 inserare Instrumente (Introduceți codul QR, Inserați imaginea din cale,...)   |   12 Convertire Instrumente (Numere la cuvinte, conversie valutara,...)   |   7 Merge & Split Instrumente (Rânduri combinate avansate, Celule divizate,...)   |   ... și altele

Îmbunătățiți-vă abilitățile Excel cu Kutools pentru Excel și experimentați eficiența ca niciodată. Kutools pentru Excel oferă peste 300 de funcții avansate pentru a crește productivitatea și a economisi timp.  Faceți clic aici pentru a obține funcția de care aveți cea mai mare nevoie...

Descriere


Fila Office aduce interfața cu file în Office și vă face munca mult mai ușoară

  • Activați editarea și citirea cu file în Word, Excel, PowerPoint, Publisher, Access, Visio și Project.
  • Deschideți și creați mai multe documente în filele noi ale aceleiași ferestre, mai degrabă decât în ​​ferestrele noi.
  • Vă crește productivitatea cu 50% și reduce sute de clicuri de mouse pentru dvs. în fiecare zi!
Comments (15)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
Hello,This is a great tool. thank you. How can i apply this to multiple columns in the same tab? Could i apply it to restart sorting by date in a new cell of the same column? Would i just repaste the VBA code into the same window?
Thank you.
This comment was minimized by the moderator on the site
Hello Noname9,How are you? To achieve your goal by using VBA code is beyond my reach. But I do know how to use formulas to do the trick.Suppose we have two columns of dates, say A2:B7. How to sort these dates into a new column? Please do as follows.
First, we need to combine the two columns of dates into one column. Copy and paste the formula =INDEX($A$2:$B$7,INT((ROWS(D$2:D2)-1)/2)+1,MOD(ROWS(D$2:D2)-1,2)+1) into cell D2. And drag the fill handle down to combine all dates. Please see screenshot 1.
Then, we will sort the combined dates. Copy and paste the formula =INDEX($D$2:$D$13,MATCH(ROWS($D$2:D2),COUNTIF($D$2:$D$13,"<="&$D$2:$D$13),0)) into F2. And drag the fill handle down to sort all dates. Please see screenshot 2.
Hope it will help. Have a nice day.Sincerely,Mandy
This comment was minimized by the moderator on the site
Hello,What if i want to do this to multiple columns or even have anew start point in the same column? Do i just do a break and recopy the VBA code in that same window?
Thank you.
This comment was minimized by the moderator on the site
That VBA code is solid gold! Thank you! :-)
This comment was minimized by the moderator on the site
With the VBA code, I have copy and pasted the above but wish for the dates in column F to be the values by which the data is sorted. I've changed the range values to F2 and F3500 (the size of the spreadsheet where row 1 is titles), but it still sorts by the dates in column A. Can somebody help me please?
This comment was minimized by the moderator on the site
Hello, Ross,
When applying the code to column F, you should change some references to your need as below code:
Private Sub Worksheet_Change(ByVal Target As Range)
'Updateby Extendoffice 20160606
On Error Resume Next
If Application.Intersect(Target, Application.Columns(6)) Is Nothing Then Exit Sub
If Target.Count > 1 Then Exit Sub
Range("F1").Sort Key1:=Range("F2"), Order1:=xlAscending, Header:=xlYes, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
End Sub

Please try, hope it can help you!
This comment was minimized by the moderator on the site
Is there a particular formula to keep the cells following the sorted date? It would be nice to organize by date but keep the entire row of information. Any help would be much appreciated.
This comment was minimized by the moderator on the site
I mad a checkbook register and it works but I want to figure out how to make my entry’s to go into date order. Any help would be appreciated. I’m still learning excel.
This comment was minimized by the moderator on the site
In addition to the duplicate dates, is there also a way to include multiple columns of data when it sorts? I need it to include multiple columns and sort them all together with the expiration dates.
This comment was minimized by the moderator on the site
how can I do this same sorting calculation but from newest date to oldest? Currently it is Oldest to Newest. Flipping the < sign isn't enough and beyond that I don't have a strong enough understanding of what it is doing. Also I think what may be happening is excel automatically works top to bottom causing difficulties.
This comment was minimized by the moderator on the site
Hello, Bo,

To auto sort the date from newest to oldest, you just need to change the <= to >= in the above formula as follows:
=INDEX($A$2:$A$15,MATCH(ROWS($A$2:A2),COUNTIF($A$2:$A$15,">="&$A$2:$A$15),0))
After inserting this formula, please remember to press Ctrl + Shift + Enter keys together to get the correct result.
Please try it.
This comment was minimized by the moderator on the site
What if there is a duplicate date in the list? And I want both numbers to show up.
This comment was minimized by the moderator on the site
Hello, Ryan,

To sort the date with duplicate ones, you should apply the following formula:

=IFERROR(INDEX($A$2:$A$11,MATCH(ROWS($A$2:A2),COUNTIF($A$2:$A$11,"<="&$A$2:$A$11),0)),IF(ROWS($A$2:A2)<ROWS($A$2:$A$11),B3,""))

Please remember to press Shift + Ctrl + Enter keys together.

Hope it can help you, thank you!
This comment was minimized by the moderator on the site
Awesome :) Working fine
This comment was minimized by the moderator on the site
U forgot to mention the formula is array and you need to ctrl+Shift+ enter. Luckily you had a screenshot or your page would be a waste of cyberspace
There are no comments posted here yet
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations