InputBox (Easy but I cant seem to figure it out)

BerryBashful1

Board Regular
Joined
Apr 23, 2004
Messages
52
I need to prompt for the name (first and last) in one inputbox but the first name goes in say a1 and the last name goes in a2. and I need to have the first and last name with a single space in between. Can anyone help?
Thanks :banghead:
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Welcome to the Board!

Two Input Boxes will probably be easier:

<font face=Courier New><SPAN style="color:#00007F">Sub</SPAN> NameMe()
    <SPAN style="color:#00007F">Dim</SPAN> First <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>
    <SPAN style="color:#00007F">Dim</SPAN> Last <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>
    
    First = InputBox("Please enter your first name", "First Name")
    Last = InputBox("Please enter your last name", "Last Name")
    
    Range("A1") = First
    Range("A2") = Last
    Range("A3") = First & " - " & Last
    
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN>
</FONT>

Hope that helps,

Smitty

EDIT: Typo
 
Upvote 0
I had to incorporate a formula, but it seems to work with 1 inputbox:

Code:
Sub test()

Dim FullName As String

FullName = InputBox("Please enter first and last name:")
Range("A1").Value = FullName
Range("B1").FormulaR1C1 = "=LEFT(RC[-1], FIND("" "",RC[-1])-1)"
    Range("C1").FormulaR1C1 = "=RIGHT(RC[-2],FIND("" "",RC[-2])-1)"
    Range("B1:C1").Copy
    Range("A1").PasteSpecial Paste:=xlPasteValues
    Range("C1").ClearContents

End Sub
 
Upvote 0
Pennysaver,

That seems to be around the right track but I need something like
strFull = strFirst & " " & strLast
strFull = Inputbox(prompt:="please enter patients first and last name", title:= "name information")

and then display and error message if there is no space between first and last name.
and then first name has to go into say b8 and last name into A8

I hope this is better explaining.

I used
intposition = instr(strfull, " ")
strfirst=Left(strfull,intposition -1)
strlast=Mid(strfull,intposition +1)
strfull=Inputbox(prompt:="Please enter full name", title:="Name Information")
and
I even got it to display in the right cells
BUT NOW :banghead:
when I put in the error msgbox or type in the name without a space it dont work again.
so now I just need an error message that will work or something to start all over with. Ya know?
Please help :!:
 
Upvote 0
What about this?

Sub fullname()
Dim fullname, firstname, lastname As String
getname:
fullname = InputBox("Please Enter Name", "Name information")
intposition = InStr(fullname, " ")
If intposition = 0 Then
MsgBox "Invalid Name", vbExclamation
GoTo getname
End If
firstname = Left(fullname, intposition - 1)
lastname = Mid(fullname, intposition + 1)
Range("A8") = lastname
Range("B8") = firstname
End Sub
 
Upvote 0
Actually, in Pennysaver's post, you don't need to worry about the spaces, because it's using 2 inputboxes. Were you perhaps referring to mine?

Try changing Pennysaver's code to:

Code:
Sub NameMe()
    Dim First As String
    Dim Last As String
    
    First = InputBox("Please enter your first name", "First Name")
    Last = InputBox("Please enter your last name", "Last Name")
    
    Range("A1") = First
    Range("B1") = Last
    
End Sub
 
Upvote 0
Kristy,

I could use that with pennysavers code but I can only use one inputbox.
Any other suggestions?

ThanKs for replying! :wink:
 
Upvote 0
hotpepper,

i actually tried to put
intposition = 0
but I still had the same error.....
Invalid call, procedure, or arguement
that refers to
strFirst = Left(strFull, intPosition - 1)
THIS IS WHAT I HAVE SO FAR!

strFull = InputBox(prompt:="Please Enter the Patient's Name", Title:="Name Information")
intPosition = InStr(strFull, " ")
strFirst = Left(strFull, intPosition - 1)
strLast = Mid(strFull, intPosition + 1)
If strFull = strFirst & "" & strLast Then
intButton = MsgBox(prompt:="Name Was Entered Incorrectly!", Buttons:=vbRetryCancel + vbExclamation, _
Title:="ERROR!!!")
Else
intButton = MsgBox(prompt:="Name Was Entered.", Buttons:=vbOKOnly, Title:="YIPPY!!!")
End If
Select Case intButton
Case vbRetry
strFull = InputBox(prompt:="Enter Patient's First and Last Name", _
Title:="Name Information")
Case vbCancel
MsgBox prompt:="No Name Was Entered!"
End Select

shtPatients.Range("A8").Value = strLast
shtPatients.Range("B8").Value = strFirst

BUT :banghead:
like I said if I input say "FULL NAME" it works great! but if I input
"FULLNAME" it goes to debugging and says
Invalid call, procedure, or arguement
that refers to
strFirst = Left(strFull, intPosition - 1)

I have to prompt for a which is assumed to be entered as first name followed by last name with a single space in between.
if there is NO SPACE in what was entered, then I have to issue an error message and reprompt.
then if the cancel button is clicked then I have to exit the procedure without entering any data into the worksheet.

Is this maybe clearer?
Thanks!
Please help! :banghead:
I have been working on this for days! :nervous: :crash:
 
Upvote 0
HOTPEPPER! I tried your code again and it does work...thank you!

Was there maybe a simpler or different way to do this without the
getname
part?

Thanks

ps remember I am just starting out so Im on only basic vba from what I described before

Thanks again :pray:
 
Upvote 0

Forum statistics

Threads
1,224,869
Messages
6,181,485
Members
453,046
Latest member
Excelvbaexpert

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