Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 156
Default Clearing a Sheet


Hi,

I have a bit of code e.g.

Dim wk As Workbook
Set wk = ActiveWorkbook
Dim sht As Worksheet
Set sht = wk.Worksheets("Reports")

With sht
..Cells.ClearContents
End With

This clears the sheet, as required, BUT if I add this next:

With Worksheets("Reports")
Set rng =
Worksheets("Reports").Range("A1").SpecialCells(xlC ellTypeLastCell)
LastRow = rng.Row
End With

LastRow still contains the number of lines in the just cleared sheet.

Can I do something to make LastRow variable correct?

Thanks - Kirk

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,624
Default Clearing a Sheet

UsedRange will continue to include formatted cells. If you don't want to
preserve formatting, then use .Clear rather than .ClearContents. Some
versions of XL require you to use .UsedRange to reset the used range.

Dim rng As Range
Dim lastRow As Long
With Worksheets("Reports")
.Cells.Clear
.UsedRange
Set rng = .Cells.SpecialCells(xlCellTypeLastCell)
lastRow = rng.Row
End With
MsgBox rng.Address & vbNewLine & lastRow

In article ,
kirkm wrote:

Hi,

I have a bit of code e.g.

Dim wk As Workbook
Set wk = ActiveWorkbook
Dim sht As Worksheet
Set sht = wk.Worksheets("Reports")

With sht
.Cells.ClearContents
End With

This clears the sheet, as required, BUT if I add this next:

With Worksheets("Reports")
Set rng =
Worksheets("Reports").Range("A1").SpecialCells(xlC ellTypeLastCell)
LastRow = rng.Row
End With

LastRow still contains the number of lines in the just cleared sheet.

Can I do something to make LastRow variable correct?

Thanks - Kirk

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 156
Default Clearing a Sheet

On Mon, 16 Apr 2007 22:03:35 -0600, JE McGimpsey
wrote:

UsedRange will continue to include formatted cells. If you don't want to
preserve formatting, then use .Clear rather than .ClearContents. Some
versions of XL require you to use .UsedRange to reset the used range.

Dim rng As Range
Dim lastRow As Long
With Worksheets("Reports")
.Cells.Clear
.UsedRange
Set rng = .Cells.SpecialCells(xlCellTypeLastCell)
lastRow = rng.Row
End With
MsgBox rng.Address & vbNewLine & lastRow


Thank you. That did work but (as you say) it
loses formatting. I'd like to keep that, if possible.

Should I use a different command, or re-apply formatting?

Thanks - Kirk

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 363
Default Clearing a Sheet

You can use the :
..Cells.value =""
to keep the existing formatting

"kirkm" wrote in message
...
On Mon, 16 Apr 2007 22:03:35 -0600, JE McGimpsey
wrote:

UsedRange will continue to include formatted cells. If you don't want to
preserve formatting, then use .Clear rather than .ClearContents. Some
versions of XL require you to use .UsedRange to reset the used range.

Dim rng As Range
Dim lastRow As Long
With Worksheets("Reports")
.Cells.Clear
.UsedRange
Set rng = .Cells.SpecialCells(xlCellTypeLastCell)
lastRow = rng.Row
End With
MsgBox rng.Address & vbNewLine & lastRow


Thank you. That did work but (as you say) it
loses formatting. I'd like to keep that, if possible.

Should I use a different command, or re-apply formatting?

Thanks - Kirk


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,624
Default Clearing a Sheet

If you keep the formatting by using .ClearContents, then the UsedRange
will reflect the formatted cells.

But if you're using .Cells.ClearContents, then rng can be set to cell A1
and lastRow will always = 1, right?



In article ,
kirkm wrote:

Thank you. That did work but (as you say) it
loses formatting. I'd like to keep that, if possible.

Should I use a different command, or re-apply formatting?



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 156
Default Clearing a Sheet

On Tue, 17 Apr 2007 15:08:57 +1000, "Corey"
wrote:

You can use the :
.Cells.value =""
to keep the existing formatting


That worked, but also seems to keep the previous count
(which I want to lose).


  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 156
Default Clearing a Sheet

On Mon, 16 Apr 2007 23:25:41 -0600, JE McGimpsey
wrote:

If you keep the formatting by using .ClearContents, then the UsedRange
will reflect the formatted cells.

But if you're using .Cells.ClearContents, then rng can be set to cell A1
and lastRow will always = 1, right?


Gosh, now i'm losing the plot :)

Yes, everytime the Sheet is cleared it should be 1. (Not zero?).

So, if I set the range at that point, as you suggest, my lastRow
variable will be 1 ?

I'm fairly new to all this... will have a play.

Thanks - Kirk


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
clearing data from excell sheet A Excel Discussion (Misc queries) 3 January 28th 08 04:09 PM
Clearing data from a so called protected sheet??? funkymonkUK[_162_] Excel Programming 5 May 17th 06 05:16 PM
my macro is clearing the wrong sheet Polarbear Excel Programming 6 August 2nd 05 08:49 PM
Clearing old data from a sheet 5lmustang[_2_] Excel Programming 0 October 26th 04 12:18 AM
Clearing old data from a sheet 5lmustang Excel Programming 1 October 20th 04 02:17 AM


All times are GMT +1. The time now is 08:57 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"