Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 331
Default Set few range variables at once

Hi,

Is there a way to do the below implicitly in a loop or as array? Thank you!


Dim States As Range
Dim Alaska As Range, Alabama As Range, Arizona As Range 'etc

Set States = Range("a2:a51")

With States
Set Alaska = .Cells.Find("Alaska", lookat:=xlWhole, MatchCase:=False)
Set Alabama = .Cells.Find("Alabama", lookat:=xlWhole, MatchCase:=False)
'...
'..
'etc

End With

--
______
Regards,
Greg
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,588
Default Set few range variables at once

Greg,

I would rethink my approach here. You have 50 separate variables, each of which require celaration and assignment: this is not
really "loopable" as it stands.

A better approach might be to create a small function to return a range based on a State name, or to use a dictionary object to
associate names and ranges.

All depends to some extent (performance-wise) on what you're going to do with those 50 variables...

Tim

--
Tim Williams
Palo Alto, CA


"Greg" wrote in message ...
Hi,

Is there a way to do the below implicitly in a loop or as array? Thank you!


Dim States As Range
Dim Alaska As Range, Alabama As Range, Arizona As Range 'etc

Set States = Range("a2:a51")

With States
Set Alaska = .Cells.Find("Alaska", lookat:=xlWhole, MatchCase:=False)
Set Alabama = .Cells.Find("Alabama", lookat:=xlWhole, MatchCase:=False)
'...
'..
'etc

End With

--
______
Regards,
Greg



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 331
Default Set few range variables at once

The variables will be used to identify rows with data for the particular
state.

Thanks for your comments, Tim. Yes, I do feel the same way, need to come up
with some go around thinking.

--
______
Regards,
Greg


"Tim Williams" wrote:

Greg,

I would rethink my approach here. You have 50 separate variables, each of which require celaration and assignment: this is not
really "loopable" as it stands.

A better approach might be to create a small function to return a range based on a State name, or to use a dictionary object to
associate names and ranges.

All depends to some extent (performance-wise) on what you're going to do with those 50 variables...

Tim

--
Tim Williams
Palo Alto, CA


"Greg" wrote in message ...
Hi,

Is there a way to do the below implicitly in a loop or as array? Thank you!


Dim States As Range
Dim Alaska As Range, Alabama As Range, Arizona As Range 'etc

Set States = Range("a2:a51")

With States
Set Alaska = .Cells.Find("Alaska", lookat:=xlWhole, MatchCase:=False)
Set Alabama = .Cells.Find("Alabama", lookat:=xlWhole, MatchCase:=False)
'...
'..
'etc

End With

--
______
Regards,
Greg




  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Set few range variables at once

Maybe you could use a collection, so you could refer to the row by using the
state name:

Option Explicit
Sub testme01()

Dim myStates As Variant
Dim myStateCells As Collection
Dim FoundCell As Range
Dim iCtr As Long
Dim myStateName As String

Set myStateCells = New Collection

myStates = Array("alaska", "alabama", "illinois", "Wyoming")
'add in all 50 + DC???

With States
For iCtr = LBound(myStates) To UBound(myStates)
Set FoundCell = .Cells.Find(what:=myStates(iCtr), _
After:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
LookAt:=xlWhole, SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False)
myStateCells.Add FoundCell, myStates(iCtr)
Next iCtr
End With

myStateName = "alaska"
If IsError(Application.Match(myStateName, myStates, 0)) Then
MsgBox "Misspelled State Name!"
Else
'find alaska cell
If myStateCells(myStateName) Is Nothing Then
MsgBox "Not found"
Else
MsgBox myStateCells(myStateName).Row
End If
End If

End Sub



Greg wrote:

Hi,

Is there a way to do the below implicitly in a loop or as array? Thank you!

Dim States As Range
Dim Alaska As Range, Alabama As Range, Arizona As Range 'etc

Set States = Range("a2:a51")

With States
Set Alaska = .Cells.Find("Alaska", lookat:=xlWhole, MatchCase:=False)
Set Alabama = .Cells.Find("Alabama", lookat:=xlWhole, MatchCase:=False)
'...
'..
'etc

End With

--
______
Regards,
Greg


--

Dave Peterson
  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,600
Default Set few range variables at once

A couple of ideas involving collections, marked A & B

Sub test()
Dim i As Long
Dim rng As Range
Dim ColRanges As Collection 'A
Dim colNames As Collection 'B
Dim arrStates()

arrStates = Array("Alaska", "Alabama", "Arizona")

ReDim ar(1 To UBound(arrStates) + 1) As Range

i = 0
Set ColRanges = New Collection ' A
Set colNames = New Collection ' B

For Each zone In arrStates
i = i + 1
ColRanges.Add Cells(i, 1), zone 'A

Set ar(i) = Cells(i, 2) 'B
colNames.Add i, zone 'B
Next

i = 0
For Each r In ColRanges 'A
i = i + 1
Debug.Print r.Address
Next

Debug.Print "Arizona", ColRanges("Arizona").Address 'A

Debug.Print ar(colNames("Arizona")).Address 'B

End Sub

For your Set to found cells (subject error handing)

method A
ColRanges.Add foundRange, "myState"

method B
first populate the colNames collection
Set ar(colNames("myState")) = foundRange

Regards,
Peter T

"Greg" wrote in message
...
Hi,

Is there a way to do the below implicitly in a loop or as array? Thank

you!


Dim States As Range
Dim Alaska As Range, Alabama As Range, Arizona As Range 'etc

Set States = Range("a2:a51")

With States
Set Alaska = .Cells.Find("Alaska", lookat:=xlWhole, MatchCase:=False)
Set Alabama = .Cells.Find("Alabama", lookat:=xlWhole,

MatchCase:=False)
'...
'..
'etc

End With

--
______
Regards,
Greg



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
Adding a range with three variables Saul Excel Discussion (Misc queries) 4 July 25th 08 02:21 PM
Range of variables Ed Excel Programming 2 August 5th 06 03:05 PM
Describing a Range with Variables mazo Excel Programming 2 May 9th 06 02:28 PM
Naming Range Variables scott Excel Programming 6 January 7th 05 10:37 PM
Range Variables Daniel[_7_] Excel Programming 1 November 20th 03 05:45 AM


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