PC Review


Reply
Thread Tools Rate Thread

Clear Range problem

 
 
Jac Tremblay
Guest
Posts: n/a
 
      13th Jun 2009
Hi,
I have this simple code but somehow, it doesn't work properly. There must be
some simple solution but I cannot figure it out.
I want to determine a specific range on a particular sheet and clear its
contents before inserting something else. I have created a test workbook to
illustrate the problem. Here is the code:
' *********************************************************
Sub UnionTest()
Dim wbk As Workbook
Dim sht As Worksheet
Dim rngStart As Range
Dim rngTemp As Excel.Range
Dim intI As Integer
Set wbk = ActiveWorkbook
For intI = 1 To 3
Set sht = wbk.Sheets(intI)
Set rngStart = sht.Range("B2")
Set rngTemp = Union( _
Range(rngStart.Offset(3, -1).Address & _
":" & rngStart.Offset(6, 1).Address), _
Range(rngStart.Offset(10, -1).Address & _
":" & rngStart.Offset(15, 1).Address))
MsgBox "sht.Name = " & sht.Name & vbCrLf & _
"rngTemp.Address = " & rngTemp.Address
sht.Activate
rngTemp.Select
rngTemp.ClearContents
Next intI
Set rngTemp = Nothing
Set rngStart = Nothing
Set sht = Nothing
Set wbk = Nothing
End Sub
' *********************************************************
Instructions for debug only:
sht.Activate
rngTemp.Select
I hope someone can pinpoint the problem easily.
Thanks.
--
Jac Tremblay
 
Reply With Quote
 
 
 
 
Jacob Skaria
Guest
Posts: n/a
 
      13th Jun 2009
Jac

Try the below. The sheet was not refereed to for the below line .
Set rngTemp = Union( sht.Range(rngStart.Offset(3, -1).Address & _)


Sub UnionTest()
Dim wbk As Workbook
Dim sht As Worksheet
Dim rngStart As Range
Dim rngTemp As Excel.Range
Dim intI As Integer
Set wbk = ActiveWorkbook
For intI = 1 To 3
Set sht = wbk.Sheets(intI)
Set rngStart = sht.Range("B2")
Set rngTemp = Union( _
sht.Range(rngStart.Offset(3, -1).Address & _
":" & rngStart.Offset(6, 1).Address), _
sht.Range(rngStart.Offset(10, -1).Address & _
":" & rngStart.Offset(15, 1).Address))
MsgBox "sht.Name = " & sht.Name & vbCrLf & _
"rngTemp.Address = " & rngTemp.Address
sht.Activate
rngTemp.Select
rngTemp.ClearContents
Next intI
Set rngTemp = Nothing
Set rngStart = Nothing
Set sht = Nothing
Set wbk = Nothing
End Sub


--
If this post helps click Yes
---------------
Jacob Skaria


"Jac Tremblay" wrote:

> Hi,
> I have this simple code but somehow, it doesn't work properly. There must be
> some simple solution but I cannot figure it out.
> I want to determine a specific range on a particular sheet and clear its
> contents before inserting something else. I have created a test workbook to
> illustrate the problem. Here is the code:
> ' *********************************************************
> Sub UnionTest()
> Dim wbk As Workbook
> Dim sht As Worksheet
> Dim rngStart As Range
> Dim rngTemp As Excel.Range
> Dim intI As Integer
> Set wbk = ActiveWorkbook
> For intI = 1 To 3
> Set sht = wbk.Sheets(intI)
> Set rngStart = sht.Range("B2")
> Set rngTemp = Union( _
> Range(rngStart.Offset(3, -1).Address & _
> ":" & rngStart.Offset(6, 1).Address), _
> Range(rngStart.Offset(10, -1).Address & _
> ":" & rngStart.Offset(15, 1).Address))
> MsgBox "sht.Name = " & sht.Name & vbCrLf & _
> "rngTemp.Address = " & rngTemp.Address
> sht.Activate
> rngTemp.Select
> rngTemp.ClearContents
> Next intI
> Set rngTemp = Nothing
> Set rngStart = Nothing
> Set sht = Nothing
> Set wbk = Nothing
> End Sub
> ' *********************************************************
> Instructions for debug only:
> sht.Activate
> rngTemp.Select
> I hope someone can pinpoint the problem easily.
> Thanks.
> --
> Jac Tremblay

 
Reply With Quote
 
Jac Tremblay
Guest
Posts: n/a
 
      15th Jun 2009
Hi Jacob,
This solves my problem all right. Thanks you for your precious comment. It
is very appreciated.
Have a good day.
--
Jac Tremblay


"Jacob Skaria" wrote:

> Jac
>
> Try the below. The sheet was not refereed to for the below line .
> Set rngTemp = Union( sht.Range(rngStart.Offset(3, -1).Address & _)
>
>
> Sub UnionTest()
> Dim wbk As Workbook
> Dim sht As Worksheet
> Dim rngStart As Range
> Dim rngTemp As Excel.Range
> Dim intI As Integer
> Set wbk = ActiveWorkbook
> For intI = 1 To 3
> Set sht = wbk.Sheets(intI)
> Set rngStart = sht.Range("B2")
> Set rngTemp = Union( _
> sht.Range(rngStart.Offset(3, -1).Address & _
> ":" & rngStart.Offset(6, 1).Address), _
> sht.Range(rngStart.Offset(10, -1).Address & _
> ":" & rngStart.Offset(15, 1).Address))
> MsgBox "sht.Name = " & sht.Name & vbCrLf & _
> "rngTemp.Address = " & rngTemp.Address
> sht.Activate
> rngTemp.Select
> rngTemp.ClearContents
> Next intI
> Set rngTemp = Nothing
> Set rngStart = Nothing
> Set sht = Nothing
> Set wbk = Nothing
> End Sub
>
>
> --
> If this post helps click Yes
> ---------------
> Jacob Skaria
>
>
> "Jac Tremblay" wrote:
>
> > Hi,
> > I have this simple code but somehow, it doesn't work properly. There must be
> > some simple solution but I cannot figure it out.
> > I want to determine a specific range on a particular sheet and clear its
> > contents before inserting something else. I have created a test workbook to
> > illustrate the problem. Here is the code:
> > ' *********************************************************
> > Sub UnionTest()
> > Dim wbk As Workbook
> > Dim sht As Worksheet
> > Dim rngStart As Range
> > Dim rngTemp As Excel.Range
> > Dim intI As Integer
> > Set wbk = ActiveWorkbook
> > For intI = 1 To 3
> > Set sht = wbk.Sheets(intI)
> > Set rngStart = sht.Range("B2")
> > Set rngTemp = Union( _
> > Range(rngStart.Offset(3, -1).Address & _
> > ":" & rngStart.Offset(6, 1).Address), _
> > Range(rngStart.Offset(10, -1).Address & _
> > ":" & rngStart.Offset(15, 1).Address))
> > MsgBox "sht.Name = " & sht.Name & vbCrLf & _
> > "rngTemp.Address = " & rngTemp.Address
> > sht.Activate
> > rngTemp.Select
> > rngTemp.ClearContents
> > Next intI
> > Set rngTemp = Nothing
> > Set rngStart = Nothing
> > Set sht = Nothing
> > Set wbk = Nothing
> > End Sub
> > ' *********************************************************
> > Instructions for debug only:
> > sht.Activate
> > rngTemp.Select
> > I hope someone can pinpoint the problem easily.
> > Thanks.
> > --
> > Jac Tremblay

 
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
Clear a range ordnance1 Microsoft Excel Programming 2 9th Nov 2009 03:11 AM
Clear a range ordnance1 Microsoft Excel Programming 1 8th Nov 2009 11:36 PM
Clear a range of data Patrick C. Simonds Microsoft Excel Programming 4 16th Mar 2009 07:50 PM
Clear Range scott Microsoft Excel Programming 3 20th Dec 2007 02:46 AM
Clear a range of cells Joe@excel Microsoft Excel Discussion 6 25th Nov 2005 06:28 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:36 PM.