Range dependent on InputBox

288enzo

Well-known Member
Joined
Feb 8, 2009
Messages
721
Office Version
  1. 2016
Platform
  1. Windows
Is it possible to set a cell range based on an inputbox?

As an example I have an inputbox, and based on the users response I would like to set a particular range that can be used throughout my code.

In my mind it would be something like
Select Case strArea
Case 80
x="E12"
Case 82
x="E13"
Case 84
x="E17"

From there I can use Range(x) and based on the above Range("E12") would be used if 80 was entered.

Am I out of my mind?

Thank you
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Yes you could do that or maybe directly from the inputbox eg:

VBA Code:
Dim rng As Range
Set rng = Application.InputBox("Enter cell reference", , , , , , , 8)

or maybe this:

VBA Code:
Select Case strArea
    Case 80
        Set x = Range("E12")
    Case 82
        Set x = Range("E13")
    Case 84
        Set x = Range("E17")
End Select

or like you said:

VBA Code:
Select Case strArea
    Case 80
        x = "E12"
    Case 82
        x = "E13"
    Case 84
        x = "E17"
End Select

Set rng = Range(x)
 
Upvote 0
Yes you could do that or maybe directly from the inputbox eg:

VBA Code:
Dim rng As Range
Set rng = Application.InputBox("Enter cell reference", , , , , , , 8)

or maybe this:

VBA Code:
Select Case strArea
    Case 80
        Set x = Range("E12")
    Case 82
        Set x = Range("E13")
    Case 84
        Set x = Range("E17")
End Select

or like you said:

VBA Code:
Select Case strArea
    Case 80
        x = "E12"
    Case 82
        x = "E13"
    Case 84
        x = "E17"
End Select

Set rng = Range(x)
Silly me, I thought it would be that easy.
I'm getting an error "Type mismatch" with a test.
VBA Code:
Select Case strArea
    Case 87
        x = "A1"
End Select
Range(x) = "Hello, this worked"
 
Upvote 0
You will need to give me exactly what you are running when you get the error including the Dim statements. The below will run and enter "hello" in the appropriate cell of the activesheet if you enter one of the numbers present in the case select.

VBA Code:
Sub hello()

Dim strArea As String, x As String

strArea = InputBox("Enter Number")

Select Case strArea
    Case 80
        x = "E12"
    Case 82
        x = "E13"
    Case 84
        x = "E17"
End Select

If Len x > 0 Then Range(x) = "hello"
    
End Sub
 
Upvote 0
Solution
You will need to give me exactly what you are running when you get the error including the Dim statements. The below will run and enter "hello" in the appropriate cell of the activesheet if you enter one of the numbers present in the case select.

VBA Code:
Sub hello()

Dim strArea As String, x As String

strArea = InputBox("Enter Number")

Select Case strArea
    Case 80
        x = "E12"
    Case 82
        x = "E13"
    Case 84
        x = "E17"
End Select

If Len x > 0 Then Range(x) = "hello"
   
End Sub
Thank you, it was "x As String" I was missing.

Is "If Len X > 0 Then" used in the event they don't enter a number?
I ran the code without it and it worked perfectly.

Thank you again
 
Upvote 0
Its in case you enter something that isnt in your list of possibilites. It prevents error. Try putting 100 in the inputbox without it.
 
Upvote 0
You will need to give me exactly what you are running when you get the error including the Dim statements. The below will run and enter "hello" in the appropriate cell of the activesheet if you enter one of the numbers present in the case select.

VBA Code:
Sub hello()

Dim strArea As String, x As String

strArea = InputBox("Enter Number")

Select Case strArea
    Case 80
        x = "E12"
    Case 82
        x = "E13"
    Case 84
        x = "E17"
End Select

If Len x > 0 Then Range(x) = "hello"
   
End Sub
How would I go about using it with this if I wanted C13 in the formula to change depending on what was entered for x?

VBA Code:
.Formula = "=""Area "" & LEFT(C13,3)"

Would I do it without the quotes?
VBA Code:
x = E17
 
Upvote 0
Its in case you enter something that isnt in your list of possibilites. It prevents error. Try putting 100 in the inputbox without it.
I don't have a list of what can be entered.

This is my input box
VBA Code:
strArea = InputBox("Area? (80,82,83,84,87)")
I'm only giving a suggestion as to what should be entered
 
Upvote 0

Forum statistics

Threads
1,214,875
Messages
6,122,037
Members
449,062
Latest member
mike575

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top