PC Review


Reply
Thread Tools Rate Thread

Can I set a variable to be equal to the range of an entire column?

 
 
=?Utf-8?B?cm9iczMxMzE=?=
Guest
Posts: n/a
 
      29th May 2007
Hi,

I am trying to set a variable named idrange to column D in a sheet - the
values in this column will vary (ie - values may go through row 54 but then
may be updated to have values through row 67, etc). I need to capture all
cells in column D that have values as each cell in column D that does have a
value will be evaluated in a subsequent equation in the code. I have not
been able to figure out how to set my variable to equal the range of column
D...below is that code I have - I get a "Runtime error '1004';" error with
this code - is it possible to do what I'm trying to do?

Sub purgepayment()

Dim payment
Dim opentranpay
Dim rownumpay As Integer
Dim idrange As Range
Dim payrange As Range
Dim y As Integer

Sheets("Member ID Report Master").Activate
Set idrange = Range(Columns("D"))




--
Robert
 
Reply With Quote
 
 
 
 
=?Utf-8?B?QmVuIE1jQmVu?=
Guest
Posts: n/a
 
      29th May 2007
Simply

foobar = Columns(4)


Should do what you want. However, you probably want to drop the "activate"
part, as it is unecesarry if you use:

Sheets("Member ID Report Master").columns(4)





"robs3131" wrote:

> Hi,
>
> I am trying to set a variable named idrange to column D in a sheet - the
> values in this column will vary (ie - values may go through row 54 but then
> may be updated to have values through row 67, etc). I need to capture all
> cells in column D that have values as each cell in column D that does have a
> value will be evaluated in a subsequent equation in the code. I have not
> been able to figure out how to set my variable to equal the range of column
> D...below is that code I have - I get a "Runtime error '1004';" error with
> this code - is it possible to do what I'm trying to do?
>
> Sub purgepayment()
>
> Dim payment
> Dim opentranpay
> Dim rownumpay As Integer
> Dim idrange As Range
> Dim payrange As Range
> Dim y As Integer
>
> Sheets("Member ID Report Master").Activate
> Set idrange = Range(Columns("D"))
>
>
>
>
> --
> Robert

 
Reply With Quote
 
=?Utf-8?B?SmF5?=
Guest
Posts: n/a
 
      29th May 2007
Hi Robert -

Change
Set idrange = Range(Columns("D"))

To
Set idrange = Columns("D")

---
It's redundant (and not permitted) to use an explicitly defined range as the
sole argument of the Range property. You've correctly called for column D
with the Columns property, but the Columns property (in Columns("D"))
already returns the range you want. Therefore, your extra use of 'Range'
emulates the redundant statement:

Range(Range("D165536")), which is not permitted.

Range("D165536") or Columns("D") is all that is needed. You can also
simplify to Columns("D").

--
Jay

 
Reply With Quote
 
=?Utf-8?B?bWExYWNoYWk=?=
Guest
Posts: n/a
 
      29th May 2007
To add to that, if you are only looking for cells that have a value, you can
try using the SpecialCells property.

Try this:
Set idrange = Columns("D").SpecialCells(xlCellTypeConstants, xlNumbers)

Or if you are looking for cells with other characteristics there are other
constants to use with SpecialCells... take a look in the help on it.

-jputman
 
Reply With Quote
 
=?Utf-8?B?cm9iczMxMzE=?=
Guest
Posts: n/a
 
      5th Jun 2007
Hi Ben,

This is actually a good point that you have brought up. From prior posts of
mine, it has sounded to me like "columns" and "rows" refer to the active
sheet, which in your example below is not necessarily "Member ID Report
Master". I infer this from a response to an ealier post I had - this
response in listed below. Can you please let me know if I am missing
something? Basically, I want to understand whether or not I SHOULD assume
that Excel will see the rows and columns in such statements as being rows and
columns within the sheet that is referenced in that line of code.

Below is a response I had to an earlier post of mine:

Sheets("Payment Sales Master").Range(Rows(2),
Rows(2).End(xlDown)).ClearContents

- If in the "Payment Sales Master" WS, OK, as Rows refers to the same sheet
as Range.
- If in a module AND "Payment Sales Master" is the ActiveSheet, OK, as Rows
refers to the same sheet as Range.
- If in a module AND "Payment Sales Master" is NOT the ActiveSheet, ERROR,
as Rows refers to the ActiveSheet, whilst as .Range refers to
Sheets("Payment Sales Master").

--
Robert


"Ben McBen" wrote:

> Simply
>
> foobar = Columns(4)
>
>
> Should do what you want. However, you probably want to drop the "activate"
> part, as it is unecesarry if you use:
>
> Sheets("Member ID Report Master").columns(4)
>
>
>
>
>
> "robs3131" wrote:
>
> > Hi,
> >
> > I am trying to set a variable named idrange to column D in a sheet - the
> > values in this column will vary (ie - values may go through row 54 but then
> > may be updated to have values through row 67, etc). I need to capture all
> > cells in column D that have values as each cell in column D that does have a
> > value will be evaluated in a subsequent equation in the code. I have not
> > been able to figure out how to set my variable to equal the range of column
> > D...below is that code I have - I get a "Runtime error '1004';" error with
> > this code - is it possible to do what I'm trying to do?
> >
> > Sub purgepayment()
> >
> > Dim payment
> > Dim opentranpay
> > Dim rownumpay As Integer
> > Dim idrange As Range
> > Dim payrange As Range
> > Dim y As Integer
> >
> > Sheets("Member ID Report Master").Activate
> > Set idrange = Range(Columns("D"))
> >
> >
> >
> >
> > --
> > Robert

 
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
Get range (entire column) in C# sweetpotatop@yahoo.com Microsoft C# .NET 2 10th Jan 2008 03:16 PM
How do I make a range of cells in a column equal to another cell jggsfl Microsoft Excel New Users 1 21st Dec 2007 01:15 AM
Sum if Condition is Equal in Range Date and find column ldiaz Microsoft Access 0 7th Dec 2007 08:08 PM
setting a range variable equal to the value of a string variable Pilgrim Microsoft Excel Programming 2 1st Jul 2004 11:32 PM
how do i set an entire row or column to a range? strataguru Microsoft Excel Programming 7 25th Dec 2003 04:08 AM


Features
 

Advertising
 

Newsgroups
 


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