Exclude columns in listbox on userform except first two columns & last column

abdelfattah

Well-known Member
Joined
May 3, 2019
Messages
1,429
Office Version
  1. 2019
  2. 2010
Platform
  1. Windows
Hi

I have this part of my project when I select the sheet name from combobox1 will populate about five columns as showing in the code ,but what I want populating specific columns . just populate columns A,B and the last column for each sheet when slect from combobox1.

the last column will change in location (this means dynamic when change in location)

whn populat th last olumn should be filled value an some cells contains blank or filled the whole column , but if the whole column is empty except the header in row1 then shhould ignore it , just bring the last column contains values.

so should exclude the whole columns except A,B and last column , based on my details will populate just three columns with change in location for last column .
here is the code .
VBA Code:
Private Sub UserForm_Initialize()
 
 Crit = ""
  Dim i As Long
  For i = 1 To Sheets.Count
   If Sheets(i).Name <> "DATA" And Sheets(i).Name <> "MAIN" Then
   ComboBox1.AddItem Sheets(i).Name
   End If
  Next
  
  If ComboBox1.ListIndex > -1 Then
    Set ws = Sheets(ComboBox1.Value)
    Call LBoxPop
    
  End If
  If ComboBox1.Value = "" Or ComboBox1.Value <> "" Then CommandButton1.Enabled = False
  
      ListBox1.SetFocus



  
End Sub



Private Sub LBoxPop()
    Dim r          As Long, c As Long
    Dim Data()     As Variant
    Dim rng        As Range
    Dim va
     
   Debug.Print ws.Name
    Set rng = ws.Cells(1, 1).CurrentRegion
'    ReDim Data(1 To rng.Rows.Count, 1 To rng.Columns.Count + 1)
    Data = ws.Cells(1, 1).CurrentRegion.Value
    
    For i = 1 To UBound(Data, 1)
        Data(i, 1) = Format(Data(i, 1), "yyyy-mm-dd")
    Next
    
    For i = 1 To UBound(Data, 1)
        Data(i, 3) = Format(Data(i, 3), "0.00")
        Data(i, 4) = Format(Data(i, 4), "0.00")
        Data(i, 5) = Format(Data(i, 5), "0.00")
    Next

    
     Me.ListBox1.List = Data
 
    With UserForm1.ListBox1
        .ColumnCount = 5
        .columnWidths = "90;300;120;120;100"
        .List = Data
    End With
 
 
End Sub
thanks
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
You can hide the desired columns by setting the column width to zero...

VBA Code:
.columnWidths = "90;300;0;0;100"

Hope this helps!
 
Upvote 0
Hi,
either you misunderstood or my explanation is not clear for you.
look at this Pic
a
ABCDEFGH
1DATECODECOMMEDITYPRODUCTION BYREFFIRSTSECONDTHIRD
205/05/2018cd-mn-1cloth-mn1ITTT-SR11201222
305/06/2018cd-mn-2cloth-mn2ITTT-SR212220
405/07/2018cd-mn-3cloth-mn3ITTT-SR3150
505/08/2018cd-mn-4cloth-mn4ITTT-SR45077
605/09/2018cd-mn-5cloth-mn5ITTT-SR52500
705/10/2018cd-mn-6cloth-mn6CHITT-SR63000
805/11/2018cd-mn-7cloth-mn7CHITT-SR7200777
905/12/2018cd-mn-8cloth-mn8CHITT-SR81500
105/13/2018cd-mn-9cloth-mn9CHITT-SR960
115/14/2018cd-mn-10cloth-mn10CHITT-SR10200
125/15/2018cd-mn-11cloth-mn1ITTT-SR11200
135/16/2018cd-mn-1cloth-mn2ITTT-SR21000
145/17/2018cd-mn-2cloth-mn3ITTT-SR3500
155/18/2018cd-mn-3cloth-mn4ITTT-SR42000
165/19/2018cd-mn-4cloth-mn5ITTT-SR5200
175/20/2018cd-mn-5cloth-mn6CHITT-SR61000
185/21/2018cd-mn-6cloth-mn7CHITT-SR71000
195/22/2018cd-mn-7cloth-mn8CHITT-SR850
205/23/2018cd-mn-8cloth-mn9CHITT-SR950444
SH
Cell Formulas
RangeFormula
H3,H11:H18,H9,H6:H7H3=F3*G3


then I want just populate data in list box for column A,B & last column H
another case
a
CDEFGHIJK
1COMMEDITYPRODUCTION BYREFFIRSTSECONDTHIRDFOURTHTHIRDTHIRD
2cloth-mn1ITTT-SR112012221223
3cloth-mn2ITTT-SR2122200
4cloth-mn3ITTT-SR3150
5cloth-mn4ITTT-SR4507778
6cloth-mn5ITTT-SR525000
7cloth-mn6CHITT-SR630000
8cloth-mn7CHITT-SR7200777778
9cloth-mn8CHITT-SR815000
10cloth-mn9CHITT-SR960
11cloth-mn10CHITT-SR102000
12cloth-mn1ITTT-SR112000
13cloth-mn2ITTT-SR210000
14cloth-mn3ITTT-SR35000
15cloth-mn4ITTT-SR420000
16cloth-mn5ITTT-SR52000
17cloth-mn6CHITT-SR610000
18cloth-mn7CHITT-SR710000
19cloth-mn8CHITT-SR850
20cloth-mn9CHITT-SR950444445
SH
Cell Formulas
RangeFormula
H3,J11:J18,H11:H18,J9,H9,J6:J7,H6:H7,J3H3=F3*G3

should populate data in listbox for columns A,B and the last column J , not K because column K is empty should not show .
so should ignore last column if the whole column contains zero or empty cells . when search the last column ( brings the last column contains numeric values even if contains some empty cell or zero.
I hope this details help you.
 
Upvote 0
Hi,

Domenic show you how you can do...
.columnWidths = "90;300;0;0;100"

To show only column A, B, J then
.columnWidths = "90;300;0;0;0;0;0;0;0;100;0"
 
Upvote 0
Hi,

Domenic show you how you can do...
.columnWidths = "90;300;0;0;100"

To show only column A, B, J then
.columnWidths = "90;300;0;0;0;0;0;0;0;100;0"
did you read this?
so should ignore last column if the whole column contains zero or empty cells . when search the last column ( brings the last column contains numeric values even if contains some empty cell or zero.
every time add new columns . so I have to change .columnWidths and columncount inside the code ,will be too much contents columns in listbox I don't need it , this is not practical way . I would deal automatically for the last column when add new columns . the last column should show in third location in listbox not as inside the sheet.I'm not sure if there is way to do that
 
Upvote 0
correction for pic2
some data are missed, sorry !
المصنف1
ABCDEFGHIJK
1DATECODECOMMEDITYPRODUCTION BYREFFIRSTSECONDTHIRDFOURTHFIFTHSIXTH
205/05/2018cd-mn-1cloth-mn1ITTT-SR112012221223
306/05/2018cd-mn-2cloth-mn2ITTT-SR2122200
407/05/2018cd-mn-3cloth-mn3ITTT-SR3150
508/05/2018cd-mn-4cloth-mn4ITTT-SR4507778
609/05/2018cd-mn-5cloth-mn5ITTT-SR525000
710/05/2018cd-mn-6cloth-mn6CHITT-SR630000
811/05/2018cd-mn-7cloth-mn7CHITT-SR7200777778
912/05/2018cd-mn-8cloth-mn8CHITT-SR815000
1013/05/2018cd-mn-9cloth-mn9CHITT-SR960
1114/05/2018cd-mn-10cloth-mn10CHITT-SR102000
1215/05/2018cd-mn-11cloth-mn1ITTT-SR112000
1316/05/2018cd-mn-1cloth-mn2ITTT-SR210000
1417/05/2018cd-mn-2cloth-mn3ITTT-SR35000
1518/05/2018cd-mn-3cloth-mn4ITTT-SR420000
1619/05/2018cd-mn-4cloth-mn5ITTT-SR52000
1720/05/2018cd-mn-5cloth-mn6CHITT-SR610000
1821/05/2018cd-mn-6cloth-mn7CHITT-SR710000
1922/05/2018cd-mn-7cloth-mn8CHITT-SR850
2023/05/2018cd-mn-8cloth-mn9CHITT-SR950444445
SH
Cell Formulas
RangeFormula
H3,J11:J18,H11:H18,J9,H9,J6:J7,H6:H7,J3H3=F3*G3
 
Upvote 0
this is how should populate in listbox
case1
CS1.PNG



case2
CS2.PNG
 
Upvote 0
You can trial this. Adjust userfom name, listbox name and sheet name to suit. HTH. Dave
Code:
Private Sub UserForm_Initialize()
Dim Arr() As Variant, LastRow As Integer, LastCol As Integer
Dim ColCnt As Integer, RowCnt As Integer
With UserForm1.ListBox1
.ColumnCount = 3
.ColumnWidths = "65;65;65"
End With
'loadcolumns to array
With Sheets("Sheet1")
LastRow = .Range("A" & .Rows.Count).End(xlUp).Row - 1
LastCol = .Cells(2, .Columns.Count).End(xlToLeft).Column
End With
ReDim Arr(LastRow, 2)
For RowCnt = 0 To LastRow
For ColCnt = 0 To 1
Arr(RowCnt, ColCnt) = Sheets("Sheet1").Cells(RowCnt + 1, ColCnt + 1)
Next ColCnt
Next RowCnt
For RowCnt = 0 To LastRow
Arr(RowCnt, ColCnt) = Sheets("Sheet1").Cells(RowCnt + 1, LastCol)
Next RowCnt
'load array to listbox
For RowCnt = 0 To LastRow
With UserForm1.ListBox1
 .AddItem
 .List(RowCnt, 0) = Arr(RowCnt, 0)
 .List(RowCnt, 1) = Arr(RowCnt, 1)
 .List(RowCnt, 2) = Arr(RowCnt, 2)
End With
Next RowCnt
End Sub
 
Last edited:
Upvote 0
Simpler...
Code:
Private Sub UserForm_Initialize()
Dim Arr() As Variant, LastRow As Integer, LastCol As Integer
Dim ColCnt As Integer, RowCnt As Integer
With UserForm1.ListBox1
.ColumnCount = 3
.ColumnWidths = "65;65;65"
End With
'loadcolumns to array
With Sheets("Sheet1")
LastRow = .Range("A" & .Rows.Count).End(xlUp).Row - 1
LastCol = .Cells(2, .Columns.Count).End(xlToLeft).Column
End With
ReDim Arr(LastRow, 2)
For RowCnt = 0 To LastRow
For ColCnt = 0 To 1
Arr(RowCnt, ColCnt) = Sheets("Sheet1").Cells(RowCnt + 1, ColCnt + 1)
Next ColCnt
Arr(RowCnt, 2) = Sheets("Sheet1").Cells(RowCnt + 1, LastCol)
Next RowCnt
'load listbox
UserForm1.ListBox1.List = Arr
End Sub
Dave
 
Last edited:
Upvote 0
Awesome !
just question , is there way to make the third column in third location in listbox instead of depends on original location inside sheet because will make big spaces between second and third columns .
 
Upvote 0

Forum statistics

Threads
1,215,071
Messages
6,122,963
Members
449,094
Latest member
Anshu121

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top