Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Excel / New Users / October 2007

Tip: Looking for answers? Try searching our database.

Loop a

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Ben - 28 Oct 2007 12:57 GMT
Guys
I am programming up a macro in Excel, I was just wondering how to loop
the range function.

Whereby I would like to somehow put a variable in the function Range,
for a set column, eg.

dim i as integer
dim c(0) as string
i=0

for i =117 to 200
c(0) = Range("C" & i)   ' This is the line that doesn't work
msgbox c(0)   'for simplicity

next i

Thanks
Bob Phillips - 28 Oct 2007 14:05 GMT
No need for a loop

Dim c

   c = Application.Transpose(Range("C117:C200"))

Signature

HTH

Bob

(there's no email, no snail mail, but somewhere should be gmail in my addy)

> Guys
> I am programming up a macro in Excel, I was just wondering how to loop
[quoted text clipped - 14 lines]
>
> Thanks
www.exciter.gr: Custom Excel Applications! - 28 Oct 2007 14:06 GMT
Instead of the range function you could use the cells function.

Example:

for i =117 to 200
c(0) = Cells(i, 3)   ' This means cell with row i and column 3: when
i=117 it will be C117, C118... to C200
msgbox c(0)

Good Luck!

> Guys
> I am programming up a macro in Excel, I was just wondering how to loop
[quoted text clipped - 14 lines]
>
> Thanks
Ken Johnson - 28 Oct 2007 14:11 GMT
> Guys
> I am programming up a macro in Excel, I was just wondering how to loop
[quoted text clipped - 14 lines]
>
> Thanks

Hi Ben,

What do you mean when you say "doesn't work"?

Your code worked for me.

Ken Johnson
Dave Peterson - 28 Oct 2007 14:12 GMT
Dim i as long
dim c() as variant 'could be numbers or strings???
dim FirstNum as Long
dim LastNum as Long
firstnum = 117
lastnum = 200

redim c(firstnum to lastnum)
for i = lbound(c) to ubound(c)
 c(i) = activesheet.range("C" & i).value
 'or  c(i) = activesheet.cells(i,"C").value
next i

======
This will create an array with lower bound 117 and upper bound 200:
dim C(117 to 200) as variant

If you really wanted to start at 0:
Dim i as long
dim c() as variant 'could be numbers or strings???
dim FirstNum as Long
dim LastNum as Long
firstnum = 117
lastnum = 200

redim c(0 to lastnum-firstnum)
for i = lbound(c) to ubound(c)
 c(i) = activesheet.range("C" & i+firstnum).value
 'or  c(i) = activesheet.cells(i+firstnum,"C").value
next i

==========
Alternatively, you could pick up the values in a 2 dimensional array in one fell
swoop:

Dim c as variant
dim i as long
c = activesheet.range("C117:C200")
for i = lbound(c,1) to ubound(c,1)
 msgbox c(i,1)
next i

C is a 2 dimensional range--84 rows by 1 column
kind of like:
dim c(1 to 84, 1 to 1) as variant

> Guys
> I am programming up a macro in Excel, I was just wondering how to loop
[quoted text clipped - 14 lines]
>
> Thanks

Signature

Dave Peterson

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.