Listboxta son işlemi başta göstermek

Katılım
1 Mart 2005
Mesajlar
82
merhabalar. verileri textboxa girip kaydet dediğimde listbox1 e verileri alıyor. verilerim Günlük!a2:j1000 arasında. istediğim şey kayıt yaptığımda listbox son işlemi başa alsın. yani kayıt işlemi yapıldıkça son işlemden ilk işleme göre sıralasın. forumdaki bir çok kodu aldım denedim olmadı. basit şekilde yazabilecek arkadaş varsa sevinirim
 

muhasebeciyiz

Altın Üye
Katılım
10 Şubat 2006
Mesajlar
906
Excel Vers. ve Dili
Office 2016
64 Bit
Altın Üyelik Bitiş Tarihi
21-12-2027
Kod:
Private Sub ListeyiYenile()
    Dim i As Long
    Dim sayfa As Worksheet
    Set sayfa = ThisWorkbook.Sheets("Günlük")

    ListBox1.Clear

    For i = 1000 To 2 Step -1
        If sayfa.Cells(i, 1).Value <> "" Then
            ListBox1.AddItem sayfa.Cells(i, 1).Value
           
        End If
    Next i
End Sub
Bu kodda:
i = 1000 To 2 Step -1 ile döngü sondan başlıyor.
Hücre doluysa ListBox’a ekleniyor.
En yeni veri her zaman üstte oluyor.

Alternatif olarak
Yeni bir kayıt eklendiğinde, önce eski listeyi saklayıp, ardından yeni veriyi en başa koyabilir ve ardından eskileri ekleyebilirsin. Bu, daha dinamik ve kontrol edilebilir bir yöntemdir ama biraz daha fazla kod gerektirir.

Eğer formdan girilen verileri bir tabloya kaydedip oradan ListBox’a çekiyorsan, kayıt işleminden sonra ListeyiYenile fonksiyonunu çağırmak yeterli olur.
 

muhasebeciyiz

Altın Üye
Katılım
10 Şubat 2006
Mesajlar
906
Excel Vers. ve Dili
Office 2016
64 Bit
Altın Üyelik Bitiş Tarihi
21-12-2027
Aşağıdaki kodla TextBox'lardan verileri Günlük sayfasına kaydedersin.Son boş satıra eklenir.Ardından ListBox1, en son işlem en üstte olacak şekilde yenilenir.

Kod:
Private Sub btnKaydet_Click()
    Dim sayfa As Worksheet
    Set sayfa = ThisWorkbook.Sheets("Günlük")
    
    Dim sonSatir As Long
    sonSatir = sayfa.Cells(sayfa.Rows.Count, "A").End(xlUp).Row + 1
    
    sayfa.Cells(sonSatir, 1).Value = TextBox1.Text
    sayfa.Cells(sonSatir, 2).Value = TextBox2.Text
    sayfa.Cells(sonSatir, 3).Value = TextBox3.Text
    sayfa.Cells(sonSatir, 4).Value = TextBox4.Text
    sayfa.Cells(sonSatir, 5).Value = TextBox5.Text
    sayfa.Cells(sonSatir, 6).Value = TextBox6.Text
    sayfa.Cells(sonSatir, 7).Value = TextBox7.Text
    sayfa.Cells(sonSatir, 8).Value = TextBox8.Text
    sayfa.Cells(sonSatir, 9).Value = TextBox9.Text
    sayfa.Cells(sonSatir, 10).Value = TextBox10.Text
  
    Call ListeyiYenile
End Sub
Listboxu güncelleyen kod
Kod:
Private Sub ListeyiYenile()
    Dim sayfa As Worksheet
    Set sayfa = ThisWorkbook.Sheets("Günlük")

    Dim i As Long
    Dim sonSatir As Long
    ListBox1.Clear
    
    ListBox1.ColumnCount = 10
    sonSatir = sayfa.Cells(sayfa.Rows.Count, "A").End(xlUp).Row

    For i = sonSatir To 2 Step -1
        If sayfa.Cells(i, 1).Value <> "" Then
            ListBox1.AddItem sayfa.Cells(i, 1).Value
            ListBox1.List(ListBox1.ListCount - 1, 1) = sayfa.Cells(i, 2).Value
            ListBox1.List(ListBox1.ListCount - 1, 2) = sayfa.Cells(i, 3).Value
            ListBox1.List(ListBox1.ListCount - 1, 3) = sayfa.Cells(i, 4).Value
            ListBox1.List(ListBox1.ListCount - 1, 4) = sayfa.Cells(i, 5).Value
            ListBox1.List(ListBox1.ListCount - 1, 5) = sayfa.Cells(i, 6).Value
            ListBox1.List(ListBox1.ListCount - 1, 6) = sayfa.Cells(i, 7).Value
            ListBox1.List(ListBox1.ListCount - 1, 7) = sayfa.Cells(i, 8).Value
            ListBox1.List(ListBox1.ListCount - 1, 8) = sayfa.Cells(i, 9).Value
            ListBox1.List(ListBox1.ListCount - 1, 9) = sayfa.Cells(i, 10).Value
        End If
    Next i
End Sub
Eğer verilerin başlığı 1. satırdaysa döngüyü To 2 Step -1 ile başlatman doğru olur. Aksi halde To 1 Step -1 yaparsın.
ListBox'ın ColumnWidths özelliğini formda ayarlayarak daha düzgün bir görünüm elde edebilirsin.
 

Muzaffer Ali

Destek Ekibi
Destek Ekibi
Katılım
5 Haziran 2006
Mesajlar
6,491
Excel Vers. ve Dili
2019 Türkçe
Merhaba.
Her yeni kayıt yapıldığında listeyi silip yeniden yüklemeye gerek yok.
Son kaydedilen listbox un en üstüne eklenebilir.

Alternatif kod.
Kod:
Private Sub btnKaydet_Click()
    Dim SonSatir As Long
    
    With ThisWorkbook.Sheets("Günlük")
        SonSatir = .Cells(Rows.Count, "A").End(xlUp).Row + 1
        .Cells(SonSatir, "A").Value = TextBox1.Text
        .Cells(SonSatir, "B").Value = TextBox2.Text
        .Cells(SonSatir, "C").Value = TextBox3.Text
        .Cells(SonSatir, "D").Value = TextBox4.Text
        .Cells(SonSatir, "E").Value = TextBox5.Text
        .Cells(SonSatir, "F").Value = TextBox6.Text
        .Cells(SonSatir, "G").Value = TextBox7.Text
        .Cells(SonSatir, "H").Value = TextBox8.Text
        .Cells(SonSatir, "I").Value = TextBox9.Text
        .Cells(SonSatir, "J").Value = TextBox10.Text
    End With
    
    With ListBox1
        .AddItem 0, 0
        .List(0, 0) = TextBox1.Text
        .List(0, 1) = TextBox2.Text
        .List(0, 2) = TextBox3.Text
        .List(0, 3) = TextBox4.Text
        .List(0, 4) = TextBox5.Text
        .List(0, 5) = TextBox6.Text
        .List(0, 6) = TextBox7.Text
        .List(0, 7) = TextBox8.Text
        .List(0, 8) = TextBox9.Text
        .List(0, 9) = TextBox10.Text
    End With
End Sub

Private Sub UserForm_Initialize()
    Dim Bak As Long
    Dim SonSatir As Long
    
    With ThisWorkbook.Sheets("Günlük")
        ListBox1.Clear
        ListBox1.ColumnCount = 10
        SonSatir = .Cells(.Rows.Count, "A").End(xlUp).Row
    
        For Bak = SonSatir To 2 Step -1
            ListBox1.AddItem .Cells(Bak, "A").Value
            ListBox1.List(ListBox1.ListCount - 1, 1) = .Cells(Bak, "B").Value
            ListBox1.List(ListBox1.ListCount - 1, 2) = .Cells(Bak, "C").Value
            ListBox1.List(ListBox1.ListCount - 1, 3) = .Cells(Bak, "D").Value
            ListBox1.List(ListBox1.ListCount - 1, 4) = .Cells(Bak, "E").Value
            ListBox1.List(ListBox1.ListCount - 1, 5) = .Cells(Bak, "F").Value
            ListBox1.List(ListBox1.ListCount - 1, 6) = .Cells(Bak, "G").Value
            ListBox1.List(ListBox1.ListCount - 1, 7) = .Cells(Bak, "H").Value
            ListBox1.List(ListBox1.ListCount - 1, 8) = .Cells(Bak, "I").Value
            ListBox1.List(ListBox1.ListCount - 1, 9) = .Cells(Bak, "J").Value
        Next
    End With
End Sub
 

Ekli dosyalar

Korhan Ayhan

Administrator
Yönetici
Admin
Katılım
15 Mart 2005
Mesajlar
43,200
Excel Vers. ve Dili
Microsoft 365 Tr-En 64 Bit
Merhaba,

Verilerinizde bir sıra numarası alanı varsa işlemler ADO ile daha hızlı yapılabilir. Veriler forma yüklenirken arka planda tersten sıralanarak Listbox nesnesine yüklenebilir.
 
Katılım
1 Mart 2005
Mesajlar
82
Merhaba.
Her yeni kayıt yapıldığında listeyi silip yeniden yüklemeye gerek yok.
Son kaydedilen listbox un en üstüne eklenebilir.

Alternatif kod.
Kod:
Private Sub btnKaydet_Click()
    Dim SonSatir As Long
   
    With ThisWorkbook.Sheets("Günlük")
        SonSatir = .Cells(Rows.Count, "A").End(xlUp).Row + 1
        .Cells(SonSatir, "A").Value = TextBox1.Text
        .Cells(SonSatir, "B").Value = TextBox2.Text
        .Cells(SonSatir, "C").Value = TextBox3.Text
        .Cells(SonSatir, "D").Value = TextBox4.Text
        .Cells(SonSatir, "E").Value = TextBox5.Text
        .Cells(SonSatir, "F").Value = TextBox6.Text
        .Cells(SonSatir, "G").Value = TextBox7.Text
        .Cells(SonSatir, "H").Value = TextBox8.Text
        .Cells(SonSatir, "I").Value = TextBox9.Text
        .Cells(SonSatir, "J").Value = TextBox10.Text
    End With
   
    With ListBox1
        .AddItem 0, 0
        .List(0, 0) = TextBox1.Text
        .List(0, 1) = TextBox2.Text
        .List(0, 2) = TextBox3.Text
        .List(0, 3) = TextBox4.Text
        .List(0, 4) = TextBox5.Text
        .List(0, 5) = TextBox6.Text
        .List(0, 6) = TextBox7.Text
        .List(0, 7) = TextBox8.Text
        .List(0, 8) = TextBox9.Text
        .List(0, 9) = TextBox10.Text
    End With
End Sub

Private Sub UserForm_Initialize()
    Dim Bak As Long
    Dim SonSatir As Long
   
    With ThisWorkbook.Sheets("Günlük")
        ListBox1.Clear
        ListBox1.ColumnCount = 10
        SonSatir = .Cells(.Rows.Count, "A").End(xlUp).Row
   
        For Bak = SonSatir To 2 Step -1
            ListBox1.AddItem .Cells(Bak, "A").Value
            ListBox1.List(ListBox1.ListCount - 1, 1) = .Cells(Bak, "B").Value
            ListBox1.List(ListBox1.ListCount - 1, 2) = .Cells(Bak, "C").Value
            ListBox1.List(ListBox1.ListCount - 1, 3) = .Cells(Bak, "D").Value
            ListBox1.List(ListBox1.ListCount - 1, 4) = .Cells(Bak, "E").Value
            ListBox1.List(ListBox1.ListCount - 1, 5) = .Cells(Bak, "F").Value
            ListBox1.List(ListBox1.ListCount - 1, 6) = .Cells(Bak, "G").Value
            ListBox1.List(ListBox1.ListCount - 1, 7) = .Cells(Bak, "H").Value
            ListBox1.List(ListBox1.ListCount - 1, 8) = .Cells(Bak, "I").Value
            ListBox1.List(ListBox1.ListCount - 1, 9) = .Cells(Bak, "J").Value
        Next
    End With
End Sub
Muzaffer bey çok uğraştım devamlı hata veriyor ilgili yerleride değiştirdim olmadı. bi bakabilirmisiniz size zahmet dosyayı ekliyorum.
 

Muzaffer Ali

Destek Ekibi
Destek Ekibi
Katılım
5 Haziran 2006
Mesajlar
6,491
Excel Vers. ve Dili
2019 Türkçe
Satış formunda iki tane ListBox var ikisi de mi tersten sıralanacak?
 

Muzaffer Ali

Destek Ekibi
Destek Ekibi
Katılım
5 Haziran 2006
Mesajlar
6,491
Excel Vers. ve Dili
2019 Türkçe
Günlük sayfasına nerede ve hangi butona basarak yada hangi kodlarla yeni eklemeler yapıyorsunuz bulamadım.
Ama listbox1'e veriler tersten yüklenecek şekilde kodladım.
 

Ekli dosyalar

Muzaffer Ali

Destek Ekibi
Destek Ekibi
Katılım
5 Haziran 2006
Mesajlar
6,491
Excel Vers. ve Dili
2019 Türkçe
Satış adlı formun UserForm_Initialize kodlarını silip aşağıdakileri ekle.

Kod:
Private Sub UserForm_Initialize()
    dolu_son_satır = Sheets("Günlük").Cells(65536, "A").End(xlUp).Row
    ListBox1_Yukle
    Satirsayisi = ListBox1.ListCount
    q = Sheets("Günlük").Range("1:1").End(xlToRight).Column
    w = Sheets("Günlük").Range("a10000").End(xlUp).Row
    For a = 1 To q
        ComboBox5.AddItem Sheets("Günlük").Cells(1, a)
    Next
    ListBox2.ColumnCount = q
    i = Sheets("Günlük").Range(Sheets("Günlük").Cells(1, 1), Sheets("Günlük").Cells(w, q))
    ListBox2.List = i
    ComboBox2.AddItem "Kredi Kartı"
    ComboBox2.AddItem "Peşin"
    ComboBox2.AddItem "Veresiye"
    ComboBox2.AddItem "İban Eft"
    ComboBox2.AddItem "İkram"
End Sub

Sub ListBox1_Yukle()
    Dim Bak As Long
    Dim SonSatir As Long
    
    With ThisWorkbook.Sheets("Günlük")
        ListBox1.Clear
        ListBox1.ColumnCount = 10
        SonSatir = .Cells(.Rows.Count, "A").End(xlUp).Row
    
        For Bak = SonSatir To 2 Step -1
            ListBox1.AddItem .Cells(Bak, "A").Value
            ListBox1.List(ListBox1.ListCount - 1, 1) = .Cells(Bak, "B").Value
            ListBox1.List(ListBox1.ListCount - 1, 2) = .Cells(Bak, "C").Value
            ListBox1.List(ListBox1.ListCount - 1, 3) = .Cells(Bak, "D").Value
            ListBox1.List(ListBox1.ListCount - 1, 4) = .Cells(Bak, "E").Value
            ListBox1.List(ListBox1.ListCount - 1, 5) = .Cells(Bak, "F").Value
            ListBox1.List(ListBox1.ListCount - 1, 6) = .Cells(Bak, "G").Value
            ListBox1.List(ListBox1.ListCount - 1, 7) = .Cells(Bak, "H").Value
            ListBox1.List(ListBox1.ListCount - 1, 8) = .Cells(Bak, "I").Value
            ListBox1.List(ListBox1.ListCount - 1, 9) = .Cells(Bak, "J").Value
        Next
    End With
End Sub
 
Katılım
1 Mart 2005
Mesajlar
82
Satış adlı formun UserForm_Initialize kodlarını silip aşağıdakileri ekle.

Kod:
Private Sub UserForm_Initialize()
    dolu_son_satır = Sheets("Günlük").Cells(65536, "A").End(xlUp).Row
    ListBox1_Yukle
    Satirsayisi = ListBox1.ListCount
    q = Sheets("Günlük").Range("1:1").End(xlToRight).Column
    w = Sheets("Günlük").Range("a10000").End(xlUp).Row
    For a = 1 To q
        ComboBox5.AddItem Sheets("Günlük").Cells(1, a)
    Next
    ListBox2.ColumnCount = q
    i = Sheets("Günlük").Range(Sheets("Günlük").Cells(1, 1), Sheets("Günlük").Cells(w, q))
    ListBox2.List = i
    ComboBox2.AddItem "Kredi Kartı"
    ComboBox2.AddItem "Peşin"
    ComboBox2.AddItem "Veresiye"
    ComboBox2.AddItem "İban Eft"
    ComboBox2.AddItem "İkram"
End Sub

Sub ListBox1_Yukle()
    Dim Bak As Long
    Dim SonSatir As Long
   
    With ThisWorkbook.Sheets("Günlük")
        ListBox1.Clear
        ListBox1.ColumnCount = 10
        SonSatir = .Cells(.Rows.Count, "A").End(xlUp).Row
   
        For Bak = SonSatir To 2 Step -1
            ListBox1.AddItem .Cells(Bak, "A").Value
            ListBox1.List(ListBox1.ListCount - 1, 1) = .Cells(Bak, "B").Value
            ListBox1.List(ListBox1.ListCount - 1, 2) = .Cells(Bak, "C").Value
            ListBox1.List(ListBox1.ListCount - 1, 3) = .Cells(Bak, "D").Value
            ListBox1.List(ListBox1.ListCount - 1, 4) = .Cells(Bak, "E").Value
            ListBox1.List(ListBox1.ListCount - 1, 5) = .Cells(Bak, "F").Value
            ListBox1.List(ListBox1.ListCount - 1, 6) = .Cells(Bak, "G").Value
            ListBox1.List(ListBox1.ListCount - 1, 7) = .Cells(Bak, "H").Value
            ListBox1.List(ListBox1.ListCount - 1, 8) = .Cells(Bak, "I").Value
            ListBox1.List(ListBox1.ListCount - 1, 9) = .Cells(Bak, "J").Value
        Next
    End With
End Sub
abi çok teşekkür ederim hata veriyor ama dur bakalım çözeceğim inşallah. tekrar teşekkürler
 
Üst