Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 39
Default Help with defining a Range

I'm using a VB to define a range and then loop through the range to fill a
combobox on a form. The code works fine if the selected sheet is the sheet
on which VB draws the range, but the code fails when some other sheet is
selected. So, simplifying things, I've got two sheets, Sheet1(Overall) and
Sheet27(Checking). When I run the form from Sheet1(Overall), I have no
problem. When I run it with some other sheet active, I get an error message.
I think the problem has to do with the Set... command.

Here's the code (including the loop):

Dim myRange As Range
Set myRange = Sheets("Overall").Range(Range("B2"), Range("B2").End(xlDown))

Dim a As Variant
Dim lrow As Integer

'fills combobox1 and sets the variable lrow
For Each a In myRange
ComboBox1.AddItem a
lrow = lrow + 1
Next a
ComboBox1.ListRows = lrow


Now, I know someone will say, simply use VB to select the "Overall" sheet,
then run the Set command. This does work, but it seems rather unnecessary.
How do I specify the range on the Overall sheet without having to activate
and select it first?

Thanks,
Dave
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default Help with defining a Range

I hope no-one will say that <g!

See if this sorts it

With Sheets("Overall")
Set myRange = .Range(.Range("B2"), .Range("B2").End(xlDown))
End With


--

HTH

RP
(remove nothere from the email address if mailing direct)


"drhalter" wrote in message
...
I'm using a VB to define a range and then loop through the range to fill a
combobox on a form. The code works fine if the selected sheet is the

sheet
on which VB draws the range, but the code fails when some other sheet is
selected. So, simplifying things, I've got two sheets, Sheet1(Overall)

and
Sheet27(Checking). When I run the form from Sheet1(Overall), I have no
problem. When I run it with some other sheet active, I get an error

message.
I think the problem has to do with the Set... command.

Here's the code (including the loop):

Dim myRange As Range
Set myRange = Sheets("Overall").Range(Range("B2"),

Range("B2").End(xlDown))

Dim a As Variant
Dim lrow As Integer

'fills combobox1 and sets the variable lrow
For Each a In myRange
ComboBox1.AddItem a
lrow = lrow + 1
Next a
ComboBox1.ListRows = lrow


Now, I know someone will say, simply use VB to select the "Overall" sheet,
then run the Set command. This does work, but it seems rather

unnecessary.
How do I specify the range on the Overall sheet without having to activate
and select it first?

Thanks,
Dave



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 39
Default Help with defining a Range

Looks like a winner. Thanks, Bob.


"Bob Phillips" wrote:

I hope no-one will say that <g!

See if this sorts it

With Sheets("Overall")
Set myRange = .Range(.Range("B2"), .Range("B2").End(xlDown))
End With


--

HTH

RP
(remove nothere from the email address if mailing direct)


"drhalter" wrote in message
...
I'm using a VB to define a range and then loop through the range to fill a
combobox on a form. The code works fine if the selected sheet is the

sheet
on which VB draws the range, but the code fails when some other sheet is
selected. So, simplifying things, I've got two sheets, Sheet1(Overall)

and
Sheet27(Checking). When I run the form from Sheet1(Overall), I have no
problem. When I run it with some other sheet active, I get an error

message.
I think the problem has to do with the Set... command.

Here's the code (including the loop):

Dim myRange As Range
Set myRange = Sheets("Overall").Range(Range("B2"),

Range("B2").End(xlDown))

Dim a As Variant
Dim lrow As Integer

'fills combobox1 and sets the variable lrow
For Each a In myRange
ComboBox1.AddItem a
lrow = lrow + 1
Next a
ComboBox1.ListRows = lrow


Now, I know someone will say, simply use VB to select the "Overall" sheet,
then run the Set command. This does work, but it seems rather

unnecessary.
How do I specify the range on the Overall sheet without having to activate
and select it first?

Thanks,
Dave




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
Defining Range Name anshu[_2_] Excel Discussion (Misc queries) 2 July 22nd 07 07:30 AM
Defining a range Don Excel Worksheet Functions 1 February 25th 05 03:54 PM
Defining a Range Ric[_5_] Excel Programming 4 April 26th 04 07:34 PM
Defining range Edgar[_3_] Excel Programming 2 February 17th 04 02:24 PM
Defining a range Matt Excel Programming 3 January 23rd 04 03:21 PM


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