+ Reply to Thread
Results 1 to 6 of 6

Merge Cells in Dynamic Range using For loop

  1. #1
    Registered User
    Join Date
    08-01-2013
    Location
    St. Louis
    MS-Off Ver
    Excel 2010
    Posts
    9

    Merge Cells in Dynamic Range using For loop

    Hello All -

    I need to be able to dynamically merge cells using VBA. I am not very experience with VBA at all. I have pieced together some code based on google searches and finding bits of code here and there, but I keep getting an error saying: "Compile error: Wrong number of arguments or invalid property assignment"

    Column C has data in it starting in row 12 and the data range in that column is dynamic. The dynamic ranges needs to always start in cell C12 and merge the cells in C, D and E. So, it will need to start with range C12:E12 and merge those 3 cells. Then the loop will need to go to cell C13 and if it's NOT blank, then merge, C13:E13. The loop will need to continue until it reaches the last NON blank cell in column C starting with cell C12.

    I would greatly appreciate any help you could provide!

    The code is breaking at the .Range part of the If statement
    Below is the code that I have so far:


    Sub Merge_Cells()
    '
    ' Merge_Cells Macro


    Application.ScreenUpdating = False

    Dim i As Long
    Dim WS As Worksheet
    Dim LastCell As Range
    Dim LastCellRowNumber As Long

    For i = LastCellRowNumber To 12

    Set WS = Worksheets("AUTO-EMAIL")
    With WS
    Set LastCell = .Cells(.Rows.Count, "C").End(xlUp)
    LastCellRowNumber = LastCell.Row

    If Cells(i, 1) = "" Then .Range(.Cells(i, 0), .Cells(i, 1), .Cells(i, 2)).Merge
    End With

    Next i

    Application.ScreenUpdating = True

    End Sub

  2. #2
    Forum Expert GeneralDisarray's Avatar
    Join Date
    09-15-2011
    Location
    Pittsburgh, PA, USA
    MS-Off Ver
    Windows Excel 2016
    Posts
    1,416

    Re: Merge Cells in Dynamic Range using For loop

    put your code in code tags please as per forum rules.

    Edit the post and highlight your code. THen press the code tag button (looks like this #) and save the changes
    Remember, saying thanks only takes a second or two. Click the star icon(*) below the post you liked, to give some Rep if you think an answer deserves it.

    Please,mark your thread [SOLVED] if you received your answer.

  3. #3
    Forum Expert GeneralDisarray's Avatar
    Join Date
    09-15-2011
    Location
    Pittsburgh, PA, USA
    MS-Off Ver
    Windows Excel 2016
    Posts
    1,416

    Re: Merge Cells in Dynamic Range using For loop

    but as to the code. You don't need to loop here, you can just find that last non-blank cell and merge the block. You know this will eliminate the values entered into all the cells other than c12 right?


    to find the last non-blank cell in C column use this:

    i = ws.cells(rows.count,3).end(xlup)

    You can just try this:

    ws.range("c12:e" & ws.cells(rows.count,3).end(xlup).row).Merge

  4. #4
    Registered User
    Join Date
    08-01-2013
    Location
    St. Louis
    MS-Off Ver
    Excel 2010
    Posts
    9

    Re: Merge Cells in Dynamic Range using For loop

    GeneralDisarray - thanks for the quick response!

    Sorry for not putting my code in tags. I'm new to this, but I will make sure I put it in tags going forward.

    I will try what you have provided below and see if that works. Cells in column D and E are blank so merging the cells won't remove anything.

    Again, thank you!

  5. #5
    Registered User
    Join Date
    08-01-2013
    Location
    St. Louis
    MS-Off Ver
    Excel 2010
    Posts
    9

    Re: Merge Cells in Dynamic Range using For loop

    General Disarray -

    that code is not working for me for some reason.

    Basically, the VBA code needs to merge cells C12:E12. Then, it needs to go to cell C13 and if that cell is not blank, then merge, C13:E13. Then it needs to go to cell C14 and if that cell is not blank, then merge C14:E14......you get the idea. There will never been any content in columns D and E. Wouldn't I need a For loop for this?

    Again, if you could provide any help on this, I would really appreciate it!

  6. #6
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Merge Cells in Dynamic Range using For loop

    Is this what you're trying to do??

    Please Login or Register  to view this content.

+ 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. How to merge dynamic range of cells?
    By Swagata in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-24-2013, 01:08 AM
  2. [SOLVED] Loop through column and merge cells
    By christensen in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 08-10-2012, 04:17 AM
  3. [SOLVED] VBA to auto merge and middle align for particular row in dynamic range
    By cruise.alter in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-09-2012, 02:32 AM
  4. Loop with dynamic range
    By mthomas in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 12-22-2011, 10:35 PM
  5. A loop to merge cells based on a value
    By tschultz in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 08-24-2005, 02:50 PM

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