PC Review


Reply
Thread Tools Rate Thread

Accessing ranges with integer variables

 
 
Andrew
Guest
Posts: n/a
 
      17th Dec 2007
Hello,
I need a snippet of code to implement the code show here, but
using variables as the range argument.

Worksheets("graphs").Range("A1:B200").ClearContents

What I would like to do is replace "A1:B200" with an integer value (to
be entered by user) of 200. Is there a way I can do this using
Cells(x,y)? Can the Cells command be used to access a range, as
opposed to a single cell?

thanks
Andy
 
Reply With Quote
 
 
 
 
sebastienm
Guest
Posts: n/a
 
      17th Dec 2007
Hi,
try something like:
''' --------------------------------------------------
Sub test()
Dim i As Long, maxrow As Long
Dim strResult As String

maxrow = ActiveSheet.Rows.Count

''' ask the user
strResult = VBA.InputBox("Enter row (number greater than 0):", "Clear
data")

If strResult = "" Then '''user cancelled
Exit Sub
ElseIf Not IsNumeric(strResult) Then ''' user didn't enter a number
Else
i = Val(strResult) ''' convert to number
If i < 1 Or i > maxrow Then
MsgBox "The number must between 1 and " & maxrow
Exit Sub
Else
Worksheets("graphs").Range("A1:B" & i).ClearContents
End If
End If

End Sub
''' ------------------------------------------
--
Regards,
Sébastien
<http://www.ondemandanalysis.com>


"Andrew" wrote:

> Hello,
> I need a snippet of code to implement the code show here, but
> using variables as the range argument.
>
> Worksheets("graphs").Range("A1:B200").ClearContents
>
> What I would like to do is replace "A1:B200" with an integer value (to
> be entered by user) of 200. Is there a way I can do this using
> Cells(x,y)? Can the Cells command be used to access a range, as
> opposed to a single cell?
>
> thanks
> Andy
>

 
Reply With Quote
 
Jerry W. Lewis
Guest
Posts: n/a
 
      17th Dec 2007
Worksheets("graphs").Range(Cells(1,1),Cells(200,1)).ClearContents

Jerry

"Andrew" wrote:

> Hello,
> I need a snippet of code to implement the code show here, but
> using variables as the range argument.
>
> Worksheets("graphs").Range("A1:B200").ClearContents
>
> What I would like to do is replace "A1:B200" with an integer value (to
> be entered by user) of 200. Is there a way I can do this using
> Cells(x,y)? Can the Cells command be used to access a range, as
> opposed to a single cell?
>
> thanks
> Andy
>

 
Reply With Quote
 
Alan Beban
Guest
Posts: n/a
 
      17th Dec 2007
Andrew wrote:
> Hello,
> I need a snippet of code to implement the code show here, but
> using variables as the range argument.
>
> Worksheets("graphs").Range("A1:B200").ClearContents
>
> What I would like to do is replace "A1:B200" with an integer value (to
> be entered by user) of 200. Is there a way I can do this using
> Cells(x,y)? Can the Cells command be used to access a range, as
> opposed to a single cell?
>
> thanks
> Andy


x = 10
Debug.Print Range("A6:B" & x).Address
 
Reply With Quote
 
Dave Peterson
Guest
Posts: n/a
 
      17th Dec 2007
Just a clarification on Jerry's post.

If Graphs isn't the activesheet, then those unqualified ranges (cells()) will
cause trouble with the code.

I'd use:

with worksheets("Graphs")
.range(.cells(1,1), .cells(200,1)).clearcontents
end with

Those dots in front of the .range & .cells mean that they belong to the object
referred to in the previous "with" statement--in this case the Graphs worksheet.

Jerry W. Lewis wrote:
>
> Worksheets("graphs").Range(Cells(1,1),Cells(200,1)).ClearContents
>
> Jerry
>
> "Andrew" wrote:
>
> > Hello,
> > I need a snippet of code to implement the code show here, but
> > using variables as the range argument.
> >
> > Worksheets("graphs").Range("A1:B200").ClearContents
> >
> > What I would like to do is replace "A1:B200" with an integer value (to
> > be entered by user) of 200. Is there a way I can do this using
> > Cells(x,y)? Can the Cells command be used to access a range, as
> > opposed to a single cell?
> >
> > thanks
> > Andy
> >


--

Dave Peterson
 
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
Divide the values of 2 integer variables? LongBeachGuy Microsoft Excel Programming 2 19th Oct 2007 02:09 AM
Re: Set Integer Variables back to Zero NickHK Microsoft Excel Programming 0 7th Dec 2006 01:45 AM
Concatenate two variables (String & Integer) =?Utf-8?B?U3JlZW5pdmFzIFZhcmFkaGFu?= Microsoft Excel Programming 3 12th Dec 2005 01:28 PM
Accessing class member variables - properties or variables? dwok Microsoft VB .NET 8 4th Mar 2005 03:54 AM
calculations with integer variables? BigMan Microsoft Windows 2000 CMD Promt 8 22nd Jan 2005 05:41 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 08:09 PM.