The Split() function

Gilbfernandes

New Member
Joined
Jan 26, 2016
Messages
20
Good morning everyone,

I have the following code but I am having problem with it:

New_Name = Split(Sheets("Report").Range("D16"))

FirstName = New_Name(0)
LastName = New_Name(1)

If LastName = "" Then
MsgBox "Please enter Last Name"
GoTo endsub
End If

But if Last Name is not entered on D16 , I get Run-Time Error 9 on LastName=New_Name(1). I don't even know if the IF will work on this case.

Can someone help me?

Thank you in advance.

Gil
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
How about
VBA Code:
Sub Gilbfernandes()
   Dim NewName As Variant
   NewName = Split(Sheets("Report").Range("D16"))
   If UBound(NewName) = 0 Then
      MsgBox "Please enter Last Name"
      Exit Sub
   End If
End Sub
 
Upvote 0
First of all, the statement as you have typed it into the thread would likely error for lack of arguments.
Maybe something like this would work
Code:
New_Name = Split(Sheets("Report").Range("D16").Value, ",")
If UBound(New_Name) = 0 Then
    FistName = New_Name(LBound(New_Name))
    MsgBox "Please Enter Last Name", vbExclamation, "MISSING NAME"
    Exit Sub
Else
    FirstName = New_Name(LBound(New_Name))
    LastName = New_Name(UBound(New_Name))
End If
 
Upvote 0
Or relying on trapping the error ...
VBA Code:
    Dim FirstName As String, LastName As String, New_Name As Variant
    On Error Resume Next
    New_Name = Split(Sheets("Report").Range("D16"))
    FirstName = New_Name(0)
    LastName = New_Name(1)
    On Error GoTo 0
    If LastName = "" Then MsgBox "Enter last name", , ""
    '..etc
 
Upvote 0
Hi.
Just thinking through it, does any of the good option above trap the error if someone enters "First " in which Split recognizes that the trailing space is an entry?

Ah. Didn't see your post Yongle. Nice
 
Upvote 0
trap the error if someone enters "First " in which Split recognizes that the trailing space is an entry?

To get rid of any extra spaces anywhere in the string ...
VBA Code:
New_Name = Split(WorksheetFunction.Trim(Sheets("Report").Range("D16")))
 
Upvote 0
How about
VBA Code:
Sub Gilbfernandes()
   Dim NewName As Variant
   NewName = Split(Sheets("Report").Range("D16"))
   If UBound(NewName) = 0 Then
      MsgBox "Please enter Last Name"
      Exit Sub
   End If
End Sub
Thank you Fluff, It worked. I really appreciate your help.
 
Upvote 0
Glad we could help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,637
Latest member
Ezio2866

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