PC Review


Reply
Thread Tools Rate Thread

Deleting Rows Based Upon ListBox Selection

 
 
=?Utf-8?B?UGFpZ2U=?=
Guest
Posts: n/a
 
      23rd Aug 2007
Option2ListBox populates with the items contained in Col H of the 'Schedules'
tab; the user can then select 1 or more of these items. For each selected
item, I want Excel to go to the 'Schedules' tab and delete any row that has
that item in Col H. Problem is that the code is not consistently deleting
all the corresponding rows for those items selected in the listbox. This
must be something simple I'm overlooking. Can anyone help???? Also, is
there a quicker way to delete these rows? Am a little concerned about speed,
since there's likely to be several thousand rows to look through.

Private Sub OKButton_Click()
Dim i As Long, j As Long
Dim ws As Worksheet
Dim cells As Range

j = 0

For i = 0 To Me.Option2ListBox.ListCount - 1
If Me.Option2ListBox.Selected(i) = True Then
With worksheets("Schedules")
Set rngToSearch = .Range(.Range("H1"), .cells(Rows.Count,
"H").End(xlUp))
End With
For Each rng In rngToSearch
If rng = Me.Option2ListBox.List(i) Then
rng.EntireRow.Delete
End If
Next
j = j + 1
End If
Next i
Unload ImportingSchedules
End Sub

 
Reply With Quote
 
 
 
 
=?Utf-8?B?VG9tIE9naWx2eQ==?=
Guest
Posts: n/a
 
      23rd Aug 2007
Private Sub OKButton_Click()
Dim i As Long, j As Long
Dim ws As Worksheet
Dim cells As Range

j = 0

For i = 0 To Me.Option2ListBox.ListCount - 1
If Me.Option2ListBox.Selected(i) = True Then
With worksheets("Schedules")
Set lrow = .cells(Rows.Count,"H").End(xlUp).row
set r = .Range("H1").Resize(lrow,1)
End With
if application.Countif(r,Me.Option2Listbox.List(i)) > 0 then
For k = lrow to 1 step -1
set rng = worksheets("Schedules").Cells(k,"H")
If rng = Me.Option2ListBox.List(i) Then
rng.EntireRow.Delete
End If
Next
End if
j = j + 1
End If
Next i
Unload ImportingSchedules
End Sub

--
Regards,
Tom Ogilvy

"Paige" wrote:

> Option2ListBox populates with the items contained in Col H of the 'Schedules'
> tab; the user can then select 1 or more of these items. For each selected
> item, I want Excel to go to the 'Schedules' tab and delete any row that has
> that item in Col H. Problem is that the code is not consistently deleting
> all the corresponding rows for those items selected in the listbox. This
> must be something simple I'm overlooking. Can anyone help???? Also, is
> there a quicker way to delete these rows? Am a little concerned about speed,
> since there's likely to be several thousand rows to look through.
>
> Private Sub OKButton_Click()
> Dim i As Long, j As Long
> Dim ws As Worksheet
> Dim cells As Range
>
> j = 0
>
> For i = 0 To Me.Option2ListBox.ListCount - 1
> If Me.Option2ListBox.Selected(i) = True Then
> With worksheets("Schedules")
> Set rngToSearch = .Range(.Range("H1"), .cells(Rows.Count,
> "H").End(xlUp))
> End With
> For Each rng In rngToSearch
> If rng = Me.Option2ListBox.List(i) Then
> rng.EntireRow.Delete
> End If
> Next
> j = j + 1
> End If
> Next i
> Unload ImportingSchedules
> End Sub
>

 
Reply With Quote
 
=?Utf-8?B?UGFpZ2U=?=
Guest
Posts: n/a
 
      23rd Aug 2007
Thanks, Tom. However, am getting a 'Type Mismatch' error on the 'Set lrow
=...' line of code. What would that be due to?

"Tom Ogilvy" wrote:

> Private Sub OKButton_Click()
> Dim i As Long, j As Long
> Dim ws As Worksheet
> Dim cells As Range
>
> j = 0
>
> For i = 0 To Me.Option2ListBox.ListCount - 1
> If Me.Option2ListBox.Selected(i) = True Then
> With worksheets("Schedules")
> Set lrow = .cells(Rows.Count,"H").End(xlUp).row
> set r = .Range("H1").Resize(lrow,1)
> End With
> if application.Countif(r,Me.Option2Listbox.List(i)) > 0 then
> For k = lrow to 1 step -1
> set rng = worksheets("Schedules").Cells(k,"H")
> If rng = Me.Option2ListBox.List(i) Then
> rng.EntireRow.Delete
> End If
> Next
> End if
> j = j + 1
> End If
> Next i
> Unload ImportingSchedules
> End Sub
>
> --
> Regards,
> Tom Ogilvy
>
> "Paige" wrote:
>
> > Option2ListBox populates with the items contained in Col H of the 'Schedules'
> > tab; the user can then select 1 or more of these items. For each selected
> > item, I want Excel to go to the 'Schedules' tab and delete any row that has
> > that item in Col H. Problem is that the code is not consistently deleting
> > all the corresponding rows for those items selected in the listbox. This
> > must be something simple I'm overlooking. Can anyone help???? Also, is
> > there a quicker way to delete these rows? Am a little concerned about speed,
> > since there's likely to be several thousand rows to look through.
> >
> > Private Sub OKButton_Click()
> > Dim i As Long, j As Long
> > Dim ws As Worksheet
> > Dim cells As Range
> >
> > j = 0
> >
> > For i = 0 To Me.Option2ListBox.ListCount - 1
> > If Me.Option2ListBox.Selected(i) = True Then
> > With worksheets("Schedules")
> > Set rngToSearch = .Range(.Range("H1"), .cells(Rows.Count,
> > "H").End(xlUp))
> > End With
> > For Each rng In rngToSearch
> > If rng = Me.Option2ListBox.List(i) Then
> > rng.EntireRow.Delete
> > End If
> > Next
> > j = j + 1
> > End If
> > Next i
> > Unload ImportingSchedules
> > End Sub
> >

 
Reply With Quote
 
=?Utf-8?B?UGFpZ2U=?=
Guest
Posts: n/a
 
      23rd Aug 2007
Did some digging re the type mismatch; was due to the fact that I hadn't
referenced the correct VBA extensibility library. So now don't get the error
message; however, it does not delete any applicable rows.

"Tom Ogilvy" wrote:

> Private Sub OKButton_Click()
> Dim i As Long, j As Long
> Dim ws As Worksheet
> Dim cells As Range
>
> j = 0
>
> For i = 0 To Me.Option2ListBox.ListCount - 1
> If Me.Option2ListBox.Selected(i) = True Then
> With worksheets("Schedules")
> Set lrow = .cells(Rows.Count,"H").End(xlUp).row
> set r = .Range("H1").Resize(lrow,1)
> End With
> if application.Countif(r,Me.Option2Listbox.List(i)) > 0 then
> For k = lrow to 1 step -1
> set rng = worksheets("Schedules").Cells(k,"H")
> If rng = Me.Option2ListBox.List(i) Then
> rng.EntireRow.Delete
> End If
> Next
> End if
> j = j + 1
> End If
> Next i
> Unload ImportingSchedules
> End Sub
>
> --
> Regards,
> Tom Ogilvy
>
> "Paige" wrote:
>
> > Option2ListBox populates with the items contained in Col H of the 'Schedules'
> > tab; the user can then select 1 or more of these items. For each selected
> > item, I want Excel to go to the 'Schedules' tab and delete any row that has
> > that item in Col H. Problem is that the code is not consistently deleting
> > all the corresponding rows for those items selected in the listbox. This
> > must be something simple I'm overlooking. Can anyone help???? Also, is
> > there a quicker way to delete these rows? Am a little concerned about speed,
> > since there's likely to be several thousand rows to look through.
> >
> > Private Sub OKButton_Click()
> > Dim i As Long, j As Long
> > Dim ws As Worksheet
> > Dim cells As Range
> >
> > j = 0
> >
> > For i = 0 To Me.Option2ListBox.ListCount - 1
> > If Me.Option2ListBox.Selected(i) = True Then
> > With worksheets("Schedules")
> > Set rngToSearch = .Range(.Range("H1"), .cells(Rows.Count,
> > "H").End(xlUp))
> > End With
> > For Each rng In rngToSearch
> > If rng = Me.Option2ListBox.List(i) Then
> > rng.EntireRow.Delete
> > End If
> > Next
> > j = j + 1
> > End If
> > Next i
> > Unload ImportingSchedules
> > End Sub
> >

 
Reply With Quote
 
Dave Peterson
Guest
Posts: n/a
 
      23rd Aug 2007
Try removing the word "Set" from this line:

Set lrow = .cells(Rows.Count,"H").End(xlUp).row
so it's just:
lrow = .cells(Rows.Count,"H").End(xlUp).row



Paige wrote:
>
> Thanks, Tom. However, am getting a 'Type Mismatch' error on the 'Set lrow
> =...' line of code. What would that be due to?
>
> "Tom Ogilvy" wrote:
>
> > Private Sub OKButton_Click()
> > Dim i As Long, j As Long
> > Dim ws As Worksheet
> > Dim cells As Range
> >
> > j = 0
> >
> > For i = 0 To Me.Option2ListBox.ListCount - 1
> > If Me.Option2ListBox.Selected(i) = True Then
> > With worksheets("Schedules")
> > Set lrow = .cells(Rows.Count,"H").End(xlUp).row
> > set r = .Range("H1").Resize(lrow,1)
> > End With
> > if application.Countif(r,Me.Option2Listbox.List(i)) > 0 then
> > For k = lrow to 1 step -1
> > set rng = worksheets("Schedules").Cells(k,"H")
> > If rng = Me.Option2ListBox.List(i) Then
> > rng.EntireRow.Delete
> > End If
> > Next
> > End if
> > j = j + 1
> > End If
> > Next i
> > Unload ImportingSchedules
> > End Sub
> >
> > --
> > Regards,
> > Tom Ogilvy
> >
> > "Paige" wrote:
> >
> > > Option2ListBox populates with the items contained in Col H of the 'Schedules'
> > > tab; the user can then select 1 or more of these items. For each selected
> > > item, I want Excel to go to the 'Schedules' tab and delete any row that has
> > > that item in Col H. Problem is that the code is not consistently deleting
> > > all the corresponding rows for those items selected in the listbox. This
> > > must be something simple I'm overlooking. Can anyone help???? Also, is
> > > there a quicker way to delete these rows? Am a little concerned about speed,
> > > since there's likely to be several thousand rows to look through.
> > >
> > > Private Sub OKButton_Click()
> > > Dim i As Long, j As Long
> > > Dim ws As Worksheet
> > > Dim cells As Range
> > >
> > > j = 0
> > >
> > > For i = 0 To Me.Option2ListBox.ListCount - 1
> > > If Me.Option2ListBox.Selected(i) = True Then
> > > With worksheets("Schedules")
> > > Set rngToSearch = .Range(.Range("H1"), .cells(Rows.Count,
> > > "H").End(xlUp))
> > > End With
> > > For Each rng In rngToSearch
> > > If rng = Me.Option2ListBox.List(i) Then
> > > rng.EntireRow.Delete
> > > End If
> > > Next
> > > j = j + 1
> > > End If
> > > Next i
> > > Unload ImportingSchedules
> > > End Sub
> > >


--

Dave Peterson
 
Reply With Quote
 
=?Utf-8?B?VG9tIE9naWx2eQ==?=
Guest
Posts: n/a
 
      23rd Aug 2007
as Dave said, remove the SET statement from that line. It is residue left
over from editing your code.

--
Regards,
Tom Ogilvy


"Paige" wrote:

> Did some digging re the type mismatch; was due to the fact that I hadn't
> referenced the correct VBA extensibility library. So now don't get the error
> message; however, it does not delete any applicable rows.
>
> "Tom Ogilvy" wrote:
>
> > Private Sub OKButton_Click()
> > Dim i As Long, j As Long
> > Dim ws As Worksheet
> > Dim cells As Range
> >
> > j = 0
> >
> > For i = 0 To Me.Option2ListBox.ListCount - 1
> > If Me.Option2ListBox.Selected(i) = True Then
> > With worksheets("Schedules")
> > Set lrow = .cells(Rows.Count,"H").End(xlUp).row
> > set r = .Range("H1").Resize(lrow,1)
> > End With
> > if application.Countif(r,Me.Option2Listbox.List(i)) > 0 then
> > For k = lrow to 1 step -1
> > set rng = worksheets("Schedules").Cells(k,"H")
> > If rng = Me.Option2ListBox.List(i) Then
> > rng.EntireRow.Delete
> > End If
> > Next
> > End if
> > j = j + 1
> > End If
> > Next i
> > Unload ImportingSchedules
> > End Sub
> >
> > --
> > Regards,
> > Tom Ogilvy
> >
> > "Paige" wrote:
> >
> > > Option2ListBox populates with the items contained in Col H of the 'Schedules'
> > > tab; the user can then select 1 or more of these items. For each selected
> > > item, I want Excel to go to the 'Schedules' tab and delete any row that has
> > > that item in Col H. Problem is that the code is not consistently deleting
> > > all the corresponding rows for those items selected in the listbox. This
> > > must be something simple I'm overlooking. Can anyone help???? Also, is
> > > there a quicker way to delete these rows? Am a little concerned about speed,
> > > since there's likely to be several thousand rows to look through.
> > >
> > > Private Sub OKButton_Click()
> > > Dim i As Long, j As Long
> > > Dim ws As Worksheet
> > > Dim cells As Range
> > >
> > > j = 0
> > >
> > > For i = 0 To Me.Option2ListBox.ListCount - 1
> > > If Me.Option2ListBox.Selected(i) = True Then
> > > With worksheets("Schedules")
> > > Set rngToSearch = .Range(.Range("H1"), .cells(Rows.Count,
> > > "H").End(xlUp))
> > > End With
> > > For Each rng In rngToSearch
> > > If rng = Me.Option2ListBox.List(i) Then
> > > rng.EntireRow.Delete
> > > End If
> > > Next
> > > j = j + 1
> > > End If
> > > Next i
> > > Unload ImportingSchedules
> > > End Sub
> > >

 
Reply With Quote
 
=?Utf-8?B?UGFpZ2U=?=
Guest
Posts: n/a
 
      23rd Aug 2007
Thanks, guys!!!! Works great now. Really really appreciate the help.

"Tom Ogilvy" wrote:

> as Dave said, remove the SET statement from that line. It is residue left
> over from editing your code.
>
> --
> Regards,
> Tom Ogilvy
>
>
> "Paige" wrote:
>
> > Did some digging re the type mismatch; was due to the fact that I hadn't
> > referenced the correct VBA extensibility library. So now don't get the error
> > message; however, it does not delete any applicable rows.
> >
> > "Tom Ogilvy" wrote:
> >
> > > Private Sub OKButton_Click()
> > > Dim i As Long, j As Long
> > > Dim ws As Worksheet
> > > Dim cells As Range
> > >
> > > j = 0
> > >
> > > For i = 0 To Me.Option2ListBox.ListCount - 1
> > > If Me.Option2ListBox.Selected(i) = True Then
> > > With worksheets("Schedules")
> > > Set lrow = .cells(Rows.Count,"H").End(xlUp).row
> > > set r = .Range("H1").Resize(lrow,1)
> > > End With
> > > if application.Countif(r,Me.Option2Listbox.List(i)) > 0 then
> > > For k = lrow to 1 step -1
> > > set rng = worksheets("Schedules").Cells(k,"H")
> > > If rng = Me.Option2ListBox.List(i) Then
> > > rng.EntireRow.Delete
> > > End If
> > > Next
> > > End if
> > > j = j + 1
> > > End If
> > > Next i
> > > Unload ImportingSchedules
> > > End Sub
> > >
> > > --
> > > Regards,
> > > Tom Ogilvy
> > >
> > > "Paige" wrote:
> > >
> > > > Option2ListBox populates with the items contained in Col H of the 'Schedules'
> > > > tab; the user can then select 1 or more of these items. For each selected
> > > > item, I want Excel to go to the 'Schedules' tab and delete any row that has
> > > > that item in Col H. Problem is that the code is not consistently deleting
> > > > all the corresponding rows for those items selected in the listbox. This
> > > > must be something simple I'm overlooking. Can anyone help???? Also, is
> > > > there a quicker way to delete these rows? Am a little concerned about speed,
> > > > since there's likely to be several thousand rows to look through.
> > > >
> > > > Private Sub OKButton_Click()
> > > > Dim i As Long, j As Long
> > > > Dim ws As Worksheet
> > > > Dim cells As Range
> > > >
> > > > j = 0
> > > >
> > > > For i = 0 To Me.Option2ListBox.ListCount - 1
> > > > If Me.Option2ListBox.Selected(i) = True Then
> > > > With worksheets("Schedules")
> > > > Set rngToSearch = .Range(.Range("H1"), .cells(Rows.Count,
> > > > "H").End(xlUp))
> > > > End With
> > > > For Each rng In rngToSearch
> > > > If rng = Me.Option2ListBox.List(i) Then
> > > > rng.EntireRow.Delete
> > > > End If
> > > > Next
> > > > j = j + 1
> > > > End If
> > > > Next i
> > > > Unload ImportingSchedules
> > > > End Sub
> > > >

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Go to sheet based on ListBox selection jeff Microsoft Excel Programming 5 14th Aug 2009 06:58 PM
Change lists of one listbox based on selection of other listbox? NeedHelp Microsoft Access Form Coding 2 1st May 2008 07:48 PM
Change lists of one listbox based on selection of other listbox? NeedHelp Microsoft Access Forms 3 1st May 2008 05:58 PM
Delete Row Based on Listbox Selection Hartman Microsoft Excel Programming 1 26th Apr 2005 06:51 AM
table insertion based og listbox selection spstieng Microsoft Access Reports 0 29th Oct 2003 01:57 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:27 PM.