Display empty cells in rows on the listbox

azizls4

New Member
Joined
Aug 4, 2023
Messages
25
Platform
  1. Windows
hi
i need vba code ,
I have 6 columns and many rows in multiple sheets , I want to display the rows that contains an empty cell in the 5th and 6th columns on listbox.

Meaning that the rows that contain empty cells in columns E and F in all sheets displayed on listbox

thank you
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
I'm going to assume that the listbox is listbox1, that you have it in a userform and that you already configured it with 6 columns.
Try:

VBA Code:
Private Sub UserForm_Activate()
  Dim sh As Worksheet
  Dim f As Range
  Dim i As Long, j As Long, lr As Long, lc As Long
  Dim a As Variant
  
  For Each sh In Sheets
    Set f = sh.Cells.Find("*", , xlValues, xlPart, xlByRows, xlPrevious)
    If Not f Is Nothing Then
      lr = f.Row
      a = sh.Range("A1:F" & lr).Value
      For i = 1 To UBound(a, 1)
        If a(i, 5) = "" And a(i, 6) = "" Then
          With ListBox1
            .AddItem
            For j = 1 To 6
              .List(.ListCount - 1, j - 1) = a(i, j)
            Next
          End With
        End If
      Next
    End If
  Next
End Sub
 
Upvote 0
I'm going to assume that the listbox is listbox1, that you have it in a userform and that you already configured it with 6 columns.
Try:

VBA Code:
Private Sub UserForm_Activate()
  Dim sh As Worksheet
  Dim f As Range
  Dim i As Long, j As Long, lr As Long, lc As Long
  Dim a As Variant
 
  For Each sh In Sheets
    Set f = sh.Cells.Find("*", , xlValues, xlPart, xlByRows, xlPrevious)
    If Not f Is Nothing Then
      lr = f.Row
      a = sh.Range("A1:F" & lr).Value
      For i = 1 To UBound(a, 1)
        If a(i, 5) = "" And a(i, 6) = "" Then
          With ListBox1
            .AddItem
            For j = 1 To 6
              .List(.ListCount - 1, j - 1) = a(i, j)
            Next
          End With
        End If
      Next
    End If
  Next
End Sub
The code is very magnificent, thank you.

There is only a simple thing that I want to modify.

The code only displays column A in the listbox. Is it possible to display in the listbox column A and column B and the sheet that contains empty cell ?
 
Upvote 0
The code only displays column A in the listbox.
You must modify the properties of your listbox for 6 columns.

Is it possible to display in the listbox column A and column B and the sheet that contains empty cell ?
These are things that you must specify in your original request.

Try the following:
VBA Code:
Private Sub UserForm_Activate()
  Dim sh As Worksheet
  Dim f As Range
  Dim i As Long, j As Long, lr As Long, lc As Long
  Dim a As Variant
  
  With ListBox1
    .ColumnCount = 3
    
    For Each sh In Sheets
      Set f = sh.Cells.Find("*", , xlValues, xlPart, xlByRows, xlPrevious)
      If Not f Is Nothing Then
        lr = f.Row
        a = sh.Range("A1:F" & lr).Value
        For i = 1 To UBound(a, 1)
          If a(i, 5) = "" And a(i, 6) = "" Then
            .AddItem
            .List(.ListCount - 1, 0) = a(i, 1)
            .List(.ListCount - 1, 1) = a(i, 2)
            .List(.ListCount - 1, 2) = sh.Name
          End If
        Next
      End If
    Next
    
  End With
End Sub

🧙‍♂️
 
Upvote 1
Solution
You must modify the properties of your listbox for 6 columns.


These are things that you must specify in your original request.

Try the following:
VBA Code:
Private Sub UserForm_Activate()
  Dim sh As Worksheet
  Dim f As Range
  Dim i As Long, j As Long, lr As Long, lc As Long
  Dim a As Variant
 
  With ListBox1
    .ColumnCount = 3
   
    For Each sh In Sheets
      Set f = sh.Cells.Find("*", , xlValues, xlPart, xlByRows, xlPrevious)
      If Not f Is Nothing Then
        lr = f.Row
        a = sh.Range("A1:F" & lr).Value
        For i = 1 To UBound(a, 1)
          If a(i, 5) = "" And a(i, 6) = "" Then
            .AddItem
            .List(.ListCount - 1, 0) = a(i, 1)
            .List(.ListCount - 1, 1) = a(i, 2)
            .List(.ListCount - 1, 2) = sh.Name
          End If
        Next
      End If
    Next
   
  End With
End Sub

🧙‍♂️
thank you so much , it’s perfect
I appreciate that
 
Upvote 0

Forum statistics

Threads
1,215,174
Messages
6,123,451
Members
449,100
Latest member
sktz

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