Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Excel VBA > Excel VBA
|
Excel VBA Discuss using VBA for Excel programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Excel VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 26th, 2012, 11:43 PM
Authorized User
 
Join Date: Jun 2005
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
Default Concatenate text from columns and rows

Hi All,

I have data in a number of rows and columns that I need to concatenate the text within this range. The number of rows and columns are different each time therefore, I wrote a code to count the number of rows and columns.

However, I have difficulty writing a code that is able to concatenate the text in each column properly.

Can anyone advise?

Thanks!
 
Old December 9th, 2012, 09:34 AM
Friend of Wrox
 
Join Date: Sep 2005
Posts: 812
Thanks: 1
Thanked 53 Times in 49 Posts
Default

Hi

It would be nice if you can provide more information

Cheers
Shasur
__________________
C# Code Snippets (http://www.dotnetdud.blogspot.com)

VBA Tips & Tricks (http://www.vbadud.blogspot.com)
 
Old December 18th, 2012, 01:16 PM
Registered User
 
Join Date: Jul 2012
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default Code for 3 functions that concatenate multiple texts

hi, I include 3 functions to choose from. The 1st is mine and is the simplest, the others have proper reference so don't ask me for help on them...


Code:
' MyConCat
' =MyConCat(" - ",A1:A15)
' or =MyConCat(,A5:B23)
'
' Very simple By Apostolos Goulandris
'
Function MyConCat(myDelimiter As String, Avar As Range) As String
Dim b As Variant, Dum As String

If IsMissing(myDelimiter) Then myDelimiter = ""
For Each b In Avar
    Dum = IIf(Len(b) > 0, Dum & myDelimiter & b, Dum)
Next
MyConCat = IIf(Len(myDelimiter) > 0, Mid(Dum, Len(myDelimiter) + 1, Len(Dum)), Dum)

End Function
Code:
' Concatenate multiple items...
'
' =CONCAT(A1:A12)
' =CONCAT(A1:A12," - ")
'
' by HansV at http://www.eileenslounge.com/viewtopic.php?f=27&t=6270
'
Function ConCatA(rng As Range, Optional sep As String = ",") As String
  Dim rngCell As Range
  Dim strResult As String
  For Each rngCell In rng
    If rngCell.Value <> "" Then
      strResult = strResult & sep & rngCell.Value
    End If
  Next rngCell
  If strResult <> "" Then
    strResult = Mid(strResult, Len(sep) + 1)
  End If
  ConCatA = strResult
End Function
Code:
' Concatenate multiple items in multiple ranges
'
' =ConCat("",A1:A3,C1,"HELLO",D1:D2)
' or like this (note the leading comma)...
' =ConCat(,A1:A3,C1,"HELLO",D1:D2)
'
' By Rick Rothstein at http://www.eileenslounge.com/viewtopic.php f=27&t=6270
'
Function ConCatB(Delimiter As Variant, ParamArray CellRanges() As Variant) As String

    Dim Cell As Range, Area As Variant

    If IsMissing(Delimiter) Then Delimiter = ""

    For Each Area In CellRanges
        If TypeName(Area) = "Range" Then
            For Each Cell In Area
                If Len(Cell.Value) Then ConCatB = ConCatB & Delimiter & Cell.Value
            Next
        Else
            ConCatB = ConCatB & Delimiter & Area
        End If
    Next

    ConCatB = Mid(ConCatB, Len(Delimiter) + 1)
End Function
 
Old December 18th, 2012, 09:16 PM
Authorized User
 
Join Date: Jun 2005
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
Default Concatenate text from columns and rows

Thanks for your help!





Similar Threads
Thread Thread Starter Forum Replies Last Post
Converting of rows to columns and merging resulting rows Ole_Marius SQL Language 2 March 20th, 2012 05:35 AM
code to read two text files, concatenate one...... dgr7 Beginning VB 6 2 April 18th, 2007 03:24 PM
Concatenate Rows stonesbg ASP.NET 2.0 Basics 5 February 6th, 2007 06:12 PM
Concatenate TEXT Type Value 1kHz SQL Language 5 December 31st, 2004 01:29 AM
Rows into columns shamsad SQL Language 0 April 7th, 2004 04:39 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.