ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Help with defining a Range (https://www.excelbanter.com/excel-programming/337080-help-defining-range.html)

drhalter

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

Bob Phillips[_6_]

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




drhalter

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






All times are GMT +1. The time now is 04:06 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com