![]() |
how to delete unselected columns in a table, thanks
Dear all,
I'm testing an userform in VBA of Excel, there are two listboxes in it. I can make user move fields name from left listbox to right listbox, by using ListBox2.AddItem ListBox1.Text but I don't know how to continue, because the result I want to realize is, based on added items in listbox2, my datatable would ONLY show those fields whose name is shown in listbox2 (in the beginning, there are 'name', 'age', 'class', 'score' and 'comments' in the table, for instance, if only 'name' and 'age' are shown in listbox2, the table would only show recorders in 'name' and 'age' fields), I'm thinking to delete fields whose name isn't shown in listbox2. Would you give me some suggestion?Thanks. JIANG |
how to delete unselected columns in a table, thanks
Something like this :-
'------------------------------------------------------ Private Sub CommandButton1_Click() Dim ColumnName As String Dim ListBoxItem As String Dim MatchColumn As Boolean '--------------------------- c = 1 Do MatchColumn = False ColumnName = UCase(ActiveSheet.Cells(1, c).Value) For i = 0 To ListBox2.ListCount - 1 ListBoxItem = UCase(ListBox2.List(i)) If ListBoxItem = ColumnName Then MatchColumn = True Exit For End If Next If MatchColumn = False Then ActiveSheet.Columns(c).EntireColumn.Delete Else c = c + 1 End If Loop While ActiveSheet.Cells(1, c).Value < "" '--------------------------- End Sub '---------------------------------------- -- Message posted from http://www.ExcelForum.com |
how to delete unselected columns in a table, thanks
Thanks.
I'm trying. best regards jiang "BrianB " wrote in message ... Something like this :- '------------------------------------------------------ Private Sub CommandButton1_Click() Dim ColumnName As String Dim ListBoxItem As String Dim MatchColumn As Boolean '--------------------------- c = 1 Do MatchColumn = False ColumnName = UCase(ActiveSheet.Cells(1, c).Value) For i = 0 To ListBox2.ListCount - 1 ListBoxItem = UCase(ListBox2.List(i)) If ListBoxItem = ColumnName Then MatchColumn = True Exit For End If Next If MatchColumn = False Then ActiveSheet.Columns(c).EntireColumn.Delete Else c = c + 1 End If Loop While ActiveSheet.Cells(1, c).Value < "" '--------------------------- End Sub '----------------------------------------- --- Message posted from http://www.ExcelForum.com/ |
All times are GMT +1. The time now is 06:25 PM. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com