Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 18
Default Selecting range with loop in VBasic

I have a list of 10,000 lines of data starting from row 2 in Excel 2007. I
would like to select a range of cells from each line individually (columns C
through L) for each of the 10k lines and sort horizontally. I am trying to
use a do loop to do this, but I cannot set the range correctly with a
variable (say c) to create a dynamic range. Here is a simplified view of
what I have:

c = 2
Do While c < 15000
Range("C2:L2").Select
c = c +1
Loop

What syntax do I use in the Range.Select argument using the c variable to
move down a line each time I run through the Do Loop?

Thanks,
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Selecting range with loop in VBasic

There are lots of different ways to get that range to sort.

One way:

Option Explicit
Sub testme()

Dim iRow As Long
Dim RngToSort As Range

For iRow = 2 To 10000
Set RngToSort = Worksheets("sheet1").Cells(iRow, "A").Range("c1:L1")

With RngToSort
.Sort Key1:=.Cells(1), _
Order1:=xlAscending, _
Header:=xlNo, _
OrderCustom:=1, _
MatchCase:=False, _
Orientation:=xlLeftToRight, _
DataOption1:=xlSortNormal
End With
Next iRow
End Sub

I could have used:
Set RngToSort = Worksheets("sheet1").Cells(iRow, "A").offset(0,2).resize(1,10)
Starting with the cell in column A, shift (offset) 2 columns to get to column C
and then resize to 1 row by 10 columns.

or just start with column C and avoid the .offset.
Set RngToSort = Worksheets("sheet1").Cells(iRow, "c").resize(1,10)

or

with worksheets(Sheet1")
set rngtosort = .range(.cells(irow,"C"),.cells(irow,"L"))
end with

with rngtosort ....


SeanF74 wrote:

I have a list of 10,000 lines of data starting from row 2 in Excel 2007. I
would like to select a range of cells from each line individually (columns C
through L) for each of the 10k lines and sort horizontally. I am trying to
use a do loop to do this, but I cannot set the range correctly with a
variable (say c) to create a dynamic range. Here is a simplified view of
what I have:

c = 2
Do While c < 15000
Range("C2:L2").Select
c = c +1
Loop

What syntax do I use in the Range.Select argument using the c variable to
move down a line each time I run through the Do Loop?

Thanks,


--

Dave Peterson
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 135
Default Selecting range with loop in VBasic

Hi,

One more variations on this theme :

'----------------------------------
Sub test()
Dim LastRow As Long
Dim Rg As Range, R As Range

With Worksheets("Sheet1") 'Adapte name sheet
LastRow = .Range("C:H").Find(What:="*", _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious).Row
Set Rg = .Range("C2:H" & LastRow)
End With
For Each R In Rg.Rows
MySort R
Next
End Sub
'----------------------------------
Sub MySort(R As Range)
With R
.Sort Key1:=R(1), Order1:=xlAscending, _
Header:=xlNo, OrderCustom:=1, _
MatchCase:=False, Orientation:=xlLeftToRight
End With
End Sub
'----------------------------------



"SeanF74" a écrit dans le message de groupe de
discussion : ...
I have a list of 10,000 lines of data starting from row 2 in Excel 2007. I
would like to select a range of cells from each line individually (columns C
through L) for each of the 10k lines and sort horizontally. I am trying to
use a do loop to do this, but I cannot set the range correctly with a
variable (say c) to create a dynamic range. Here is a simplified view of
what I have:

c = 2
Do While c < 15000
Range("C2:L2").Select
c = c +1
Loop

What syntax do I use in the Range.Select argument using the c variable to
move down a line each time I run through the Do Loop?

Thanks,

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
Compile Errors in Excel VBasic n250jg Excel Programming 2 June 26th 09 07:49 PM
'spin buttons' & vbasic language Browny Excel Discussion (Misc queries) 0 May 9th 08 04:27 AM
Selecting a range by using a loop lasca Excel Programming 2 October 25th 07 04:51 PM
resetting Public Arrays in VBASIC Max Excel Programming 1 April 18th 06 07:06 PM
Formula in VBasic SB Excel Discussion (Misc queries) 3 July 27th 05 09:46 AM


All times are GMT +1. The time now is 05:20 PM.

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"