PC Review


Reply
Thread Tools Rate Thread

Delete last row from range

 
 
condotta@shaw.ca
Guest
Posts: n/a
 
      8th Sep 2007
Hi,

I have code as follows,

Dim DataRow As Long
Application.ScreenUpdating = False
DataRow = ProjectsListBox.ListIndex + 1
With ThisWorkbook.Worksheets("Data")
Rng.Rows(DataRow).Delete xlShiftUp
' If listbox entries have all been deleted, reset the 'Rng'
object _
' so that a valid rowsource can be updated.
If Rng Is Nothing Then
Set Rng = .Range("S2").Resize(, 2)
Else
Set Rng = .Range("S2").Resize(Rng.Rows.Count, 2)
End If
ProjectsListBox.RowSource = Rng.Address(External:=True)
ProjectsListBox.ListIndex = -1
End With

My problem is with my set 'Rng'. If the last row of the range object
'Rng'
is deleted, "Rng Is Nothing" always returns false and then errors. Rng
is
initially set to a worksheet range and works well until the last entry
is deleted.
Any suggestions or alternatives?

Regards,

Stefano

 
Reply With Quote
 
 
 
 
=?Utf-8?B?QmFyYiBSZWluaGFyZHQ=?=
Guest
Posts: n/a
 
      8th Sep 2007
Here's some test code you could try

Dim Rng As Range
Dim myRange As Range
Set Rng = Range("A2:Z200")
Debug.Print Rng.Row, Rng.Rows.Count
Set myRange = Cells(Rng.Row, Rng.Column).Offset(Rng.Rows.Count - Rng.Row +
1, 0)
myRange.EntireRow.Delete

--
HTH,
Barb Reinhardt



"(E-Mail Removed)" wrote:

> Hi,
>
> I have code as follows,
>
> Dim DataRow As Long
> Application.ScreenUpdating = False
> DataRow = ProjectsListBox.ListIndex + 1
> With ThisWorkbook.Worksheets("Data")
> Rng.Rows(DataRow).Delete xlShiftUp
> ' If listbox entries have all been deleted, reset the 'Rng'
> object _
> ' so that a valid rowsource can be updated.
> If Rng Is Nothing Then
> Set Rng = .Range("S2").Resize(, 2)
> Else
> Set Rng = .Range("S2").Resize(Rng.Rows.Count, 2)
> End If
> ProjectsListBox.RowSource = Rng.Address(External:=True)
> ProjectsListBox.ListIndex = -1
> End With
>
> My problem is with my set 'Rng'. If the last row of the range object
> 'Rng'
> is deleted, "Rng Is Nothing" always returns false and then errors. Rng
> is
> initially set to a worksheet range and works well until the last entry
> is deleted.
> Any suggestions or alternatives?
>
> Regards,
>
> Stefano
>
>

 
Reply With Quote
 
condotta@shaw.ca
Guest
Posts: n/a
 
      8th Sep 2007
On Sep 7, 7:56 pm, Barb Reinhardt
<BarbReinha...@discussions.microsoft.com> wrote:
> Here's some test code you could try
>
> Dim Rng As Range
> Dim myRange As Range
> Set Rng = Range("A2:Z200")
> Debug.Print Rng.Row, Rng.Rows.Count
> Set myRange = Cells(Rng.Row, Rng.Column).Offset(Rng.Rows.Count - Rng.Row +
> 1, 0)
> myRange.EntireRow.Delete
>
> --
> HTH,
> Barb Reinhardt
>
>
>
> "condo...@shaw.ca" wrote:
> > Hi,

>
> > I have code as follows,

>
> > Dim DataRow As Long
> > Application.ScreenUpdating = False
> > DataRow = ProjectsListBox.ListIndex + 1
> > With ThisWorkbook.Worksheets("Data")
> > Rng.Rows(DataRow).Delete xlShiftUp
> > ' If listbox entries have all been deleted, reset the 'Rng'
> > object _
> > ' so that a valid rowsource can be updated.
> > If Rng Is Nothing Then
> > Set Rng = .Range("S2").Resize(, 2)
> > Else
> > Set Rng = .Range("S2").Resize(Rng.Rows.Count, 2)
> > End If
> > ProjectsListBox.RowSource = Rng.Address(External:=True)
> > ProjectsListBox.ListIndex = -1
> > End With

>
> > My problem is with my set 'Rng'. If the last row of the range object
> > 'Rng'
> > is deleted, "Rng Is Nothing" always returns false and then errors. Rng
> > is
> > initially set to a worksheet range and works well until the last entry
> > is deleted.
> > Any suggestions or alternatives?

>
> > Regards,

>
> > Stefano- Hide quoted text -

>


Thanks for the test code Barb, however, my test at "If Rng Is Nothing
Then" still
puzzles me. It always returns false possibly letting me know that I am
using a poor
construct. I guess I could use "If Rng.Rows.Count = 1 Then" .....

Regards,

Stefano

 
Reply With Quote
 
=?Utf-8?B?QmFyYiBSZWluaGFyZHQ=?=
Guest
Posts: n/a
 
      10th Sep 2007
I believe that if these is nothing in the range, the rows.count part won't
work. Think about it.
--
HTH,
Barb Reinhardt



"(E-Mail Removed)" wrote:

> On Sep 7, 7:56 pm, Barb Reinhardt
> <BarbReinha...@discussions.microsoft.com> wrote:
> > Here's some test code you could try
> >
> > Dim Rng As Range
> > Dim myRange As Range
> > Set Rng = Range("A2:Z200")
> > Debug.Print Rng.Row, Rng.Rows.Count
> > Set myRange = Cells(Rng.Row, Rng.Column).Offset(Rng.Rows.Count - Rng.Row +
> > 1, 0)
> > myRange.EntireRow.Delete
> >
> > --
> > HTH,
> > Barb Reinhardt
> >
> >
> >
> > "condo...@shaw.ca" wrote:
> > > Hi,

> >
> > > I have code as follows,

> >
> > > Dim DataRow As Long
> > > Application.ScreenUpdating = False
> > > DataRow = ProjectsListBox.ListIndex + 1
> > > With ThisWorkbook.Worksheets("Data")
> > > Rng.Rows(DataRow).Delete xlShiftUp
> > > ' If listbox entries have all been deleted, reset the 'Rng'
> > > object _
> > > ' so that a valid rowsource can be updated.
> > > If Rng Is Nothing Then
> > > Set Rng = .Range("S2").Resize(, 2)
> > > Else
> > > Set Rng = .Range("S2").Resize(Rng.Rows.Count, 2)
> > > End If
> > > ProjectsListBox.RowSource = Rng.Address(External:=True)
> > > ProjectsListBox.ListIndex = -1
> > > End With

> >
> > > My problem is with my set 'Rng'. If the last row of the range object
> > > 'Rng'
> > > is deleted, "Rng Is Nothing" always returns false and then errors. Rng
> > > is
> > > initially set to a worksheet range and works well until the last entry
> > > is deleted.
> > > Any suggestions or alternatives?

> >
> > > Regards,

> >
> > > Stefano- Hide quoted text -

> >

>
> Thanks for the test code Barb, however, my test at "If Rng Is Nothing
> Then" still
> puzzles me. It always returns false possibly letting me know that I am
> using a poor
> construct. I guess I could use "If Rng.Rows.Count = 1 Then" .....
>
> Regards,
>
> Stefano
>
>

 
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
Test variable range for 'Delete'; then delete the row ryguy7272 Microsoft Excel Programming 2 18th May 2010 04:41 PM
How to Delete blanks between a range and populate only the names inthe given range Yuvraj Microsoft Excel Misc 2 4th Nov 2009 08:32 PM
delete a range The Weather Girl Microsoft Excel Worksheet Functions 4 13th Jul 2009 03:51 PM
How to Delete a Range in Closed Workbook (to Replace Delete Query) resant_v@yahoo.com Microsoft Excel Misc 1 8th Mar 2006 10:10 AM
Range.Delete and Range.Resize.Name performance issues =?Utf-8?B?VGVzdC5GaWxl?= Microsoft Excel Programming 0 15th Feb 2005 03:33 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:42 PM.