Solved

Concatenate values using VBA

Posted on 2011-09-19
7
291 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 200 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 200 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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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 100 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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
Viewers will learn the basics of slicers and timelines for both PivotTables and standard Excel tables in Excel 2013.
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…

762 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now