+ Reply to Thread
Results 1 to 3 of 3

Concatenating two (2) columns

Hybrid View

Jim15 Concatenating two (2) columns 11-06-2008, 03:52 PM
Bob Phillips Sub ConcatColumns() Dim i As... 11-06-2008, 05:05 PM
Jim15 Thanks Mr. Phillips! This... 11-07-2008, 10:58 AM
  1. #1
    Forum Contributor
    Join Date
    08-15-2005
    Location
    Oklahoma
    MS-Off Ver
    2010, 2013
    Posts
    113

    Concatenating two (2) columns

    This is the concatenation macro I have for 2 columns. I want to make this more general in that I would like the program to insert a new column to the right and concatenate the 2 columns based on the current cell the cursor is on and the one adjacent to the left (-1). The loop works fine, the modification I need is for the following three (3) lines.

    Columns("I:I").Select
        Selection.Insert Shift:=xlToRight
        Range("H2").Select
    
    Sub ConcatColumns()
        
        Columns("I:I").Select
        Selection.Insert Shift:=xlToRight
        Range("H2").Select
          
          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, 0) & " - " & ActiveCell.Offset(0, -1)
    
             ActiveCell.Offset(1, 0).Select
          Loop
    
       End Sub
    Thanks,

    Jim15
    Last edited by NBVC; 11-07-2008 at 11:00 AM.
    Jim15

  2. #2
    Forum Expert Bob Phillips's Avatar
    Join Date
    09-03-2005
    Location
    Wessex
    MS-Off Ver
    Office 2003, 2010, 2013, 2016, 365
    Posts
    3,284
    Sub ConcatColumns()
    Dim i As Long
    
        ActiveCell.Offset(0, 1).EntireColumn.Insert Shift:=xlToRight
        
        With Cells(2, ActiveCell.Column)
          
            Do While .Offset(i, 0) <> "" 'Loops until the active cell is blank.
    
               .Offset(i, 1).Value = .Offset(i, -1) & " - " & .Offset(i, 0)
               i = i + 1
            Loop
        End With
    End Sub

  3. #3
    Forum Contributor
    Join Date
    08-15-2005
    Location
    Oklahoma
    MS-Off Ver
    2010, 2013
    Posts
    113
    Thanks Mr. Phillips! This works great and I appreciate your assitance.

    Jim Williams

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1