Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Excel / New Users / February 2008

Tip: Looking for answers? Try searching our database.

Concatenation within a macro

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Risky Dave - 22 Feb 2008 13:30 GMT
Hi,

I have a large data set that is automatically formatted by a macro. As part
of this macro I need to concatenate sets of three cells into one and format
the output.
Eg. I need to convert:
        A             B            C
1    1Data1    1Data2    1Data3
2    2Data1    2Data2    2Data3
3    3Data1    3Data2    3Data3

To:
        A
1   1Data1
    1Data2
    1Data3
2   2Data1
    2Data2
    2Data3
3   3Data1
    3Data2
    3Data3

The actual output cell can be pretty much anywhere - I can adjust this bit
to fit in with the rest of the formatting.

I can do this by setting up a separate sheet with loads of formulae, but as
there are several hundreds of lines involved (I have no way of knowing
exactly how many), each with about 100 cells, this soon makes the file size
excessively large (especially as there's a load of other stuff going on in
the overall workbook).

Can anyone supply a piece of code that I can drop into the rest of the macro
and will produce the output above?

Although I don't know how many times this needs to be carried out (because I
don't know the exact number of lines with data in them), I can set a high
(eg. 500 or 1000) limit for the number of lines to be formatted - it doesn't
matter if the macro attempts to format cells with no data in them.

Hope this makes sense :-)

If it makes any difference, this is in Office 2003.

TIA Dave
Gary''s Student - 22 Feb 2008 14:14 GMT
Sub put_um()
Set r = Range("D10")
For j = 1 To 3
   v = ""
   For i = 1 To 3
       v = v & Cells(i, j).Value & Chr(10)
   Next
   r.Offset(j, 0).Value = v
Next
End Sub

will take your data and put the results in D11, D12, and D13.
Signature

Gary''s Student - gsnu200770

> Hi,
>
[quoted text clipped - 41 lines]
>
> TIA Dave
Risky Dave - 22 Feb 2008 15:27 GMT
Hi,

Thanks for the quick response.

This is nearly what I am after, but is giving me:

1Data1    
2Data1  
3Data1  

1Data2
2Data2
3Data2

1Data3
2Data3
2Data3

ie. the columns concatenated into each cell, whereas I need the rows
concatenated vertically into each cell.

Thanks.

Dave

> Sub put_um()
> Set r = Range("D10")
[quoted text clipped - 54 lines]
> >
> > TIA Dave
Gary''s Student - 22 Feb 2008 21:20 GMT
Use this version instead:

Sub put_um()
Set r = Range("D10")
For i = 1 To 3
   v = ""
   For j = 1 To 3
       v = v & Cells(i, j).Value & Chr(10)
   Next
   r.Offset(i, 0).Value = v
Next
End Sub
Signature

Gary''s Student - gsnu200770

> Hi,
>
[quoted text clipped - 79 lines]
> > >
> > > TIA Dave
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.