+ Reply to Thread
Results 1 to 16 of 16

Look for each listbox item in .UsedRange

Hybrid View

SIMBAtheCAT Look for each listbox item in... 07-15-2014, 09:45 AM
Andy Pope Re: Look for each listbox... 07-15-2014, 10:04 AM
SIMBAtheCAT Re: Look for each listbox... 07-15-2014, 10:11 AM
Andy Pope Re: Look for each listbox... 07-15-2014, 10:18 AM
SIMBAtheCAT Re: Look for each listbox... 07-15-2014, 02:06 PM
Andy Pope Re: Look for each listbox... 07-16-2014, 03:30 AM
SIMBAtheCAT Re: Look for each listbox... 07-16-2014, 09:36 AM
Andy Pope Re: Look for each listbox... 07-16-2014, 09:40 AM
SIMBAtheCAT Re: Look for each listbox... 07-16-2014, 10:08 AM
SIMBAtheCAT Re: Look for each listbox... 07-16-2014, 02:47 PM
Andy Pope Re: Look for each listbox... 07-17-2014, 03:47 AM
SIMBAtheCAT Re: Look for each listbox... 07-17-2014, 09:43 AM
Andy Pope Re: Look for each listbox... 07-17-2014, 10:03 AM
SIMBAtheCAT Re: Look for each listbox... 07-17-2014, 11:01 AM
Andy Pope Re: Look for each listbox... 07-17-2014, 11:05 AM
SIMBAtheCAT Re: Look for each listbox... 07-18-2014, 12:44 PM
  1. #1
    Registered User
    Join Date
    06-03-2014
    MS-Off Ver
    2007
    Posts
    87

    Look for each listbox item in .UsedRange

    Hi, Good morning,

    So basically everything is in the title! I have a userform with a ListBox and I'm trying to refer to each particular item in the Listbox for a range but it doesn't work.

    For Each ws In Worksheets
        For i = 400 To 18 Step -1
        
            For a = 1 To 20
            If ws.Cells(i, 1).Value = UserForm2.ListBox2.List(a) Then
            Next a
            
            Else
             
                ws.Cells(i, 1).EntireRow.Delete
                
            End If
        Next i
    Next ws
    Last edited by SIMBAtheCAT; 07-18-2014 at 12:45 PM.

  2. #2
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,482

    Re: Look for each listbox item in .UsedRange

    Sub xx()
        For Each ws In Worksheets
            For i = 400 To 18 Step -1
                For a = 1 To 20
                    If ws.Cells(i, 1).Value = UserForm2.ListBox2.List(a) Then
                         ' do nothing
                    Else
                        ws.Cells(i, 1).EntireRow.Delete
                        exit for 
                    End If
                Next a
            Next i
        Next ws
    End Sub
    Check the indentation of your code and it should be clear why it's not compiling.

    You have a FOR NEXT loop arounf only the IF part of your IF THEN ELSE construct.
    Cheers
    Andy
    www.andypope.info

  3. #3
    Registered User
    Join Date
    06-03-2014
    MS-Off Ver
    2007
    Posts
    87

    Re: Look for each listbox item in .UsedRange

    Tried it but it is just deleting every row.

    I made this little code and it works. However, it's not as efficient as your code would be Andy Pope if it would be working.

    For Each Object In ListBox2.List
        For Each ws In Worksheets
            For i = 400 To 18 Step -1
    
                If ws.Cells(i, 1).Value = Object Then
                ws.Cells(i, 1).Interior.ColorIndex = 8
                
                Else
                End If
            Next i
        Next ws
    Next Object
    
    For Each ws In Worksheets
        For i = 400 To 18 Step -1
        
            If ws.Cells(i, 1).Interior.ColorIndex = 8 Then
            
            Else
             
                ws.Cells(i, 1).EntireRow.Delete
                
            End If
        Next i
        
        With ws.Columns(1)
            .Interior.ColorIndex = 0
        End With
    Next ws

  4. #4
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,482

    Re: Look for each listbox item in .UsedRange

    My bad. You actually want to delete if match rather than if not.

    Sub xx()
        For Each ws In Worksheets
            For i = 400 To 18 Step -1
                For a = 1 To 20
                    If ws.Cells(i, 1).Value = UserForm2.ListBox2.List(a) Then
    ' delete row if cell matches
                        ws.Cells(i, 1).EntireRow.Delete
                        exit for 
                    End If
                Next a
            Next i
        Next ws
    End Sub

  5. #5
    Registered User
    Join Date
    06-03-2014
    MS-Off Ver
    2007
    Posts
    87

    Re: Look for each listbox item in .UsedRange

    No, you were right. I'm trying to keep the rows with the value in ListBox2 and hide the other rows.

    Your code seems to just delete every single rows so I made so minor modifications without success. It still hides/deletes every rows :

        For Each ws In Worksheets
            For i = 400 To 18 Step -1
                ws.Cells(i, 1).EntireRow.Hidden = False
            Next i
        Next ws
    
        For Each ws In Worksheets
            For i = 400 To 18 Step -1
                For Each Object In ListBox2.List
                    If ws.Cells(i, 1).Value = ListBox2.Object Then
                    
                    Else
                        ws.Cells(i, 1).EntireRow.Hidden = True
                    End If
                Next Object
            Next i
        Next ws

  6. #6
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,482

    Re: Look for each listbox item in .UsedRange

    Hide or delete, there is a difference.
    Your original code would suggest delete.

    Can you post workbook example and detail what choices to make so we can see exactly what is happening.

  7. #7
    Registered User
    Join Date
    06-03-2014
    MS-Off Ver
    2007
    Posts
    87

    Re: Look for each listbox item in .UsedRange

    Sure! Here is an image of my userform with the listbox :
    Sans titre.PNG

    The red circle is the objects I want to KEEP! Those objects.text can be found in another workbook always in the first column. When the objects are not in the ListBox2 then I want to HIDE the entire row.

    Here is my code, it works BUT it takes literally 10 minutes to execute... :

    Dim ws As Worksheet, wbthis As Workbook, wbtarget As Workbook, wbtarget1 As Workbook, i As Integer, a As Integer
    With Application
        .ScreenUpdating = False
        .Calculation = xlCalculationManual
    End With
    
    If UserForm2.CheckBox3 = True Then
        Set wbtarget = Workbooks.Open(UserForm2.TextBox3.Text)
        wbtarget.Activate
            For Each Object In UserForm2.ListBox2.List
                For Each ws In wbtarget.Worksheets
                    For i = 400 To 18 Step -1
                        ws.Cells(i, 1).EntireRow.Hidden = False
                        If ws.Cells(i, 1).Value = Object Then
                        ws.Cells(i, 1).Interior.ColorIndex = 8
                        Else
                        End If
                    Next i
                Next ws
            Next Object
            
            For Each ws In wbtarget.Worksheets
                For i = 400 To 18 Step -1
                    If ws.Cells(i, 1).Interior.ColorIndex = 8 Then
                    Else
                        ws.Cells(i, 1).EntireRow.Hidden = True
                    End If
                Next i
                With ws.Columns(1)
                    .Interior.ColorIndex = 0
                End With
            Next ws
        wbtarget.Close
    End If
        
    wbtarget.Save
    wbtarget.Close
    Set wbtarget = Nothing
    End If
    
    With Application
        .ScreenUpdating = True
        .Calculation = xlCalculationAutomatic
    End With
    Last edited by SIMBAtheCAT; 07-16-2014 at 09:45 AM.

  8. #8
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,482

    Re: Look for each listbox item in .UsedRange

    Yeah, not really a lot I can do with a redacted image.

  9. #9
    Registered User
    Join Date
    06-03-2014
    MS-Off Ver
    2007
    Posts
    87

    Re: Look for each listbox item in .UsedRange

    I'm sorry but that is the maximum I can post here... Still I appreciate the will to help.

  10. #10
    Registered User
    Join Date
    06-03-2014
    MS-Off Ver
    2007
    Posts
    87

    Re: Look for each listbox item in .UsedRange

    Instead of starting another thread,

    I'm trying to rename the sheets of another workbook if they don't match the objects/items in a ListBox. However, I always get 'execution error 438.

       For i = 1 To 11
    wbtarget.Activate
    wbtarget.Worksheets(i).Name = wbthis.UserForm2.ListBox4.List(i)
       Next i

  11. #11
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,482

    Re: Look for each listbox item in .UsedRange

    If that code is in the userform code module then you don't need the wbthis reference. You can use the Me object instead.

    wbtarget.Worksheets(i).Name = me.ListBox4.List(i)

  12. #12
    Registered User
    Join Date
    06-03-2014
    MS-Off Ver
    2007
    Posts
    87

    Re: Look for each listbox item in .UsedRange

    No it's not in the userform code. It is in a macro and serves a error handlerpurpose :

    ErrHandler:
        Select Case Err
            Case 9:
                MsgBox "One of the selected sheets does not exist in one or multiple selected workbooks", vbCritical, "WARNING! ERROR"
                    If MsgBox("Do you want to modify the names of the sheets in the selected workbooks?", vbExclamation + vbYesNo, "Modify sheets name") = vbYes Then
                        For i = 1 To 11
                            wbtarget.Activate
                            wbtarget.Worksheets(i).Name = wbthis.UserForm2.ListBox4.List(i)
                        Next i
                    Else
                    End If
                wbtarget.Close
                Set wbtarget = Nothing
                Set wbthis = Nothing
            Case Else
        End Select

  13. #13
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,482

    Re: Look for each listbox item in .UsedRange

    Have you not declared a object reference to you userform?

    if not, and the userform is still in memory, you could resort to using userforms collection.
    vba.UserForms(0).controls("ListBox4").List(i)
    Although I think you would need to determine what index value to use

  14. #14
    Registered User
    Join Date
    06-03-2014
    MS-Off Ver
    2007
    Posts
    87

    Re: Look for each listbox item in .UsedRange

    You are right. I just referred it manually without any loop. I'll dig more into that when I have some time off.

    Could you help me make this code more efficient because when I run it, it just stays in execute mode forever (literally)...

        With Application
            .ScreenUpdating = False
            .Calculation = xlCalculationManual
        End With
        
        For i = 1 To 11
            If UserForm2.Controls("checkbox" & i).Value = True Then
                Set wbtarget = Workbooks.Open(UserForm2.Controls("textbox" & i).Text)
                wbtarget.Activate
                    For Each Object In UserForm2.ListBox3.List
                        wbtarget.Worksheets(Object).Columns(UserForm2.TextBox12.Value).EntireColumn.Delete
                        wbtarget.Worksheets(Object).Rows(UserForm2.TextBox13.Value).EntireRow.Delete
                    Next Object
                Set wbtarget = Nothing
            End If
        Next i
    
        With Application
            .ScreenUpdating = True
            .Calculation = xlCalculationAutomatic
        End With

  15. #15
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,482

    Re: Look for each listbox item in .UsedRange

    Back to issue of needing example workbook

  16. #16
    Registered User
    Join Date
    06-03-2014
    MS-Off Ver
    2007
    Posts
    87

    Re: Look for each listbox item in .UsedRange

    Once again, I can't provide that. However, after a couple of changes, I made it work :

    Sub DeleteLR()
    Dim ws As Worksheet, i As Integer, a As Integer, iiint As Integer
        
        On Error GoTo ErrHandler
        
        If MsgBox("Careful! You have selected the option to delete rows and columns. This action cannot be cancelled after approval. Are you sure you want to continue?", vbYesNo + vbExclamation) = vbNo Then
            Exit Sub
        End If
        
        With Application
            .ScreenUpdating = False
            .Calculation = xlCalculationManual
        End With
        
        Set wbthis = ThisWorkbook
        
        For i = 1 To 10
        Set wbtarget = Nothing
            If UserForm2.Controls("checkbox" & i).Value = True Then
                Set wbtarget = Workbooks.Open(UserForm2.Controls("textbox" & i).Text)
                wbtarget.Activate
                For Each Object In UserForm2.ListBox3.List
                    If IsNull(Object) Then
                        Exit For
                    Else
                        wbtarget.Worksheets(Object).Columns(UserForm2.TextBox12.Value).EntireColumn.Delete
                        wbtarget.Worksheets(Object).Rows(UserForm2.TextBox13.Value).EntireRow.Delete
                    End If
                Next Object
            End If
    continueloop:
        Next i
    
        With Application
            .ScreenUpdating = True
            .Calculation = xlCalculationAutomatic
        End With
    Exit Sub
    
    ErrHandler:
        Select Case Err
            Case 9:
                MsgBox "One of the selected sheets does not exist in one or multiple selected workbooks", vbCritical, "WARNING! ERROR"
                    If MsgBox("Do you want to modify the names of the sheets in the selected workbooks?", vbExclamation + vbYesNo, "Modify sheets name") = vbYes Then
                        wbtarget.Worksheets(1).Name = "...
                        ....
                    Else
                        Exit Sub
                    End If
                Resume
            Case 13:
                GoTo continueloop
        End Select
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Select one item from a listbox disables another listbox
    By EagleInsight in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-22-2014, 09:53 PM
  2. [SOLVED] first item in listbox does not show in listbox
    By cfinch100 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 08-19-2013, 10:33 AM
  3. [SOLVED] Copy one listbox item to same index position in another listbox
    By Foreverlearning in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 05-19-2012, 06:05 AM
  4. Multi select Listbox Items selection based on other Listbox item selection.
    By srinivassathi in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-20-2011, 05:53 AM
  5. Populate ListBox Based On Item Chosen In Another ListBox
    By davemojo82 in forum Excel General
    Replies: 1
    Last Post: 08-04-2009, 08:39 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1