Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements.
Sample Visual Basic Procedure
Sub ConcatColumns()
Do While ActiveCell <> "" 'Loops until the active cell is blank.
'The "&" must have a space on both sides or it will be
'treated as a variable type of long integer.
ActiveCell.Offset(0, 1).FormulaR1C1 = _
ActiveCell.Offset(0, -1) & " " & ActiveCell.Offset(0, 0)
ActiveCell.Offset(1, 0).Select
Loop
End Sub
NOTE: The statement ActiveCell.Offset(0, 1).FormulaR1C1 can be replaced
with the statement ActiveCell.Offset(0, 1).Formula. They can be used with
equal success if you are using text and numbers only (not formulas). The
R1C1 used at the end of the first statement refers to row one, column one
and is the form used in examples in Help.
To Use the Macro in Microsoft Excel 97
- Open the workbook which contains the data.
- Press ALT+F11 to activate the Visual Basic Editor.
- Click Module on the Insert menu to insert a module. Type the macro
above in the module's code window.
- Click Close and Return to Microsoft Excel on the File menu.
- Select the worksheet that contains the data that you want to
concatenate.
- Click the top cell in the right-hand column of data that you want to
concatenate. For example, if cells A1:A100 and B1:B100 contain data,
click cell B1.
- Point to Macros on the Tools menu and click Macro. Select the
ConcatColumns macro, and click Run.
To Use the Macro in Microsoft Excel 5.0 or 7.0
- Open the workbook which contains the data.
- Point to Macro on the Insert menu and click Module to insert a
Visual Basic module in the workbook. Type the macro above on the
module sheet.
- Select the worksheet that contains the data that you want to
concatenate.
- Click the top cell in the right-hand column of data that you want to
concatenate. For example, if cells A1:A100 and B1:B100 contain data,
click cell B1.
- On the Tools menu, click Macro, click the ConcatColumns macro, and
click Run.