Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
RichT
 
Posts: n/a
Default vb to clear cell contents


Hi,
Looking for some very basic VB assistance.

I use a range of cells to hold some temporary data, after which I clear
the range with a macro.
The range always begins at "B4"and has a fixed number of columns, but
the number of rows could be between 1 and 15.
I currently do this (messily) as follows:

Sub ClearCells1()
Range("B4").Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.ClearContents
Range("B4").Select
End Sub

which I have now discovered doesn't work if there's only 1 row of
data,
and is lethal if there's no data in the range.
Any assistance would be greatly appreciated.


--
RichT
------------------------------------------------------------------------
RichT's Profile: http://www.excelforum.com/member.php...o&userid=23678
View this thread: http://www.excelforum.com/showthread...hreadid=380548

  #2   Report Post  
JE McGimpsey
 
Posts: n/a
Default

One way:

Public Sub ClearCells1()
Dim nRows As Long
Dim nCols As Long
With Range("B4")
If IsEmpty(.Value) Then Exit Sub
If IsEmpty(.Offset(1, 0).Value) Then
nRows = 1
Else
nRows = Application.Min(18, .End(xlDown).Row) - 3
End If
nCols = .End(xlToRight).Column - 1
.Resize(nRows, nCols).ClearContents
End With
End Sub



In article ,
RichT wrote:

Hi,
Looking for some very basic VB assistance.

I use a range of cells to hold some temporary data, after which I clear
the range with a macro.
The range always begins at "B4"and has a fixed number of columns, but
the number of rows could be between 1 and 15.
I currently do this (messily) as follows:

Sub ClearCells1()
Range("B4").Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.ClearContents
Range("B4").Select
End Sub

which I have now discovered doesn't work if there's only 1 row of
data,
and is lethal if there's no data in the range.
Any assistance would be greatly appreciated.

  #3   Report Post  
PeterAtherton
 
Posts: n/a
Default

Sub Clear()
Dim r as long, nr as long
Dim j as integer, ncol as integer.
nr = Range("B2").currentregion.rows.count
ncol = Range("B2").currentregion.rows.count
Range(cells(2,2),cells(nr,ncol).clearcontents
End sub

Peter Atherton

"RichT" wrote:


Hi,
Looking for some very basic VB assistance.

I use a range of cells to hold some temporary data, after which I clear
theas range with a macro.
The range always begins at "B4"and has a fixed number of columns, but
the number of rows could be between 1 and 15.
I currently do this (messily) as follows:

Sub ClearCells1()
Range("B4").Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.ClearContents
Range("B4").Select
End Sub

which I have now discovered doesn't work if there's only 1 row of
data,
and is lethal if there's no data in the range.
Any assistance would be greatly appreciated.


--
RichT
------------------------------------------------------------------------
RichT's Profile: http://www.excelforum.com/member.php...o&userid=23678
View this thread: http://www.excelforum.com/showthread...hreadid=380548


  #4   Report Post  
RichT
 
Posts: n/a
Default


Many thanks for this.
It works well.
I don't really get what the following line does:-

nRows = Application.Min(18, .End(xlDown).Row) - 3

but no worries, I'm just happy it works.
Thanks again,
Richard.

JE McGimpsey Wrote:
One way:

Public Sub ClearCells1()
Dim nRows As Long
Dim nCols As Long
With Range("B4")
If IsEmpty(.Value) Then Exit Sub
If IsEmpty(.Offset(1, 0).Value) Then
nRows = 1
Else
nRows = Application.Min(18, .End(xlDown).Row) - 3
End If
nCols = .End(xlToRight).Column - 1
.Resize(nRows, nCols).ClearContents
End With
End Sub



In article ,
RichT wrote:

Hi,
Looking for some very basic VB assistance.

I use a range of cells to hold some temporary data, after which I

clear
the range with a macro.
The range always begins at "B4"and has a fixed number of columns,

but
the number of rows could be between 1 and 15.
I currently do this (messily) as follows:

Sub ClearCells1()
Range("B4").Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.ClearContents
Range("B4").Select
End Sub

which I have now discovered doesn't work if there's only 1 row of
data,
and is lethal if there's no data in the range.
Any assistance would be greatly appreciated.



--
RichT
------------------------------------------------------------------------
RichT's Profile: http://www.excelforum.com/member.php...o&userid=23678
View this thread: http://www.excelforum.com/showthread...hreadid=380548

  #5   Report Post  
RichT
 
Posts: n/a
Default


Actually, I've just discovered a problem with the solution provided. due
to my not supplying sufficient info.
My data to be deleted resides in say B4:X10, (always these columns,
but last row varies between 6 and 16).
I also have some formulae in Z4:Z10 that I don't want deleted.
The solution from JE McGimpsey unfortunately deletes these formulae.


RichT Wrote:
Many thanks for this.
It works well.
I don't really get what the following line does:-

nRows = Application.Min(18, .End(xlDown).Row) - 3

but no worries, I'm just happy it works.
Thanks again,
Richard.



--
RichT
------------------------------------------------------------------------
RichT's Profile: http://www.excelforum.com/member.php...o&userid=23678
View this thread: http://www.excelforum.com/showthread...hreadid=380548



  #6   Report Post  
JE McGimpsey
 
Posts: n/a
Default

One way:

Change

.Resize(nRows, nCols).ClearContents

to

.Resize(nRows, 23).ClearContents

and delete these lines:

Dim nCols As Long

nCols = .End(xlToRight).Column - 1


In article ,
RichT wrote:

Actually, I've just discovered a problem with the solution provided. due
to my not supplying sufficient info.
My data to be deleted resides in say B4:X10, (always these columns,
but last row varies between 6 and 16).
I also have some formulae in Z4:Z10 that I don't want deleted.
The solution from JE McGimpsey unfortunately deletes these formulae.


RichT Wrote:
Many thanks for this.
It works well.
I don't really get what the following line does:-

nRows = Application.Min(18, .End(xlDown).Row) - 3

but no worries, I'm just happy it works.
Thanks again,
Richard.

  #7   Report Post  
RichT
 
Posts: n/a
Default


Many thanks for your help once more, JE.
It's much appreciated.


--
RichT
------------------------------------------------------------------------
RichT's Profile: http://www.excelforum.com/member.php...o&userid=23678
View this thread: http://www.excelforum.com/showthread...hreadid=380548

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
Function syntax to compare cell contents ES Excel Worksheet Functions 2 May 18th 05 03:53 PM
I can not see all contents in one cell. Help me pls! :-) Excel Discussion (Misc queries) 3 April 28th 05 01:15 PM
How can I edit cell contents with a macro in Excel? Mind the gaps! Excel Discussion (Misc queries) 2 March 23rd 05 08:51 PM
Insert new row as cell contents change George Excel Discussion (Misc queries) 2 January 26th 05 11:47 AM
Modify Row & Cell Contents based upon Cells Values bpat1434 Excel Worksheet Functions 0 November 7th 04 03:31 PM


All times are GMT +1. The time now is 02:27 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"