Help with inputbox

ColdGeorge

Active Member
Joined
Aug 21, 2012
Messages
412
Office Version
  1. 2016
Platform
  1. Windows
Hi all

I'd appreciate your assistance in order to prevent errors with an input box, this is the code

VBA Code:
Dim selection As Range
Dim rng As Range

Set selection = Application.InputBox("First, select a range", "Title Here", Type:=8)

Set rng = Application.InputBox("Now, select a cell", "Title Here", Type:=8)

selection.Cut rng

Thanks in advance
ColdGeorge
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Why select a range...then select a cell ??
Why not simply
VBA Code:
Set rng = Application.InputBox("First, select a range", "Title Here", Type:=8
rng.cut
 
Upvote 0
Try.
VBA Code:
Dim selection As Range
Dim rng As Range

Set selection = Application.InputBox("First, select a range", "Title Here", Type:=8)
Line1:
Set rng = Application.InputBox("Now, select a cell", "Title Here", Type:=8)
If rng.Cells.Count > 1 Then
MsgBox "Select single cell only"
GoTo Line1
End If

selection.Cut rng
 
Upvote 0
Hi all

VBA Code:
Dim selection As Range
Dim rng As Range

'Selecting range to move
Set selection = Application.InputBox("First, select a range", "Title Here", Type:=8)

'Selecting cell to paste
Set rng = Application.InputBox("Now, select a cell", "Title Here", Type:=8)

'Pasting
selection.Cut rng

I hope this make sense, I need help to prevent error when Input box is empty, Cancel or Esc key is hit, thanks.

ColdGeorge
 
Upvote 0
@ColdGeorge
see if that work for you.
VBA Code:
Sub n()
Dim selection As Range
Dim rng As Range
Application.DisplayAlerts = False
'Selecting range to move
On Error GoTo Canceled
Set selection = Application.InputBox("First, select a range", "Title Here", Type:=8)
'Selecting cell to paste
Set rng = Application.InputBox("Now, select a cell", "Title Here", Type:=8)
'Pasting
selection.Cut rng
1 Canceled:
2 End Sub
 
Upvote 0
Solution
I wouldn't use selection as a variable name, it's a VBA keyword, and that can cause problems.
 
Upvote 0
Hi

abdelfattah, thanks for yor help.
Scott Huish, good point, I'll consider it, thanks.

ColdGeorge
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,119
Members
451,398
Latest member
rjsteward

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