Solved

Select just one cell using input box

Posted on 2014-03-22
4
282 Views
Last Modified: 2014-03-24
Dear Experts:

I would like to run the following macro

An inputbox should prompt the user to ...

... just select (1) one cell
... the selected cell is to conform to the following cell format '##-###-##-##', the pound sign standing for numbers, e.g. 55-443-22-99
... if one or both of the requirements is false the macro is to say so and to exit
... if both requirements are true the macro is to select the 'second' cell in that column. For example: the user selects 'C7', the macro is to move (offset) this selection to 'C2'

Help is much appreciated. Thank you very much in advance for your valuable help.

Regards, Andreas
0
Comment
Question by:AndreasHermle
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 51

Expert Comment

by:Rgonzo1971
ID: 39947697
Hi,

pls try

Sub Macro()

On Error Resume Next
Set MyRange = Application.InputBox("Please select the cell :", Default:=Range("A1").Address(0, 0), Type:=8)
On Error GoTo 0

If MyRange Is Nothing Then
    MsgBox "You have selected no cell"
    Exit Sub
End If

If MyRange.Count > 1 Then
    MsgBox "You have selected more than one cell"
    Exit Sub
ElseIf MyRange.DisplayFormat.NumberFormat <> "##-###-##-##" Then
    MsgBox "This cell has not a valid format"
    Exit Sub
End If
Cells(2, MyRange.Column).Select
End Sub

Open in new window

Regards
0
 

Author Comment

by:AndreasHermle
ID: 39948480
Hi Rgonzo,
thank you very much for your swift and professional help.

Thank you very much for your great code. It works just fine although line 15 of the code only works properly if the cell that is selected has been formatted as follows before running the macro:
'user-defined' ##-###-##-##

If the cell format is 'just' standard and the number that is selected is e.g. 55-554-33-99 the macro will come up with the msgbox on line 16. For me this does not make sense since you correctly (in my view) used the 'DisplayFormat'.

Any idea why this is so?

Regards, Andreas
0
 
LVL 51

Accepted Solution

by:
Rgonzo1971 earned 500 total points
ID: 39948489
In my first code, I supposed the number you had was for example 123456789 then formatted as 12-345-67-89 but if you have directly entered 12-345-67-89 then

pls try this

Sub Macro()

On Error Resume Next
Set myRange = Application.InputBox("Please select the cell :", Default:=Range("A1").Address(0, 0), Type:=8)
On Error GoTo 0

If myRange Is Nothing Then
    MsgBox "You have selected no cell"
    Exit Sub
End If

If myRange.Count > 1 Then
    MsgBox "You have selected more than one cell"
    Exit Sub
ElseIf myRange.DisplayFormat.NumberFormat <> "##-###-##-##" _
        And Not (myRange.Value Like "##-###-##-##") Then
    MsgBox "This cell has not a valid format"
    Exit Sub
End If
Cells(2, myRange.Column).Select
End Sub

Open in new window

Regards
0
 

Author Closing Comment

by:AndreasHermle
ID: 39951688
Great job, as always, Rgonzo. Thank you very much for your professional support. I really appreciate it.

Regards, Andreas
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question