Range Variable (From Cell Value)

RudRud

Active Member
Joined
Feb 2, 2023
Messages
275
Office Version
  1. 365
  2. 2021
Platform
  1. Windows
  2. MacOS
Hi Gurus,

If vbNo , inpboxa & b will become as a range variable, therefore when i put inpboxa.select it will be selected

However if vbYes, inpboxa cant be select. Any quick thoughts for selecting an inpboxa when vbyes? Thanks

VBA Code:
Sub test()

answer = MsgBox("Last Range (Yes), New Range(No) ", vbYesNo)
If answer = vbYes Then
inpboxa = Range("b2").Value
inpboxb = Range("b3").Value

ElseIf answer = vbNo Then
    Set inpboxa = Application.InputBox(Prompt:="Select a range", Type:=8)
   Set inpboxb = Application.InputBox(Prompt:="Select a range", Type:=8)
   Range("b2").Value = inpboxa.Address
   Range("b3").Value = inpboxb.Address
End If

inpboxa.Select
End Sub
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
In your code, inpboxa and inpboxb are NOT range variables! You have set them equal to the values in the cells, not to the cells themselves.
This is another great example why you should always declare all your variables before using them. It will help prevent you from making these sort of mistakes.
You also need to use "Set" when setting object variables, like range variables.

Try this:
VBA Code:
Dim inpboxa as Range
Dim inpboxb as Range
Set inpboxa = Range("b2")
Set inpboxb = Range("b3")
 
Upvote 0
In your code, inpboxa and inpboxb are NOT range variables! You have set them equal to the values in the cells, not to the cells themselves.
This is another great example why you should always declare all your variables before using them. It will help prevent you from making these sort of mistakes.
You also need to use "Set" when setting object variables, like range variables.

Try this:
VBA Code:
Dim inpboxa as Range
Dim inpboxb as Range
Set inpboxa = Range("b2")
Set inpboxb = Range("b3")

Hi @Joe4, Well Noted, Thanks for your kind information.

But when vbyes, inpboxa.select not working.

1685533559596.png
 
Upvote 0
Your code works just fine for me.
Please walk us through an example of how it is not working.
What are you entering in for each prompt, and what does it do?
 
Upvote 0
Your code works just fine for me.
Please walk us through an example of how it is not working.
What are you entering in for each prompt, and what does it do?

Apologies,

What i mean is If Vbyes, Then inpboxa become a range from b2 value (which is a2 a10)

When VBYes inpboxa.select become A2:A10 select.
 
Upvote 0
In VByes you must set the range contained in the cell.
Try this:

Rich (BB code):
Sub test()
  Dim inpboxa As Range, inpboxb As Range
  Dim answer As VbMsgBoxResult
 
  answer = MsgBox("Last Range (Yes), New Range(No) ", vbYesNo)
  If answer = vbYes Then
    Set inpboxa = Range(Range("b2").Value)
    Set inpboxb = Range(Range("b3").Value)
 
  ElseIf answer = vbNo Then
    On Error Resume Next  'Use in case of pressing cancel.
    Set inpboxa = Application.InputBox(Prompt:="Select a range", Type:=8)
    Set inpboxb = Application.InputBox(Prompt:="Select a range", Type:=8)
    If inpboxa Is Nothing Then Exit Sub
    If inpboxb Is Nothing Then Exit Sub
    On Error GoTo 0
    Range("b2").Value = inpboxa.Address
    Range("b3").Value = inpboxb.Address
  End If

  inpboxa.Select
  inpboxb.Select
End Sub
 
Upvote 1
Solution
VBA Code:
  inpboxa.Select
  inpboxb.Select
Not really sure what the point of that would be.
The second select would simply overwrite the first, would it not?
 
Upvote 0
Thanks all Gurus for the assists, truly appreciated.

I found out Dante's code and this's the key part that i've been debugging a while (solution)

Set inpboxa = Range(Range("b2").Value)

--


Actually Im working with checking different values from ranges

If vbNo then user will require to select a new range, if vbyes then based in lastrange.

Book2
A
2asd
3asd
Sheet2


Book2
B
2Rud
3Asd
Sheet3


then the ranges will become two arrays , through array it will compare one by one based on two selected ranges.
 
Upvote 0
Thanks all Gurus for the assists, truly appreciated.

I found out Dante's code and this's the key part that i've been debugging a while (solution)

Set inpboxa = Range(Range("b2").Value)

--


Actually Im working with checking different values from ranges

If vbNo then user will require to select a new range, if vbyes then based in lastrange.

Book2
A
2asd
3asd
Sheet2


Book2
B
2Rud
3Asd
Sheet3


then the ranges will become two arrays , through array it will compare one by one based on two selected ranges.
I am glad he was able to guess what it was that you wanted to do. It wasn't quite clear from your original post. You posted the code, but did not explain what you were ultimately trying to accomplish with it.
 
Upvote 0
I am glad he was able to guess what it was that you wanted to do. It wasn't quite clear from your original post. You posted the code, but did not explain what you were ultimately trying to accomplish with it.

I will take note for that and i'll try to be more clearer in the future. Thanks for the fast response anyway :)
 
Upvote 0

Forum statistics

Threads
1,215,467
Messages
6,124,984
Members
449,201
Latest member
Lunzwe73

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