2014-10-30 67 views
1

我正在創建一個客戶端寄存器,可以使用按鈕添加客戶端。我也有一個排序功能,可以讓我在klantnr上排序。但問題是,每當我使用排序功能時,它只排序klantnr,並且從表單中提供的附加信息保持不變。例如,這是我的用戶窗體:使用excel對整行進行排序VBA

http://puu.sh/cw94k/ebf4510a24.png

這是我的excel表:

http://puu.sh/cw9nc/3a8e19a989.png

當我添加另一排它按klantnr,但它不其他值,如NaamAdres與它。因此,它需要排序klantnr,並採取與它

其他信息,這是我的代碼:

Private Sub btn_Toevoegen_Click() 
    Dim laatsteKlantNummer As Integer 

Range("B4:B13").End(xlDown).Select 
laatsteKlantNummer = ActiveCell.Value 
ActiveCell.Offset(1, 0).Value = txtKlant + 0 
ActiveCell.Offset(1, 1).Value = txtNaam 
ActiveCell.Offset(1, 2).Value = txtAdres 
ActiveCell.Offset(1, 3).Value = txtWoonplaats 
ActiveCell.Offset(1, 4).Value = txtContact 
Me.Hide 
Range("B4:B13").Sort Key1:=Range("B4:B13"), Order1:=xlAscending 
End Sub 

回答

1

用牀單,而不是一個從範圍對象的排序對象:

ActiveSheet.Sort.SortFields.Clear 
ActiveSheet.Sort.SortFields.Add Key:=Range("B4") 
ActiveSheet.Sort.SetRange Range("B4:F13") 
ActiveSheet.Sort.Apply 

在那裏您可以使用SetRange方法來定義不僅鍵列而且還有其他您想要分類的方法

+0

非常感謝您!這就像一個魅力! – 2014-10-30 12:29:58