Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 22
Default Loop from left to the right

With Worksheets("sheetA")
Set rng = .Range("a2", .Range("a" & Rows.Count).End(xlUp))
For Each X In rng
'
Next X
End With

With above code i can loop through all rows of my data in column(A).
Say my data consists of unknown numbers of rows and columns and i want to loop from last cell of column(A) to the right most column which is unkown. I mean from left most to the right most column of last row with data.
How can i modify above code for that purpose ?
Thank you very much in advance.
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 538
Default Loop from left to the right

"rumkus" wrote:

With Worksheets("sheetA")
Set rng = .Range("a2", .Range("a" & Rows.Count).End(xlUp))
For Each X In rng
'
Next X
End With

With above code i can loop through all rows of my data in column(A).
Say my data consists of unknown numbers of rows and columns and i want
to loop from last cell of column(A) to the right most column which is
unkown. I mean from left most to the right most column of last row with
data. How can i modify above code for that purpose ?


I'd do it like this:

'find the bottom of A:
ro = Cells(Cells.SpecialCells(xlCellTypeLastCell).Row, 1).End(xlUp).Row
'step through all cells in that row:
For col = 1 To Cells.SpecialCells(xlCellTypeLastCell).Column
Debug.Print Cells(ro, col).Value
Next

Adjust as you see fit.

--
- We have to compromise!
- No. Not even in the face of Armageddon. Never compromise.
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 621
Default Loop from left to the right

Dim rng As Range
Dim I As Integer
Dim X as range
I = 2
With Worksheets("sheet2")
Set rng = .Range(Cells(I, "A"), Cells(I, Columns.Count).End(xlToLeft))
For Each X in rng
'do stuff
Next X
End With



Gord


On Mon, 12 Mar 2012 14:23:51 -0700 (PDT), wrote:

With Worksheets("sheetA")
Set rng = .Range("a2", .Range("a" & Rows.Count).End(xlUp))
For Each X In rng
'
Next X
End With

With above code i can loop through all rows of my data in column(A).
Say my data consists of unknown numbers of rows and columns and i want to loop from last cell of column(A) to the right most column which is unkown. I mean from left most to the right most column of last row with data.
How can i modify above code for that purpose ?
Thank you very much in advance.

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 621
Default Loop from left to the right

Kinda missed the reading part.

Thought you wanted to go across from A2.

See Auric's post.


Gord

On Mon, 12 Mar 2012 15:33:27 -0700, Gord Dibben
wrote:

Dim rng As Range
Dim I As Integer
Dim X as range
I = 2
With Worksheets("sheet2")
Set rng = .Range(Cells(I, "A"), Cells(I, Columns.Count).End(xlToLeft))
For Each X in rng
'do stuff
Next X
End With



Gord


On Mon, 12 Mar 2012 14:23:51 -0700 (PDT), wrote:

With Worksheets("sheetA")
Set rng = .Range("a2", .Range("a" & Rows.Count).End(xlUp))
For Each X In rng
'
Next X
End With

With above code i can loop through all rows of my data in column(A).
Say my data consists of unknown numbers of rows and columns and i want to loop from last cell of column(A) to the right most column which is unkown. I mean from left most to the right most column of last row with data.
How can i modify above code for that purpose ?
Thank you very much in advance.

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,514
Default Loop from left to the right

One way...

Dim vData As Variant, i As Long, j As Long

'Get the data
vData = Sheets("sheetA").UsedRange
For i = LBound(vData) To UBound(vData)
For j = LBound(vData, 2) To UBound(vData, 2)
vData(i, j) = UCase$(vData(i, j)) 'do stuff
Next 'j
Next 'i

'Put the modified data back
Sheets("sheetA").Range("A1").Resize(i, j) = vData

...as iterating an array will always be faster than read/write each cell
in a range.

--
Garry

Free usenet access at http://www.eternal-september.org
ClassicVB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion




  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 22
Default Loop from left to the right

And who says God hasn't left any angels on this world ?
Thank you so much.
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
when inserting new worksheets they read right to left not left to. Andy Setting up and Configuration of Excel 2 December 3rd 08 09:51 PM
Match, then Look Left 4 Columns, then Loop to End of Range ryguy7272 Excel Programming 9 November 22nd 08 08:42 PM
Loop Until Desired Columns left SteveT Excel Programming 3 November 29th 07 11:22 PM
How to make a cell appear in upper left (top left) corner of works jeff Excel Programming 2 March 6th 07 10:14 PM
How to change the right-to-left worksheet to left-to-right workshe RAMA Excel Discussion (Misc queries) 1 July 4th 05 01:57 PM


All times are GMT +1. The time now is 04:03 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"