Solved

Select just one cell using input box

Posted on 2014-03-22
4
272 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 50

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 50

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Approximate matching with VLOOKUP and MATCH seems to me to be a greatly under-used technique, and one which is vital for getting good performance out of large lookups. Until recently I would always have advised using an exact match for simplicity an…
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 …
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

831 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