Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Select just one cell using input box

Posted on 2014-03-22
4
Medium Priority
?
335 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
  • 2
  • 2
4 Comments
 
LVL 53

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 53

Accepted Solution

by:
Rgonzo1971 earned 2000 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

Technology Partners: 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

After seeing numerous questions for Dynamic Data Validation I notice that most have used Visual Basic to solve the problem. This suggestion is purely formula based and can be used in multiple rows.
If you need to forecast numbers -- typically for finance -- the Windows and Mac versions of Excel 2016 have a basket of tools to get the job done.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa‚Ķ

916 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