Solved

Select just one cell using input box

Posted on 2014-03-22
4
293 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 52

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 52

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

Enroll in July's Course of the Month

July's Course of the Month is now available! Enroll to learn HTML5 and prepare for certification. It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

617 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