Populate to Listbox depending of range and column

Gemitec

New Member
Joined
Jun 22, 2021
Messages
20
Office Version
  1. 2013
Platform
  1. Windows
Hello

My question is: Is there a possibility to populate Data in a Listbox, where X Rows and Y columns are met, but only show the columns where columns are not empty?

As you see on the picture, your see there are some rows without data in columns.
So my Aim is to populate the rows and DELETE the columns, if columns have no data

I uploaded a picture, there you see, I dont need Columne J:P , R:S, U:X as an example, but these can difference

(The basic program is set up, I just need to programatically delete all those columnes in a range, what have no data (are empty))

Thank you
 

Attachments

  • aaaaaa.png
    aaaaaa.png
    20 KB · Views: 22

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Is there a possibility to populate Data in a Listbox, where X Rows and Y columns are met, but only show the columns where columns are not empty?

How about:

VBA Code:
Private Sub CommandButton1_Click()
  Dim i As Long, j As Long
  Dim a As Variant
  Dim xWidth As String
  Dim xData As Boolean
  
  With Sheets("Sheet1")
    a = .Range("C6:AB" & .Range("C" & Rows.Count).End(3).Row).Value
    ReDim b(1 To UBound(a, 1), 1 To UBound(a, 2))
    For j = 1 To UBound(a, 2)
      xData = False
      For i = 2 To UBound(a, 1)
        If a(i, j) <> "" Then xData = True: Exit For
      Next
      xWidth = IIf(xData, xWidth & Int(.Cells(6, j + 2).Width + 1) & " pt;", xWidth & "0 pt;")
    Next
  End With
  With ListBox1
    .ColumnCount = UBound(a, 2)
    .ColumnWidths = xWidth
    .List = a
  End With
End Sub
 
Upvote 0
Solution
I'm glad to help you. Thanks for the feedback.
 
Upvote 0
Really helped me a lot, but Your code doesnt work if the first Cell is empty.

So I made some changes, but now I struggeling that the column widths in the Listbox9 (in this example) are not equal to the size of the worksheet cells width. Doesnt matter actually, I can live with it :)
The range is from A6:AB, but the data that has to be listed are from I6:AB. Other listboxes (Listbox1-Listbox8) fills the sheet with data, and then your code comes in. (as you see on the picture, each row has different values, some columnes are empty and I didnt want those to bee shown, it works perfectly. I just placed artcleNr into Columne I, so no cells are empty there and the code works like this)

Dim rngData As Range
Dim i As Long, j As Long
Dim a As Variant
Dim xWidth As String
Dim xData As Boolean

Sheets("Article Calculation").Select

Set rngData = ArticleCriteria.Range("A6").CurrentRegion

ListBox9.ColumnHeads = True
ListBox9.ColumnCount = rngData.Columns.Count - 8

Set rngData = rngData.Resize(rngData.Rows.Count - 1).Offset(1, rngData.Columns.Count - ListBox9.ColumnCount)

With Sheets("Article Calculation")
a = .Range("i6:AB" & .Range("i" & Rows.Count).End(3).Row).Value

ReDim b(1 To UBound(a, 1), 1 To UBound(a, 2))
For j = 1 To UBound(a, 2)
xData = False
For i = 2 To UBound(a, 1)
If a(i, j) <> "" Then xData = True: Exit For
Next
xWidth = IIf(xData, xWidth & Int(.Cells(6, j + 0).Width + 0) & " pt;", xWidth & "0 pt;")
Next
End With

ListBox9.RowSource = "'" & rngData.Parent.Name & "'!" & rngData.Address

With ArtikelSuche.ListBox9
.ColumnWidths = xWidth
End With

End Sub
 

Attachments

  • bbbbbb.png
    bbbbbb.png
    23.5 KB · Views: 22
  • cccccc.png
    cccccc.png
    13.5 KB · Views: 22
Last edited:
Upvote 0
Your code doesnt work if the first Cell is empty.
Which cell?

You can put real examples and show here the example when you have the problem.
Saying "Your code doesn't work if the first Cell is empty" doesn't help enough.
I am happy to make the necessary changes but help me with some examples.

Use code tag to put code.
Use XL2BB tool to put minisheets.
 
Last edited:
Upvote 0
Hello, sure I show you, but as I said I fixed it.
This code is yours, I just made other range (from I:AB)
So if "I" is empty, Listbox9 return no data. Please see attachments.

If I use this code from you

Private Sub Listbox9fill()

Dim i As Long, j As Long
Dim a As Variant
Dim xWidth As String
Dim xData As Boolean

With Sheets("Article Calculation")
a = .Range("i6:AB" & .Range("i" & Rows.Count - 1).End(3).Row).Value

ReDim b(1 To UBound(a, 1), 1 To UBound(a, 2))
For j = 1 To UBound(a, 2)
xData = False
For i = 2 To UBound(a, 1)
If a(i, j) <> "" Then xData = True: Exit For
Next
xWidth = IIf(xData, xWidth & Int(.Cells(6, j + 2).Width + 1) & " pt;", xWidth & "0 pt;")
Next
End With
With ArtikelSuche.ListBox9
ListBox9.ColumnHeads = True

.ColumnCount = UBound(a, 2)
.ColumnWidths = xWidth
.List = a
End With

End Sub


Then The error happens here, because if the first checked cell empty, your code hides everything after.

The Data comes from a sheet where all other listboxes filter out but that doesnt have to do nothing.

My code also put in some Header into listbox, since I also need those info, see A4.png

But your code helped a lot, thanks Sir
 

Attachments

  • doesnt not find value, since I is empty, see A1.png.png
    doesnt not find value, since I is empty, see A1.png.png
    25.5 KB · Views: 15
  • a1.png
    a1.png
    32.3 KB · Views: 14
  • finds value, since I not empty, see A3.png.png
    finds value, since I not empty, see A3.png.png
    29.5 KB · Views: 13
  • a3.png
    a3.png
    37.1 KB · Views: 14
  • a4.png
    a4.png
    17.9 KB · Views: 15
Upvote 0
I could upload a test file for you , but I dont know how to do that
 
Upvote 0
So if "I" is empty, Listbox9 return no data. Please see attachments.

Generally, a column is taken as the base, assuming that column will have data, but if it has no data or there is no "base" column then we have to read all the cells.
Change this:

VBA Code:
With Sheets("Article Calculation")
a = .Range("i6:AB" & .Range("i" & Rows.Count - 1).End(3).Row).Value

For this:
VBA Code:
  With Sheets("Article Calculation")
    lr = .Cells.Find("*", , xlValues, , xlByRows, xlPrevious).Row
    a = .Range("I6:AB" & lr).Value

_____________________

Let me know if you have another detail and we will gladly review it, the idea is to finish your project.
 
Upvote 0
Perfect, thank you very much again
I will stick to the first methode, where first column has to be filled, since I find that more stable version. And besides, I like it in listbox as first column to have the articleNumbers :)
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,429
Messages
6,124,840
Members
449,193
Latest member
MikeVol

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