using application.inputbox

jimrward

Well-known Member
Joined
Feb 24, 2003
Messages
1,897
Office Version
  1. 2021
  2. 2019
  3. 2016
  4. 2013
  5. 2011
  6. 2010
  7. 2007
  8. 2003 or older
Platform
  1. Windows
in simple terms I want to achieve the following using INPUTBOX

workbook has 2 sheets, sheet1 and sheet2

I wish to select a cell in sheet2, run a macro to prompt for a source cell, use the address of the source cell to create a simple reference

for example I select sheet2!A6 and I wish to set the formula to =sheet1!A3, where Sheet1!A3 is prompted for using INPUTBOX(...type=8)

I keep getting various object errors, this is the current state of play

Code:
Sub TestInputBox2()
    Dim myRange As Range
    Set myRange = Application.InputBox(Prompt:= _
        "Please Select a Range", _
        Title:="InputBox Method", Type:=8)
 
    If myRange Is Nothing Then
        ' Range is blank
    Else
        ActiveCell.Formula = "=" & myRange
    End If
End Sub
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Shouldn't that be:

Code:
ActiveCell.Formula = "=" & myRange.Address

... but, shouldn't you also check that only a single cell has been selected as the range?
 
Upvote 0
Try:
Code:
Sub TestInputBox2()
    Dim myRange As Range
 
    On Error Resume Next    'Skips the error if myRange is nothing
 
    Set myRange = Application.InputBox(Prompt:= _
        "Please Select a Range", _
        Title:="InputBox Method", Type:=8)
 
    If myRange Is Nothing Then
        ' Range is blank
    Else
        ActiveCell.Formula = "=" & myRange.Address
        'ActiveCell.Formula = "=" & Replace(myRange.Address, "$", "")
    End If
End Sub
And if you want the sheet name to be included use:
Code:
ActiveCell.Formula = "='" & myRange.Parent.Name & "'!" & myRange.Address
 
Last edited:
Upvote 0
Sub TestInputBox2()
Dim myRange As Range
Set myRange = Application.InputBox(Prompt:= _
"Please Select a Range", _
Title:="InputBox Method", Type:=8)

If myRange Is Nothing Then
' Range is blank
Else
ActiveCell.Formula = "='" & myRange.Worksheet.Name & "'!" & myRange.Address
End If
End Sub


Works for me
 
Upvote 0
thanks all

it was the myrange.parent.name that I was missing there was me thinking address meant the complete address, ho hum.

I had tried myrange.address, but in debug mode it was showing the contents of the cell and when patched into the recipient cell was showing =$A$4 as the formula and 0 as the value as it was taking it from the current sheet

all working fine now ta
 
Upvote 0

Forum statistics

Threads
1,224,503
Messages
6,179,134
Members
452,890
Latest member
Nikhil Ramesh

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