Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Range through Inputbox


Hi,

Why the following code produces an error?

Public Sub aSub()

Const msg1 = "Select input range"
Const msg2 = "Select output cell"

Dim WB As Workbook
Dim SH As Worksheet
Dim Rng As Range
Dim destRng As Range
Dim rngInput As Range, rngOutput As Range

Set rngInput = Application.InputBox(msg1, Type:=8)
Set rngOutput = Application.InputBox(msg2, Type:=8)
If rngInput Is Nothing Then Exit Sub
If rngOutput Is Nothing Then Exit Sub

Set WB = ActiveWorkbook
Set SH = WB.ActiveSheet
Set Rng = SH.Range(rngInput)
Set destRng = SH.Range(rngOutput)

End Sub

While if I change the last rows, for example
Set Rng=SH.Range("B1:C1000")
Set destRng = SH.Range("F2")

it works

I want to use the ranges selected with inputboxes, and not change the code!
Where is the mistake?

Thanks
j.


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9,101
Default Range through Inputbox

I don't think anything is really wrong except Excel doesn't like the Dim
statements. I got the code below to work by commenting out the DIM. I would
like to know the answer also. I often have to comment out the DIM statements
because I don't know what is the correct format to use.

Public Sub aSub()

Const msg1 = "Select input range"
Const msg2 = "Select output cell"

Dim WB As Workbook
Dim SH As Worksheet
Dim Rng As Range
' Dim destRng As Range
'Dim rngInput As Range ', rngOutput As Range

Set rngInput = Application.InputBox(msg1, Type:=8)
Set rngOutput = Application.InputBox(msg2, Type:=8)
If rngInput Is Nothing Then Exit Sub
If rngOutput Is Nothing Then Exit Sub

Set WB = ActiveWorkbook
Set SH = WB.ActiveSheet
Set Rng = rngInput
Set destRng = rngOutput

End Sub


"jasmine" wrote:


Hi,

Why the following code produces an error?

Public Sub aSub()

Const msg1 = "Select input range"
Const msg2 = "Select output cell"

Dim WB As Workbook
Dim SH As Worksheet
Dim Rng As Range
Dim destRng As Range
Dim rngInput As Range, rngOutput As Range

Set rngInput = Application.InputBox(msg1, Type:=8)
Set rngOutput = Application.InputBox(msg2, Type:=8)
If rngInput Is Nothing Then Exit Sub
If rngOutput Is Nothing Then Exit Sub

Set WB = ActiveWorkbook
Set SH = WB.ActiveSheet
Set Rng = SH.Range(rngInput)
Set destRng = SH.Range(rngOutput)

End Sub

While if I change the last rows, for example
Set Rng=SH.Range("B1:C1000")
Set destRng = SH.Range("F2")

it works

I want to use the ranges selected with inputboxes, and not change the code!
Where is the mistake?

Thanks
j.



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Range through Inputbox


Thank you! It works, but I have to comment Option Explicit too...

Regards
j.

I don't think anything is really wrong except Excel doesn't like the Dim
statements. I got the code below to work by commenting out the DIM. I
would
like to know the answer also. I often have to comment out the DIM
statements
because I don't know what is the correct format to use.

Public Sub aSub()

Const msg1 = "Select input range"
Const msg2 = "Select output cell"

Dim WB As Workbook
Dim SH As Worksheet
Dim Rng As Range
' Dim destRng As Range
'Dim rngInput As Range ', rngOutput As Range

Set rngInput = Application.InputBox(msg1, Type:=8)
Set rngOutput = Application.InputBox(msg2, Type:=8)
If rngInput Is Nothing Then Exit Sub
If rngOutput Is Nothing Then Exit Sub

Set WB = ActiveWorkbook
Set SH = WB.ActiveSheet
Set Rng = rngInput
Set destRng = rngOutput

End Sub



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,058
Default Range through Inputbox

The problem is that in:

Dim rngInput As Range, rngOutput As Range
and
Set rngInput = Application.InputBox(msg1, Type:=8)


you are treating rngInput as a Range variable, but in:

Set Rng = SH.Range(rngInput)

you are using it as a String variable ! Consider:

Set Rng = SH.rngInput
--
Gary''s Student
gsnu200709


"jasmine" wrote:


Hi,

Why the following code produces an error?

Public Sub aSub()

Const msg1 = "Select input range"
Const msg2 = "Select output cell"

Dim WB As Workbook
Dim SH As Worksheet
Dim Rng As Range
Dim destRng As Range
Dim rngInput As Range, rngOutput As Range

Set rngInput = Application.InputBox(msg1, Type:=8)
Set rngOutput = Application.InputBox(msg2, Type:=8)
If rngInput Is Nothing Then Exit Sub
If rngOutput Is Nothing Then Exit Sub

Set WB = ActiveWorkbook
Set SH = WB.ActiveSheet
Set Rng = SH.Range(rngInput)
Set destRng = SH.Range(rngOutput)

End Sub

While if I change the last rows, for example
Set Rng=SH.Range("B1:C1000")
Set destRng = SH.Range("F2")

it works

I want to use the ranges selected with inputboxes, and not change the code!
Where is the mistake?

Thanks
j.



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
Can't Select Range off sheet from inputbox Jim May Excel Discussion (Misc queries) 4 September 29th 07 08:26 PM
InputBox method with range Emily Edgington Excel Programming 6 September 8th 06 06:20 PM
Select range with InputBox. Is it possible? Isabel Excel Programming 3 March 16th 06 07:02 PM
Selecting a Range using Inputbox Method John Pierce Excel Programming 3 June 4th 05 12:07 PM
Trying to return Range from InputBox Jiana Excel Programming 3 April 28th 04 11:25 PM


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

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"