Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Concatenate values using VBA

Posted on 2011-09-19
7
Medium Priority
?
347 Views
Last Modified: 2012-05-12
Dear Experts:

I got values in Column C and D

As soon as the macro hits an entry in Column C …
... the macro is to enter a formula in Column E concatenating the values in Column C and D with the values separated by a line break (e.g. C5&Chr(10)&D5 or C10&Chr(10)&D10)
and so forth.

I have attached a sample file for your convenience.

Help is much appreciated.

Thank you very much in advance.

Regards, Andreas

 VBA-To-Enter-Formula.xls
0
Comment
Question by:AndreasHermle
7 Comments
 
LVL 24

Expert Comment

by:StephenJR
ID: 36561417
Andreas - what's wrong with the formula you already have?
0
 
LVL 33

Accepted Solution

by:
jppinto earned 800 total points
ID: 36561446
Try with this code. I've attached a sample file for this.

jppinto
Private Sub Worksheet_Change(ByVal Target As Range)

Dim rng As Range
Set rng = Range("C4:C100")

    If Not Intersect(Target, rng) Is Nothing Then
        Target.Offset(0, 2).Value = Target.Value & Chr(10) & Target.Offset(0, 1).Value
        Target.EntireRow.AutoFit
    End If

End Sub

Open in new window

VBA-To-Enter-Formula.xls
0
 
LVL 24

Assisted Solution

by:StephenJR
StephenJR earned 800 total points
ID: 36561514
I see now, you did it manually? Perhaps this:
With Columns(3).SpecialCells(xlCellTypeConstants)
    .Offset(, 2).Formula = "=" & .Cells(1).Address(0, 0) & " & Char(10) & " & _
                   .Cells(1).Offset(, 1).Address(0, 0)
End With

Open in new window

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 5

Expert Comment

by:slycoder
ID: 36561550
This makes the column values static -

Option Explicit

Sub ConcatColumns()
'
    Dim myLastRow As Integer
    Dim myRange As Range
    Dim myCell As Range
   
    ActiveCell.SpecialCells(xlLastCell).Select
   
    ' Determine last row
    myLastRow = ActiveCell.Row
   
    ' Select column c
    Range("C1:C" & myLastRow).Select
   
    ' Cycle through each cell
    For Each myCell In Selection
   
        ' set contents of Column F
        If myCell.Value <> "" Then
            ' Combine columns with a carriage return separation
            myCell.Offset(0, 3) = myCell.Value & vbCrLf & myCell.Offset(0, 1).Value
        End If
    Next myCell
   
End Sub
0
 
LVL 5

Expert Comment

by:slycoder
ID: 36561752
You did say formula - so StephenJR's method will work (and should receive the points).

Implementing his method with my function:

Option Explicit

Sub ConcatColumns()
'
    Dim myLastRow As Integer
    Dim myCell As Range
   
    ActiveCell.SpecialCells(xlLastCell).Select
   
    ' Determine last row
    myLastRow = ActiveCell.Row
   
    ' Select column c
    Range("C1:C" & myLastRow).Select
   
    ' Cycle through each cell
    For Each myCell In Selection
   
        ' set contents of Column F
        If myCell.Value <> "" Then
       
            ' Combine columns with a carriage return separation
            myCell.Offset(0, 3).Formula = "=" & myCell.Address & " & Char(10) & " & _
                   myCell.Offset(0, 1).Address
                   
            ' use the line below for static values
            ' myCell.Value & vbCrLf & myCell.Offset(0, 1).Value
        End If
    Next myCell
   
End Sub
0
 
LVL 5

Assisted Solution

by:slycoder
slycoder earned 400 total points
ID: 36561784
The function above will set the cell references to absolute($C$4), if you want them to be relative (C4) - you can make the following change:

            ' Combine columns with a carriage return separation
            myCell.Offset(0, 3).Formula = "=" & myCell.Address(0, 0) & " & Char(10) & " & _
                   myCell.Offset(0, 1).Address(0, 0)

Thanks
0
 

Author Comment

by:AndreasHermle
ID: 36563031
Dear All:

Great help from all of  you.  Thank you very much for  your professional support. I suggest splitting the point since all approaches work just fine, although I slightly favour jppinto's and StephenJR's approach since no selection is involved.

Again, thank you very much for your great support.
Regards, Andreas
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

963 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