ListBbox ve ComboBox Satır Kaydırma

26 Aralık 2020 14 Şubat 2021 1126

Listbox ve Combobox Satır Kaydırma isimli kodları kullanarak, her iki nesnede satır kaydırma işlemi yapma işlemini gerçekleştirebilirsiniz.

Option Explicit

Private Sub UserForm_Initialize()
    Dim lCounter As Long
    For lCounter = 1 To 1000
    Lst1.AddItem lCounter
    Lst2.AddItem lCounter
    ComboBox1.AddItem lCounter
    ComboBox2.AddItem lCounter
    Next lCounter
    HookWheel Me, Me.Width, Me.Height, 1
End Sub

Private Sub UserForm_Terminate()
    UnHookWheel
End Sub

Option Explicit
Option Private Module

Private Declare Function CallWindowProc Lib "user32.dll" Alias _
"CallWindowProcA" ( _
ByVal lpPrevWndFunc As Long, _
ByVal hWnd As Long, _
ByVal Msg As Long, _
ByVal wParam As Long, _
ByVal lParam As Long) As Long

Private Declare Function SetWindowLong Lib "user32.dll" Alias _
"SetWindowLongA" ( _
ByVal hWnd As Long, _
ByVal nIndex As Long, _
ByVal dwNewLong As Long) As Long
Private Declare Function FindWindowA Lib "user32" ( _
ByVal lpClassName As String, _
ByVal lpWindowName As String) As Long

Private Declare Function GetSystemMetrics Lib "user32" _
(ByVal nIndex As Long) As Long

Private Declare Function GetWindowRect Lib "user32" ( _
ByVal hWnd As Long, lpRect As typeRect) As Long
Private Type typeRect
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Private dXFactor As Double
Private dYFactor As Double
Private lCaptionHeight As Long
Private Const GWL_WNDPROC = -4
Private Const WM_MOUSEWHEEL = &H20A
Private Const SM_MOUSEWHEELPRESENT = 75
Private lLines As Long
Private hForm As Long
Public lPrevWndProc As Long
Private lX As Long
Private lY As Long
Private bUp As Boolean
Private frmContainer As msForms.UserForm

Private Function WindowProc( _
ByVal lWnd As Long, _
ByVal lMsg As Long, _
ByVal wParam As Long, _
ByVal lParam As Long) As Long
If lMsg = WM_MOUSEWHEEL Then
lX = lParam And 65535
lY = lParam \ 65535
bUp = (wParam > 0)
WheelHandler bUp
End If
If lMsg <> WM_MOUSEWHEEL Then
WindowProc = CallWindowProc(lPrevWndProc, lWnd, lMsg, wParam, lParam)
End If
End Function

Public Sub HookWheel(ByVal frmName As msForms.UserForm, dWidth As Double, _
dHeight As Double, ByVal lLinesToScroll As Long)
If WheelPresent Then
Set frmContainer = frmName
hForm = GetFormHandle(frmName)
GetScreenFactors hForm, dWidth, dHeight
lLines = lLinesToScroll
lPrevWndProc = SetWindowLong(hForm, GWL_WNDPROC, AddressOf WindowProc)
End If
End Sub

Public Sub UnHookWheel()
Call SetWindowLong(hForm, GWL_WNDPROC, lPrevWndProc)
End Sub

Private Function GetFormHandle(ByVal frmName As msForms.UserForm, _
Optional bByClass As Boolean = True) As Long
Dim strClassName As String
Dim strCaption As String
strClassName = IIf(Val(Application.Version) > 8, "ThunderDFrame", _
"ThunderXFrame") & vbNullChar
strCaption = vbNullString
GetFormHandle = FindWindowA(strClassName, strCaption)
End Function

Public Sub GetScreenFactors(lHwnd As Long, _
dWidth As Double, _
dHeight As Double)
Dim uRect As typeRect
GetWindowRect lHwnd, uRect
dXFactor = dWidth / (uRect.Right - uRect.Left)
dYFactor = dHeight / (uRect.Bottom - uRect.Top)
lCaptionHeight = dHeight - frmContainer.InsideHeight
End Sub

Private Function WheelPresent() As Boolean
If GetSystemMetrics(SM_MOUSEWHEELPRESENT) Then
WheelPresent = True
ElseIf FindWindowA("MouseZ", "Magellan MSWHEEL") <> 0 Then
WheelPresent = True
End If
End Function

Public Sub WheelHandler(bUp As Boolean)
Dim ctlFocus As msForms.Control
Dim ctlName As msForms.Control
Dim lTopIndex As Long
Dim bMultiPage As Boolean
Dim lPage As Long
Dim lMove As Long
If Not IsOverForm Then Exit Sub
Set ctlFocus = frmContainer.ActiveControl
If TypeOf ctlFocus Is msForms.MultiPage Then
bMultiPage = True
lPage = ctlFocus.Value
Set ctlFocus = ctlFocus.SelectedItem.ActiveControl
End If
lX = lX * dXFactor
lY = lY * dYFactor
lY = lY - lCaptionHeight
If Not (TypeOf ctlFocus Is msForms.CommandButton Or _
TypeOf ctlFocus Is msForms.TextBox) Then
End If
For Each ctlName In frmContainer.Controls
With ctlName
On Error Resume Next
If TypeOf ctlName Is msForms.ListBox Or TypeOf ctlName Is msForms.ComboBox Or TypeOf ctlName Is msForms.TextBox Then
If bMultiPage = True Then
If lPage <> .Parent.Index Then GoTo SkipControl
End If
If lX > .Left Then
If lX < .Left + .Width Then
If lY > .Top Then
If lY < .Top + .Height Then
If .ListCount = 0 Then Exit Sub
lMove = IIf(bUp, -lLines, lLines)
lTopIndex = .ListIndex + lMove
If lTopIndex < 0 Then
lTopIndex = 0
ElseIf lTopIndex > .ListCount - (.Height / 10) + 2 Then
lTopIndex = .ListIndex
End If
If lTopIndex < 0 Then lTopIndex = 0
.ListIndex = lTopIndex
Exit Sub
End If
End If
End If
End If
End If
End With
SkipControl:
Next ctlName
End Sub

Public Function IsOverForm() As Boolean
Dim uRect As typeRect
GetWindowRect hForm, uRect
    With uRect
        If lX >= .Left Then
            If lX <= .Right Then
                If lY >= .Top Then
                    If lY <= .Bottom Then
                        IsOverForm = True
                        lX = lX - .Left
                        lY = lY - .Top
                    End If
                End If
            End If
        End If
    End With
End Function

Gerekli Adımlar

Kodu çalıştırmanız için aşağıdaki adımları yerine getirmeniz gerekir.

  1. Microsoft Visual Basic for Applications penceresini (Alt + F11) açın.
  2. Project - VBAProject alanının, ekranın sol tarafında görüldüğünden emin olun. Görünmüyorsa, Ctrl + R kısayolu ile hızlıca açın.
  3. Araç çubuklarından Insert -> UserForm yazısına tıklayın.
  4. Solunda klasör simgesi olan Forms yazısının başındaki + simgesine tıklayın.
  5. Alt kısma eklenecek olan UserForm(1) yazısına çift tıklayın.
  6. Üstteki kodu yapıştırın.

Kod Açıklaması

Bu kod çalıştırmak için bir UserFom nesnesi üzerinde ListBox ve ComboBox nesnesi eklemeniz gerekir.

50 Farklı Hazır Makro (VBA) Kodu
UserForm Açılırken Efekt Ekleme
Dar Kenar Boşluğu Yazdırma
Sütunda Yazılı Olanlardan Sayfa Adı Yapma
X Sayı Atlayarak Satır Gizleme
Tarih Değeri Yazarken Otomatik Ayraç Kullanımı
Seçimdeki Alternatif Satırları Vurgulama
Yazı Efekti Verme Kodu
Makro Çalışırken İmlecin Hareket Etmemesi
Roma Rakamlarını Normal Sayılara Dönüştürme
Bilgisayar Bilgilerini Özetleme
Kes Kopyala ve Başka Çalışma Kitabına Yapıştır
Bir Kitaptaki Sayfaları Başka Kitaba Taşıma
Excel Kapanırken Otomatik İşlem Yaptırma
Google Üzerinde Arama Yapma
TextBox Nesnesine Harf Girilmesini Engelleme
Tek Sütunu Kes Kopyala ve Yapıştır
Seçili Hücrelerdeki Harfleri Büyük Harfe Değiştirme
Sütunun Herhangi Hücresine Çift Tıklayınca UserForm Açılsın
Birden Çok Satır Ekleme
R1C1 Referans Stilini Etkinleştirme
Hücre Aralığını Kes Kopyala ve Yapıştır
Salt Okunur Excel Dosyasını Kontrol Etme
Derece Simgesi Ekleme
Belirli Metni Vurgulama
Bir Dizeden Sayısal Veriler Elde Etme
Bir Sayfadaki Veriyi Diğer Sayfalara Kopyalama
Aktif Sayfayı Kopyala Yeni Bir Kitaba Yapıştır
Tarih ve Saatten Tarihi Kaldırma
Başka Dosyada Şifreli Userform Oluşturmak
Metnin İçinden TC Kimlik Numarasını Ayırma
ListBox Üzerindeki Verilere Çoklu Seçim Yapma
Gizlenmiş Makro Örnekleri
Aç Penceresini Açan Makro
Makro ile Açılan Liste Oluşturma
Açılışta Üç Kere Şifre Hakkı Vermek
Aktif Satırı ve Sütunu Vurgulama
Transparan UserForm Oluşturmak
Makrolar ile Klasör Silme
Hücrenin Arka Plan Rengini Değiştirme
Aktif Hücrenin Sütun Numarasını Bulma
Klavyeye Makro Atama ve Çalıştırma
Yardım Menüsünü Açma
Aktif Hücreye Açıklama Ekleme
PDF Dosyalarını Birleştirme
Toplu Sütun Silme
İki Sütundaki Değerleri Karşılaştırıp Ayrıştırma
Hücredeki Değer Sıfırdan Büyükse İşlem Yapma
Bağlantılı Resim Ekleme
Toplu Satır Silme
Seçili Aralığı PDF Olarak Kaydetme

Daha önce yorum yapılmamış!

Backlink Reklam Bu alanda 50 karakterlik açıklama ile web sitenize ait Backlink Reklam yayınlayabilirsiniz.
Yükleniyor...