View Single Post
  #6   Report Post  
Posted to microsoft.public.excel.worksheet.functions
OssieMac OssieMac is offline
external usenet poster
 
Posts: 2,510
Default Vertical Concatenate function

Hi again,

To install the macro:-

Open the workbook.
Alt/F11 to open the VBA editor.
Select Menu item Insert then select Module.
Copy the macro and paste it into the module.
Close the VBA editor (Cross in red background far top right)
Save workbook.

To run the macro from the worksheet:-

Select menu item Tools-Macro-Macros.
Select the macro name (Should default to it).
Click Run.

After run:-
You will need to autofit the columns in the output. If you selected the Wrap
Text option in the macro, then autofit the rows also. (Essential to do
columns first). You said that you know how to record a macro, so you could
record the columns and rows autofit then copy and paste it into the bottom of
the main macro for future use. (Note that when you record another macro in
the workbook, it will be on a new module and you will have to copy it from
there. You can then right click on the extra module in the Project Explorer
and remove it.)

If required, To insert a button to run the macro:-
There are two types of controls. Forms controls and ActiveX controls. The
Forms controls are on the Forms Toolbar and the ActiveX controls are on the
Control Toolbox toolbar.

The easy method for this project is the Forms control so if not already
displayed, then display the Forms toolbar. (Menu item View-Toolbars-Forms).
Select the button and the cursor turns to a fine line plus sign. Move the
cursor to the worksheet and then hold the left button down on the mouse and
drag the button out to the size required.
When you release the mouse button, the Assign Macro dialog box appears.
Select the macro name (Concat_Comments) then OK.
Highlight the default button name (Button1) and edit to the name you want.
Click anywhere on the worksheet.
Click on the button to run the macro.

Note that the macro that I have given you does not clear the output
worksheet before running. If there is anything on it then you should clear it
first.

Feel free to get back to me again if you have any other problems or the
macro doen not work exactly as you want.

I'll post the macro again so you have it all together with the instructions:-

Sub Concat_Comments()

Dim wsInput As Worksheet
Dim wsOutput As Worksheet
Dim rngInvoice As Range
Dim invNbr As Range
Dim strComments As String
Dim Invoice As Variant

'Edit with your input sheet name
Set wsInput = Sheets("Sheet1")

'Edit with your output sheet name
Set wsOutput = Sheets("Sheet2")

'Starts as row 2 because of column headers
'Ends at one cell past last data in 1st column.
With wsInput
Set rngInvoice = Range(.Cells(2, 1), _
.Cells(.Rows.Count, 1).End(xlUp).Offset(1, 0))
End With

'Prepare output sheet with column headers
With wsOutput
.Cells(1, 1) = "Invoice#"
.Cells(1, 2) = "Comments"
End With

'Set Invoice equal to 1st cell in rngInvoice
Invoice = rngInvoice.Cells(1, 1)

For Each invNbr In rngInvoice
If invNbr < Invoice Then 'End of invoice number
With wsOutput
.Cells(.Rows.Count, "A").End(xlUp).Offset(1, 0) = Invoice
.Cells(.Rows.Count, "A").End(xlUp).Offset(0, 1) = strComments
End With
strComments = ""
Invoice = invNbr
End If

If Len(Trim(invNbr.Offset(0, 2))) 0 Then

If Len(Trim(strComments)) 0 Then
'Following inserts a space between comments
strComments = strComments & " "

'Alternative inserts a linefeed to wrap text
'strComments = strComments & Chr(10)
End If

strComments = strComments & Trim(invNbr.Offset(0, 2))

End If
Next invNbr

End Sub




Regards,

OssieMac