Consulting

Results 1 to 4 of 4

Thread: Solved: Merge Rows Problem

  1. #1

    Solved: Merge Rows Problem

    Hi all,

    I wrote a simple code to merge rows, but it causes the problem when having blank rows. I've attached a file to explain what I want to do..Thank you for kind attention!!

    [vba]
    Public Sub MergeRows()
    Set myRange = Range("A1:A50")
    For Each c In myRange
    jText = jText & "+" & c
    Next
    Range("B2") = jText

    End Sub
    [/vba]

    [vba]
    PROBLEM+1+2+3+4+5+6+7+8++++++14+15+16+17+18+19+20+21+22+23+24+25+26+++++31+32+33+3 4+35+36+37+38+39+40+41+42+43+44+45+46+47+48+49+50

  2. #2
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    You just need to add a quick test to see if c has anything in it before you append the info to the string:

    [vba]Public Sub MergeRows()

    Set myRange = Range("A1:A50")
    For Each c In myRange

    If Not Len(c) = 0 Then jText = jText & "+" & c

    Next
    Range("B2") = jText

    End Sub[/vba]

    HTH,
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





  3. #3
    Hi, Thanks for your reply.

    There's still a problem when I follow the above code.
    I want to divide it into 3 different sentences, not one.
    e.g in my example, I want the following 3 sentences,

    1+2+3+4+5+6+7+8
    4+15+16+17+18+19+20+21+22+23+24+25+26
    31+32+33+34+35+36+37+38+39+40+41+42+43+44+45+46+47+48+49+50

    Is it possible?

  4. #4
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Adding to Ken's code

    [VBA]
    Public Sub MergeRows()
    Dim MyRange As Range, c As Range
    Dim JText As String
    Set MyRange = Range("A1:A50")
    For Each c In MyRange
    If Not Len(c) = 0 Then
    JText = JText & "+" & c
    Else
    If Right(JText, 1) <> Chr(10) Then JText = JText & Chr(10)
    End If
    Next
    Range("B2") = JText
    End Sub

    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

Posting Permissions

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