+ Reply to Thread
Results 1 to 5 of 5

VBA to Merge Cells based on Other Column Values

Hybrid View

  1. #1
    Registered User
    Join Date
    03-18-2014
    Location
    Dallas, Texas
    MS-Off Ver
    Excel 2007
    Posts
    3

    VBA to Merge Cells based on Other Column Values

    Hi - first post to this forum. Now I know that merging cells is usually not a good thing in VBA, but I'm working on a data report that just looks bad unless longer pieces of text can span several cells to minimize column width.

    So here's the problem:

    I have a column of 1's and 0's in columns E and F, and text in Column G. I want to merge the cells in columns G and H for that specific row if there is a 1 in either the E or F column for that specific row. And I need to do this for a long range...rows 7-5000. Any ways that VBA can do this?

    Thanks for the help.

  2. #2
    Forum Expert Solus Rankin's Avatar
    Join Date
    05-24-2013
    Location
    Hollywood, CA
    MS-Off Ver
    Win7 Office 2010 VS Express 2012
    Posts
    2,655

    Re: VBA to Merge Cells based on Other Column Values

    Sub meystingray()
    Dim l As Long
    
    For l = 7 To 5000
        If Cells(l, 5).Value = 1 Or Cells(l, 6).Value = 1 Then
            Range(Cells(l, 7), Cells(l, 8)).HorizontalAlignment = xlCenterAcrossSelection
        End If
    Next l
    End Sub
    Maybe this is best of both worlds. It centers across the cells without merging.
    Thanks,
    Solus


    Please remember the following:

    1. Use [code] code tags [/code]. It keeps posts clean, easy-to-read, and maintains VBA formatting.
    Highlight the code in your post and press the # button in the toolbar.
    2. Show appreciation to those who have helped you by clicking below their posts.
    3. If you are happy with a solution to your problem, mark the thread as [SOLVED] using the tools at the top.

    "Slow is smooth, smooth is fast."

  3. #3
    Registered User
    Join Date
    03-18-2014
    Location
    Dallas, Texas
    MS-Off Ver
    Excel 2007
    Posts
    3

    Re: VBA to Merge Cells based on Other Column Values

    Solus - thanks for the help - but it looks like this is just centering the text in the cell and not across multiple cells. The text is large, but only for a few rows here and there, and I don't want to increase the whole width of the column just for these texts. Does centering across a range act similar to merging?

  4. #4
    Forum Expert Solus Rankin's Avatar
    Join Date
    05-24-2013
    Location
    Hollywood, CA
    MS-Off Ver
    Win7 Office 2010 VS Express 2012
    Posts
    2,655

    Re: VBA to Merge Cells based on Other Column Values

    It displays them centered across the cells (for you its centered across G & H, the columns you requested) without changing the cell width, and keeps the cells unmerged.

  5. #5
    Registered User
    Join Date
    03-18-2014
    Location
    Dallas, Texas
    MS-Off Ver
    Excel 2007
    Posts
    3

    Re: VBA to Merge Cells based on Other Column Values

    I combined your code with something else and got it to work. Thanks for the help!

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Merge values from column in one cell, based on duplicates in another column
    By Rawland_Hustle in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 01-24-2014, 11:04 AM
  2. need to merge cells in column B based on data in column A
    By saintdesy in forum Excel General
    Replies: 2
    Last Post: 08-15-2012, 05:50 AM
  3. Merge cells in a column based on cells highlighted in other column
    By nileshrg in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 11-27-2011, 03:22 AM
  4. Merge Cells based on an adjacent row of values
    By Metal Head in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-04-2011, 09:55 AM
  5. Replies: 0
    Last Post: 07-27-2010, 03:08 PM

Tags for this Thread

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