Solved

EXCEL VBA To combine cells into one seperated by

Posted on 2016-08-03
10
46 Views
Last Modified: 2016-08-08
Hello,
Can you please help with an Excel  macro.
Column "A",
if the cell is called Tracking ID
Then, I need to combine all cells after the  Tracking ID cell into the first next row cell.
until the cell in Column "C" has a name which starts with QA:
and clear cells.

Please see sample attached sample, before, after.

Thanks,
0
Comment
Question by:W.E.B
  • 5
  • 5
10 Comments
 
LVL 43

Expert Comment

by:Saqib Husain, Syed
ID: 41741201
Sample?
0
 

Author Comment

by:W.E.B
ID: 41741210
Ooooops.
Sample.xlsx
0
 
LVL 43

Expert Comment

by:Saqib Husain, Syed
ID: 41744152
Sub qa2qaMerge()
    Dim tid As Range, nxtcel As Range
    Set tid = Cells.Find("Tracking ID", Cells.SpecialCells(xlCellTypeLastCell), , xlWhole).Offset(1)
    Set nxtcel = Range("A1")
    Do While tid.Row > nxtcel.Row
        tid.Value = "TRID: " & tid.Text
        tid.HorizontalAlignment = xlGeneral
        Set nxtcel = Cells.Find("*", tid)
        Do While nxtcel.Row >= tid.Row
            If Left(nxtcel, 3) = "QA:" Then Exit Do
                tid.Value = tid.Text & ", " & nxtcel.Text
                nxtcel.ClearContents
            Set nxtcel = Cells.Find("*", nxtcel)
        Loop
        Set tid = Cells.Find("Tracking ID", nxtcel, , xlWhole).Offset(1)
    Loop
End Sub
0
 

Author Comment

by:W.E.B
ID: 41744343
Hi Saqib,
Appreciate your help,
I'm trying the code on about 1000 Rows,
it just keeps looping and doesn't stop.

thanks
0
 
LVL 43

Expert Comment

by:Saqib Husain, Syed
ID: 41744351
Can you share the file?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:W.E.B
ID: 41744378
Please see attached sample
Sample.xlsx
0
 
LVL 43

Expert Comment

by:Saqib Husain, Syed
ID: 41744519
This file is very much different from the previous sample.

The infinite loop is because there is no QA: after the last Tracking ID.

Do you want to do it this way?
0
 

Author Comment

by:W.E.B
ID: 41744539
yes, please
thank you very much
0
 
LVL 43

Accepted Solution

by:
Saqib Husain, Syed earned 500 total points
ID: 41747462
Sub qa2qaMerge()
    Dim tid As Range, nxtcel As Range, qacel As Range
    Set tid = Cells.Find("Tracking ID", Cells.SpecialCells(xlCellTypeLastCell), , xlWhole).Offset(1)
    Set nxtcel = Range("A1")
    Set qacel = nxtcel
    Do While tid.Row > nxtcel.Row And tid.Row > qacel.Row
        tid.Select
        tid.Value = "TRID: " & tid.Text
        tid.HorizontalAlignment = xlGeneral
        Set qacel = Range(tid, Cells.SpecialCells(xlCellTypeLastCell)).Find("QA:", tid, , xlPart)
        Set nxtcel = Cells.Find("*", tid)
        nxtcel.Select
        Do
            If nxtcel.Row < tid.Row Then Exit Do
            If Not qacel Is Nothing Then
                If nxtcel.Row >= qacel.Row Then Exit Do
            End If
            If Left(nxtcel, 3) = "QA:" Then Exit Do
                tid.Value = tid.Text & ", " & nxtcel.Text
                nxtcel.ClearContents
            Set nxtcel = Cells.Find("*", nxtcel)
            nxtcel.Select
        Loop
        If qacel Is Nothing Then Exit Do
        Set tid = Cells.Find("Tracking ID", qacel, , xlWhole).Offset(1)
    Loop
End Sub
0
 

Author Closing Comment

by:W.E.B
ID: 41747994
Awesome Saqib.
Thank you very VERY much.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now