Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default How do I programmatically select various ranges in vba?

From what I understand you must use Application.Union to combine large Range
groups in vba to use in Excel.

Somthing like...

Dim BigRange as Range, R(10) as String
R(1) = "1:1, 3:3"
R(2) = "7:7, 20:20, 43:43"
R(3) = "100:100"

Set BigRange = Application.Union ( Range (R(1)), Range (R(2)), Range
(R(3)) )
BigRange.Select


Which is fine and dandy with just the 3, but if I had a hundred or so ranges
how would I create a loop statment to combine the various ranges.

*The ranges would contain the entire row of a worksheet and only values from
the first column that contain the text "PICK ME!" would be selected.


Any help is much appreciated!
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 83
Default How do I programmatically select various ranges in vba?

On Tue, 23 Mar 2010 12:51:01 -0700, The Flash
wrote:

From what I understand you must use Application.Union to combine large Range
groups in vba to use in Excel.

Somthing like...

Dim BigRange as Range, R(10) as String
R(1) = "1:1, 3:3"
R(2) = "7:7, 20:20, 43:43"
R(3) = "100:100"

Set BigRange = Application.Union ( Range (R(1)), Range (R(2)), Range
(R(3)) )
BigRange.Select


Which is fine and dandy with just the 3, but if I had a hundred or so ranges
how would I create a loop statment to combine the various ranges.

*The ranges would contain the entire row of a worksheet and only values from
the first column that contain the text "PICK ME!" would be selected.


Any help is much appreciated!


Try this as an alternative to you Set statement

Set BigRange = Range(R(1))
For i = 2 To 100
Set BigRange = Application.Union(BigRange, Range(R(i)))
Next i

Hope this helps / Lars-Åke
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 342
Default How do I programmatically select various ranges in vba?

I am not sure that Application Union is necessary. I wrote the following to
select all of the rows in the active worksheet that have "Pick Me!" in column
A. I hope that is what you had in mind.

Sub mcrSelect_Pick_Me()

Dim rngRow As Range
Dim strAll As String

For Each rngRow In ActiveSheet.UsedRange.Rows
If Range("A" & rngRow.Row) = "Pick Me!" Then
strAll = strAll & rngRow.Row & ":" & rngRow.Row & ","
End If
Next rngRow

strAll = Left(strAll, Len(strAll) - 1)
Range(strAll).Select

End Sub

tom
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 342
Default How do I programmatically select various ranges in vba?

Nice Lars.

Here is an alternative that does not require the program to identify the
first range to be set "manually".

Sub mcrSelect_Pick_Me2()

Dim rngBig As Range
Dim rngRow As Range

For Each rngRow In ActiveSheet.UsedRange.Rows
If Range("A" & rngRow.Row) = "Pick Me!" Then
If rngBig Is Nothing Then
Set rngBig = rngRow
Else
Set rngBig = Application.Union(rngBig, rngRow)
End If
End If
Next rngRow

rngBig.Select

End Sub

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 690
Default How do I programmatically select various ranges in vba?

Set BigRange = Application.Union ( Range (R(1)), Range (R(2)),
Range(R(3)) )

Hi. As a side note, you do not have to type in each of the indexes.
However, if you really do have "Hundreds" of indexes, then I think there
'might' be a size limit to this technique. Not sure. ??

Sub Demo()
Dim BigRange As Range
Dim R(1 To 3) As String

R(1) = "1:1, 3:3"
R(2) = "7:7, 20:20, 43:43"
R(3) = "100:100"

Set BigRange = Range(Join(R, ","))
BigRange.Select
End Sub

HTH
Dana DeLouis


On 3/23/2010 3:51 PM, The Flash wrote:
From what I understand you must use Application.Union to combine large Range
groups in vba to use in Excel.

Somthing like...

Dim BigRange as Range, R(10) as String
R(1) = "1:1, 3:3"
R(2) = "7:7, 20:20, 43:43"
R(3) = "100:100"

Set BigRange = Application.Union ( Range (R(1)), Range (R(2)), Range
(R(3)) )
BigRange.Select


Which is fine and dandy with just the 3, but if I had a hundred or so ranges
how would I create a loop statment to combine the various ranges.

*The ranges would contain the entire row of a worksheet and only values from
the first column that contain the text "PICK ME!" would be selected.


Any help is much appreciated!



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default How do I programmatically select various ranges in vba?

Thanks guys!

"Dana DeLouis" wrote:

Set BigRange = Application.Union ( Range (R(1)), Range (R(2)),

Range(R(3)) )

Hi. As a side note, you do not have to type in each of the indexes.
However, if you really do have "Hundreds" of indexes, then I think there
'might' be a size limit to this technique. Not sure. ??

Sub Demo()
Dim BigRange As Range
Dim R(1 To 3) As String

R(1) = "1:1, 3:3"
R(2) = "7:7, 20:20, 43:43"
R(3) = "100:100"

Set BigRange = Range(Join(R, ","))
BigRange.Select
End Sub

HTH
Dana DeLouis


On 3/23/2010 3:51 PM, The Flash wrote:
From what I understand you must use Application.Union to combine large Range
groups in vba to use in Excel.

Somthing like...

Dim BigRange as Range, R(10) as String
R(1) = "1:1, 3:3"
R(2) = "7:7, 20:20, 43:43"
R(3) = "100:100"

Set BigRange = Application.Union ( Range (R(1)), Range (R(2)), Range
(R(3)) )
BigRange.Select


Which is fine and dandy with just the 3, but if I had a hundred or so ranges
how would I create a loop statment to combine the various ranges.

*The ranges would contain the entire row of a worksheet and only values from
the first column that contain the text "PICK ME!" would be selected.


Any help is much appreciated!

.

  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 690
Default How do I programmatically select various ranges in vba?

Hi. As a side note, this was on my "ToDo" list for my Library, so let
me do it now.
If interested, instead of writing rows as "1:1, 3:3, 5:5"
here is a technique to write it as "1,3,5"

In Vba, you need to set a library reference to "BScript_RegExp_55"

Sub Main()
Dim R(1 To 3) As String
Dim BigRng As Range

Dim s As String
R(1) = "1,3"
R(2) = "7,10,13"
R(3) = "15"

Set BigRng = Range(ToRows(Join(R, ",")))
BigRng.Select
End Sub

Function ToRows(s) As String
' = = = = = = = = = = = = = = =
'// By: Dana DeLouis
'// VBA Library Ref to VBScript_RegExp_55
'// Input: Rows as single digits
'// ie "1,3,5"
'// Returns Proper Row Reference
'// ie "1:1, 3:3, 5:5"
' = = = = = = = = = = = = = = =


With New RegExp
.Pattern = "(\d+)"
.Global = True
ToRows = .Replace(s, "$1:$1")
End With
End Function

= = = = = = = = = =
HTH :)
Dana DeLouis



On 3/24/2010 9:26 AM, The Flash wrote:
Thanks guys!

"Dana DeLouis" wrote:

Set BigRange = Application.Union ( Range (R(1)), Range (R(2)),

Range(R(3)) )

Hi. As a side note, you do not have to type in each of the indexes.
However, if you really do have "Hundreds" of indexes, then I think there
'might' be a size limit to this technique. Not sure. ??

Sub Demo()
Dim BigRange As Range
Dim R(1 To 3) As String

R(1) = "1:1, 3:3"
R(2) = "7:7, 20:20, 43:43"
R(3) = "100:100"

Set BigRange = Range(Join(R, ","))
BigRange.Select
End Sub

HTH
Dana DeLouis


On 3/23/2010 3:51 PM, The Flash wrote:
From what I understand you must use Application.Union to combine large Range
groups in vba to use in Excel.

Somthing like...

Dim BigRange as Range, R(10) as String
R(1) = "1:1, 3:3"
R(2) = "7:7, 20:20, 43:43"
R(3) = "100:100"

Set BigRange = Application.Union ( Range (R(1)), Range (R(2)), Range
(R(3)) )
BigRange.Select


Which is fine and dandy with just the 3, but if I had a hundred or so ranges
how would I create a loop statment to combine the various ranges.

*The ranges would contain the entire row of a worksheet and only values from
the first column that contain the text "PICK ME!" would be selected.


Any help is much appreciated!

.



--
= = = = = = =
HTH :)
Dana DeLouis
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
Programmatically allowing user to select worksheet Barb Reinhardt Excel Programming 2 November 27th 07 06:08 PM
Build Select Case Programmatically Dave Birley Excel Programming 11 June 1st 07 05:39 PM
Programmatically Protect/Unprotect Ranges in Excel 2003? Tony Excel Programming 2 May 23rd 07 05:27 PM
Programmatically Select Tab on Ribbon? [email protected] Excel Programming 1 February 21st 07 01:59 PM
VBA-Select several ranges using variables waveracerr[_8_] Excel Programming 2 February 10th 04 11:12 PM


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