If you are facing trouble in splitting the text into two or more separate columns based on the specific character then you should read this article.
In this article, we are going to learn how to split the name into 3 cells, using VBA in Microsoft Excel.
Let’s take an example to understand where and how we can split the name.
We have a list of names in the format "FirstName Initial LastName" in column A. We want to split the entire column into three columns, with one containing all of the first names, second contains Initial and the third containing all of the last names.
We will write the VBA code to split the name. Follow below given steps:-
To split the name into 3 cells, we will use VBA. See the below procedure and code, and you will understand it easily:-
Public Sub SplitName() X = Cells(Rows.Count, 1).End(xlUp).Row For A = 1 To X B = InStr(Cells(A, 1), " ") C = InStrRev(Cells(A, 1), " ") Cells(A, 2) = Left(Cells(A, 1), B) Cells(A, 3) = Mid(Cells(A, 1), B, C - B) Cells(A, 4) = Right(Cells(A, 1), Len(Cells(A, 1)) - C) Next A End Sub
Code Explanation:- First we have given the macro name, then we used x to define the data range, and then we used A, B and C to define how values should be picked from the cells.
Conclusion: In this way, we can split the names into 3 columns, using VBA in Microsoft Excel.
If you liked our blogs, share it with your friends on Facebook. And also you can follow us on Twitter and Facebook.
We would love to hear from you, do let us know how we can improve, complement or innovate our work and make it better for you. Write us at info@exceltip.com
The applications/code on this site are distributed as is and without warranties or liability. In no event shall the owner of the copyrights, or the authors of the applications/code be liable for any loss of profit, any problems or any damage resulting from the use or evaluation of the applications/code.
The SplitName macro is quick and dirty but is good enough to do the job in most situations. The addition of one statement would greatly enhance its usability.
Adding the statement:
Cells(A, 1) = Trim(Cells(A, 1))
before the statement that sets the value of B will allow this macro to work properly when trailing spaces are present in column A.