Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 18
Default Run piece of code for each line of data

I have a spreadsheet that will have a varying number of rows of data. I need
to write a macro that will (for each row of data) run my code until there are
no more rows of data.

Also, my data rows do not start at the top of the sheet, the rows start at
row 5 so I guess I will need some type of offset or something like that.

While there's rows of data

- my code here -

End when there's no more data

Any help would be greatly appreciated.
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 175
Default Run piece of code for each line of data

This suggestion isnt using "code" but just some native-excel functions...

You might be able to use the =countif() function to generate a cell
reference array that exactly fits your data as it changes.

If your data is populated from A5 through F105 for example... you would need
a cell reference stating "$A$5:$F$105". Use Countif(A:A," ") to return 100
(assuming that no headings are in row 1-4 but a simple offset can adjust for
them) Then make cell AA1 ="$A$5:$F$"&Countif(A:A," ")+5
Then reference this cell using "INDIRECT(AA1)" in any formulas that need a
dynamically changing cell-reference for your array of data.

NOTE: the " " is 2 characters, a greater than and a space, which will count
the entries that are greater than a space.

Hope this helps (i'm still learning the "code" part too)

"Diane Alsing" wrote:

I have a spreadsheet that will have a varying number of rows of data. I need
to write a macro that will (for each row of data) run my code until there are
no more rows of data.

Also, my data rows do not start at the top of the sheet, the rows start at
row 5 so I guess I will need some type of offset or something like that.

While there's rows of data

- my code here -

End when there's no more data

Any help would be greatly appreciated.

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 983
Default Run piece of code for each line of data

Assuming the data is continuious starting in cell A5 through A?. Here is some
fairly easy code to understand. It is not necessarily the most efficient not
the most fool proof, but it will work and should get you started.

sheets("Sheet1").Range("A5").Select

do while activecell.value < ""
msgbox activecell.value
loop

Give this a try and let me know if you wnat some more help...

HTH

"Diane Alsing" wrote:

I have a spreadsheet that will have a varying number of rows of data. I need
to write a macro that will (for each row of data) run my code until there are
no more rows of data.

Also, my data rows do not start at the top of the sheet, the rows start at
row 5 so I guess I will need some type of offset or something like that.

While there's rows of data

- my code here -

End when there's no more data

Any help would be greatly appreciated.

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 983
Default Run piece of code for each line of data

sorry in my code I forgot to add a line after the messagebox

activecell.offset(1,0).select

Sorry....

HTH

"Diane Alsing" wrote:

I have a spreadsheet that will have a varying number of rows of data. I need
to write a macro that will (for each row of data) run my code until there are
no more rows of data.

Also, my data rows do not start at the top of the sheet, the rows start at
row 5 so I guess I will need some type of offset or something like that.

While there's rows of data

- my code here -

End when there's no more data

Any help would be greatly appreciated.

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 14
Default Run piece of code for each line of data

Try using CurrentRegion

Dim MyRng as Range
Dim cell as Range
Dim i as Integer

' This sets MyRng to the entire region or area of cells that contain data
and "touch" cell A5
Set MyRng = Range("A5").CurrentRegion

For Each cell In MyRng.Columns(1).Cells
cell.Select
For i = 0 to MyRng.Columns.Count
MsgBox ActiveCell.OffSet(0, i - 1).Value
Next i
Next cell

This will cycle through all cells in your range, where MyRng represents the
entire range of rows and columns. The outer loop just cycles through the
first cell in each row, and the inner loop cycles through all cells in the
row. The loop

For Each cell In MyRng.Cells
MsgBox cell.Value
Next cell

will cycle through all cells in the entire range also, but distinguishing
between rows is more difficult. The example I listed has a flaw in that if
one of the rows has no data in it whatsoever, then the CurrentRegion method
will fail.

There are lots of ways to loop through a range. Think about setting a range
variable to the entire range of data, and look at the Row.Count,
Column.Count and Offset properties and methods also.

HTH.

-gk-




"Diane Alsing" wrote in message
...
I have a spreadsheet that will have a varying number of rows of data. I
need
to write a macro that will (for each row of data) run my code until there
are
no more rows of data.

Also, my data rows do not start at the top of the sheet, the rows start at
row 5 so I guess I will need some type of offset or something like that.

While there's rows of data

- my code here -

End when there's no more data

Any help would be greatly appreciated.





  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 18
Default Run piece of code for each line of data

I ended up combining all these options (thanks for the help!) I used the
counta function to find out how many rows I had, then used the
activesheet.Cells in a for next loop -

"Jim Thomlinson" wrote:

Assuming the data is continuious starting in cell A5 through A?. Here is some
fairly easy code to understand. It is not necessarily the most efficient not
the most fool proof, but it will work and should get you started.

sheets("Sheet1").Range("A5").Select

do while activecell.value < ""
msgbox activecell.value
loop

Give this a try and let me know if you wnat some more help...

HTH

"Diane Alsing" wrote:

I have a spreadsheet that will have a varying number of rows of data. I need
to write a macro that will (for each row of data) run my code until there are
no more rows of data.

Also, my data rows do not start at the top of the sheet, the rows start at
row 5 so I guess I will need some type of offset or something like that.

While there's rows of data

- my code here -

End when there's no more data

Any help would be greatly appreciated.

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
Interpretation of a piece of code FARAZ QURESHI Excel Discussion (Misc queries) 3 December 30th 07 11:29 PM
What is wrong with this vba piece of code? Jo[_2_] Excel Discussion (Misc queries) 4 October 4th 07 05:01 PM
Need to "not show" piece of line in graph Callan Charts and Charting in Excel 5 October 8th 05 03:42 AM
changing a piece of code Ajit Excel Programming 0 September 17th 04 02:45 PM
Query on small piece of code Mike[_65_] Excel Programming 7 March 4th 04 03:23 PM


All times are GMT +1. The time now is 02:41 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"