Separating first and last names - vba

lordriel

Board Regular
Joined
Nov 1, 2005
Messages
68
Office Version
  1. 365
Platform
  1. Windows
I've seen several methods for separating names, or text, from one cell to two or more. However, none of them give a vba answer.

How would I code it, were it to be done when hitting a command button?
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
As far as I'm aware you cannot use VBA in OpenOffice.
 
Upvote 0
Hi there,

I am organizing data into a number of different sheets using VBA code, @NateO I am experimenting with using your array code in my macro, it seems to work fine but I need to make a few modifications. My name column is in column B not column A. Also, I only care about extracting the first name, but I would like to put it in column BP (the last column in the worksheet). I would like the heading for that column to say "First Name." Is it possible to modify your code to do that? I'm trying but haven't had any luck yet, I'm new to VBA so this is well beyond what I know. Thank you!!

VBA Code:
Dim varArr As Variant
Dim i As Long, strArr() As String
Application.ScreenUpdating = False
With Worksheets(3)
    Let varArr = .Range(.Cells(1, 1), _
        .Cells(.Rows.Count, 1).End(xlUp)).Value
    If IsArray(varArr) Then
        For i = LBound(varArr, 1) To UBound(varArr, 1)
            Let strArr() = Split(varArr(i, 1))
            Let .Cells(i, 2).Resize( _
                , UBound(strArr) + 1).Value = strArr
            Erase strArr
        Next
    Else: .Cells(1, 2).Resize(, 2).Value = Split(varArr)
    End If
End With
Application.ScreenUpdating = True
 
Upvote 0
I am experimenting with using your array code
Unfortunately NateO is no longer with us but see if this helps. Depends a bit just what you data might be like but give this a try in a copy of your workbook.
If not what you want post back with more details and samples of original data and expected results.

VBA Code:
Sub First_Name()
  Range("BP1").Value = "First Name"
  Range("B2", Range("B" & Rows.Count).End(xlUp)).TextToColumns Destination:=Range("BP2"), DataType:=xlDelimited, Space:=True, Other:=False, FieldInfo:=Array(Array(1, 1), Array(2, 9), Array(3, 9))
End Sub
 
Upvote 0
Hi Peter_SSs,

Thanks for your response and sorry for the delay! So I am getting a run time error of 1004 with the code. Although I modified it to declare the sheet because there are multiple worksheets in the workbook. Maybe you can't do that with an array? Also, I think its an array but I'm not sure :unsure: Anyway this is what I did... The first line works and the second line does not.

VBA Code:
 Sheets("Cpl-Child Info").Range("BP1").Value = "First Name"
  Sheets("Cpl-Child Info").Range("B2", Range("B" & Rows.Count).End(xlUp)).TextToColumns Destination:=Range("BP2"), DataType:=xlDelimited, Space:=True, Other:=False, FieldInfo:=Array(Array(1, 1), Array(2, 9), Array(3, 9))

As a side note I tried to step into the code to see if I could get anything more specific for the error. I have the macro tied to a button on the ribbon and when you click the button a message box pops up and asks if you would like to organize the data. If I step into the macro and proceed by pushing F8 the message box comes up and I can't proceed by pushing F8, I have to click yes, at which point the macro runs and then I can't step through it. Is there a way around it so that I can actually step through the macro using F8?

Thanks!
Gingerbreadgrl
 
Upvote 0
Do you get the same error with the code below?
VBA Code:
Sheets("Cpl-Child Info").Range("BP1").Value = "First Name"
  Sheets("Cpl-Child Info").Range("B2", Sheets("Cpl-Child Info").Range("B" & Rows.Count).End(xlUp)).TextToColumns Destination:=Range("BP2"), DataType:=xlDelimited, Space:=True, Other:=False, FieldInfo:=Array(Array(1, 1), Array(2, 9), Array(3, 9))
 
Upvote 0

Forum statistics

Threads
1,225,401
Messages
6,184,760
Members
453,254
Latest member
topeb

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