Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 54
Default Using an Input Box To Define a Range

I am trying to set a variable to a range by using an input box. Basically
it's a macro that will ask a user what range of cells the data they are
looking for is in, but I can't seem to get it. I tried it once and entered
C1:C120 into the input box but then I got an error that said Object variable
not set (and the debug highlight was on the input box for the range.

Any help?
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,986
Default Using an Input Box To Define a Range

It is easier when you post the code.

"WBTKbeezy" wrote:

I am trying to set a variable to a range by using an input box. Basically
it's a macro that will ask a user what range of cells the data they are
looking for is in, but I can't seem to get it. I tried it once and entered
C1:C120 into the input box but then I got an error that said Object variable
not set (and the debug highlight was on the input box for the range.

Any help?

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 54
Default Using an Input Box To Define a Range

Sub HIDEROWS()

Application.ScreenUpdating = False

Dim HideCell As Range
Dim HideRange As String
Dim HideValue As String

HideRange = InputBox("What is the Range of cells the Hide Values In?")
HideValue = InputBox("What is the value of the Hiding Range Text?")

For Each HideCell In HideRange.Rows

If HideCell = HideValue Then
HideCell.EntireRow.Hidden = True
End If

Next HideCell

End Sub

"JLGWhiz" wrote:

It is easier when you post the code.

"WBTKbeezy" wrote:

I am trying to set a variable to a range by using an input box. Basically
it's a macro that will ask a user what range of cells the data they are
looking for is in, but I can't seem to get it. I tried it once and entered
C1:C120 into the input box but then I got an error that said Object variable
not set (and the debug highlight was on the input box for the range.

Any help?

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,202
Default Using an Input Box To Define a Range

The return from an InputBox is Text, not a Range. Try adding this subroutine
to your code window...

Sub SetRange(RangeVariable As Range, Prompt As String)
Dim Answer As String
On Error Resume Next
Do
Answer = InputBox(Prompt)
If Len(Answer) = 0 Then
Exit Do
Else
Set RangeVariable = Range(Answer)
End If
Loop While RangeVariable Is Nothing
End Sub

and then, as one example, set your ranges like this...

Dim HideValue As Range
SetRange HideValue, "What is the value of the Hiding Range Text?"
If Not HideValue Is Nothing Then
' Put your actual code here
Debug.Print HideValue.Address
End If

Note the need to test your range against Nothing... that is because I
allowed the user to exit the SetRange subroutine by clicking OK when the
input field is empty... I did this so the user wouldn't be trapped in an
endless loop if they decided not to input a range for some reason. If you
don't want to let the user escape having to put in an actual range, then you
can use this subroutine instead...

Sub SetRange(RangeVariable As Range, Prompt As String)
Dim Answer As String
On Error Resume Next
Do
Set RangeVariable = Range(InputBox(Prompt))
Loop While RangeVariable Is Nothing
End Sub

and you won't have to test the returned range against Nothing any more...

Dim HideValue As Range
SetRange HideValue, "What is the value of the Hiding Range Text?"
' Put your actual code here
Debug.Print HideValue.Address

Your choice on how you want to approach it.

Rick



"WBTKbeezy" wrote in message
...
Sub HIDEROWS()

Application.ScreenUpdating = False

Dim HideCell As Range
Dim HideRange As String
Dim HideValue As String

HideRange = InputBox("What is the Range of cells the Hide Values In?")
HideValue = InputBox("What is the value of the Hiding Range Text?")

For Each HideCell In HideRange.Rows

If HideCell = HideValue Then
HideCell.EntireRow.Hidden = True
End If

Next HideCell

End Sub

"JLGWhiz" wrote:

It is easier when you post the code.

"WBTKbeezy" wrote:

I am trying to set a variable to a range by using an input box.
Basically
it's a macro that will ask a user what range of cells the data they are
looking for is in, but I can't seem to get it. I tried it once and
entered
C1:C120 into the input box but then I got an error that said Object
variable
not set (and the debug highlight was on the input box for the range.

Any help?


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5
Default Using an Input Box To Define a Range

Check this page: http://www.jkp-ads.com/Articles/SelectARange.asp

--
Groeten / Greetings,

Gerard


"Rick Rothstein (MVP - VB)" wrote:

The return from an InputBox is Text, not a Range. Try adding this subroutine
to your code window...

Sub SetRange(RangeVariable As Range, Prompt As String)
Dim Answer As String
On Error Resume Next
Do
Answer = InputBox(Prompt)
If Len(Answer) = 0 Then
Exit Do
Else
Set RangeVariable = Range(Answer)
End If
Loop While RangeVariable Is Nothing
End Sub

and then, as one example, set your ranges like this...

Dim HideValue As Range
SetRange HideValue, "What is the value of the Hiding Range Text?"
If Not HideValue Is Nothing Then
' Put your actual code here
Debug.Print HideValue.Address
End If

Note the need to test your range against Nothing... that is because I
allowed the user to exit the SetRange subroutine by clicking OK when the
input field is empty... I did this so the user wouldn't be trapped in an
endless loop if they decided not to input a range for some reason. If you
don't want to let the user escape having to put in an actual range, then you
can use this subroutine instead...

Sub SetRange(RangeVariable As Range, Prompt As String)
Dim Answer As String
On Error Resume Next
Do
Set RangeVariable = Range(InputBox(Prompt))
Loop While RangeVariable Is Nothing
End Sub

and you won't have to test the returned range against Nothing any more...

Dim HideValue As Range
SetRange HideValue, "What is the value of the Hiding Range Text?"
' Put your actual code here
Debug.Print HideValue.Address

Your choice on how you want to approach it.

Rick



"WBTKbeezy" wrote in message
...
Sub HIDEROWS()

Application.ScreenUpdating = False

Dim HideCell As Range
Dim HideRange As String
Dim HideValue As String

HideRange = InputBox("What is the Range of cells the Hide Values In?")
HideValue = InputBox("What is the value of the Hiding Range Text?")

For Each HideCell In HideRange.Rows

If HideCell = HideValue Then
HideCell.EntireRow.Hidden = True
End If

Next HideCell

End Sub

"JLGWhiz" wrote:

It is easier when you post the code.

"WBTKbeezy" wrote:

I am trying to set a variable to a range by using an input box.
Basically
it's a macro that will ask a user what range of cells the data they are
looking for is in, but I can't seem to get it. I tried it once and
entered
C1:C120 into the input box but then I got an error that said Object
variable
not set (and the debug highlight was on the input box for the range.

Any help?



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
Define variable range input for SUM() function JeffC Excel Worksheet Functions 4 May 16th 09 03:54 AM
Can I use a text input to define another file to reference? bob Excel Discussion (Misc queries) 5 October 6th 08 03:35 PM
Define Range in VBA loopoo[_15_] Excel Programming 2 November 22nd 05 01:38 PM
Define a range based on another named range Basil Excel Worksheet Functions 2 February 21st 05 01:47 PM
CODE to select range based on User Input or Value of Input Field Sandi Gauthier Excel Programming 4 December 8th 03 03:22 PM


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