You are here:

Excel/Macro to seperate 3rd column data

Advertisement


Question
Hello Tom,
Hope you are well.

I have a data as below (Input data)


ORIGIN   DEST   RBD   DISCOUNT A   DISCOUNT B

MY   GB   F      7%   0%
MY   LK   A      8%   BLANK
MY   PL   P,Z,I      5%   BLANK
PK   UM   J      10%   7%
PK   UM   C,D      8%   BLANK


In Column "C" i have booking code (alpha bate)
I just want to know, is there any macro which will put the RBD in below row if they are placed or separated by ","

e.g in above case my output data should be.

ORIGIN   DEST   RBD   DISCOUNT A   DISCOUNT B

MY   GB   F      7%   0%
MY   LK   A      8%   BLANK
MY   PL   P      5%   BLANK
MY   PL   Z      5%   BLANK
MY   PL   I      5%   BLANK
PK   UM   J      10%   7%
PK   UM   C      8%   BLANK
PK   UM   D      8%   BLANK

means, all data should come as it is only the RBD data should be separated and placed in below row.

Hope i explained correctly.

Thanks
Rakesh

Answer
Rakesh,

If the word ORIGIN is in A1 and MY GB F 7% 0%  is in row 2, then this code produced the output you show from the orginal data you show.

Sub abc()
Dim rw As Long, i As Long, v As Variant, cnt As Long
Dim j As Long
rw = Cells(Rows.Count, "C").End(xlUp).Row
For i = rw To 2 Step -1
 If InStr(1, Cells(i, "C"), ",") Then
   v = Split(Cells(i, "C"), ",")
   cnt = UBound(v) - LBound(v) + 1
   Cells(i + 1, "C").Resize(cnt - 1).EntireRow.Insert
   Cells(i, "C").EntireRow.Copy Cells(i, "C").Resize(cnt).EntireRow
   For j = LBound(v) To UBound(v)
     Cells(i + j, "C").Value = v(j)
   Next
 End If
Next

End Sub

test this on a copy of your data until you are sure it does what you want.

My output  from the running the macro (word blank typed in by me)

ORIGIN   DEST   RBD   DISCOUNT A   DISCOUNT B
MY   GB   F   7%   0%
MY   LK   A   8%   blank
MY   PL   P   5%   blank
MY   PL   Z   5%   blank
MY   PL   I   5%   blank
PK   UM   J   10%   7%
PK   UM   C   8%   blank
PK   UM   D   8%   blank


--
Regards,
Tom Ogilvy

About Excel
This topic answers questions related to Microsoft Excel spreadsheet (or workbook) stand-alone or Mircrosoft Office Excel including Excel 2003, Excel 2007, Office 2000, and Office XP. You can get Excel help on Excel formulas(or functions), Excell macros, charting in Excel, advanced features, and the general use of Excel. This does not provide a general Excel tutorial nor the basics of using a spreadsheet. It provides specific answers to using Microsoft Excel only. If you do not see your Excel question answered in this area then please ask an Excel question here

Excel

All Answers


Answers by Expert:


Ask Experts

Volunteer


Tom Ogilvy

Expertise

Selected as an Excel MVP by Microsoft since 1999. Answering Excel questions in Allexperts since its inception in 2001. Able to answer questions on almost all aspects of Excel's internal capabilities. If seeking a VBA solution, please specify that in your question itself so I give you the answer you want. [Excel has weak protection - if you are distributing an application, I don't answer questions on how to protect your project from your users.]

Experience

Extensive experience.

Education/Credentials
Master of Science (MS) degree Operations Research (ORSA)

Awards and Honors
Microsoft MVP in Excel.

©2016 About.com. All rights reserved.