Consulting

Results 1 to 10 of 10

Thread: Solved: Clear content of a merged cell

  1. #1

    Solved: Clear content of a merged cell

    Hey!

    What I’m trying to do is simply clear the contents of 2 different merged cells named “FirstName” and “LastName” on the click of a cmd button, which I have already created and is already carrying out some functions. But this is proving much more difficult than clearing a non-merged cell. When I try: [VBA]range(“firstname”).clearcontents[/VBA]
    This return an error saying you cannot edit a merged cell.

    So I did a bit of internet research and it threw up this: http://antonio.beyah.net/blog/2008/04/08/vba-merged-cell-information/
    This person appears to have worked it out, but me being very new to this I couldn’t make sense of it.
    If anyone can make sense of it… and give me some help with it that would be great.
    Cheers,
    Joe

  2. #2
    forgot to say using vba in excel 2007 if it makes a difference

  3. #3
    VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Why cause yourself the grief? Avoid merged cells at all costs. You now know why. Unmerge the cells and use center across selection.

    After you unmerge them select the cells you had merged and right click-select format cells.

    In the allignment tab look for horizontal alignment...drop the box and select "center across selection"
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  4. #4
    VBAX Regular
    Joined
    Nov 2008
    Posts
    44
    Location
    Assuming you can't change the fact that you have merged cells, try this:

    [vba]Dim mRange As Excel.Range
    Set mRange = ActiveSheet.Range("firstname").MergeArea
    mRange.ClearContents[/vba]
    This is similar to what you found at the site you linked, but using a named range instead. I think using the MergeArea property returns a range object that represents the merged area. Seemed to work for me.


    Duluter


    [EDIT]

    As #5 points out, it's lame to include the ActiveSheet nonsense above. My bad.
    Last edited by duluter; 02-06-2009 at 09:52 PM.

  5. #5
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    [VBA]Sub ClearAbomination()
    Range("firstname").MergeArea.ClearContents
    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'

  6. #6
    VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    A little strong Malcolm, but you know how to make a point.....
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  7. #7
    solved!!!! cheers for all the help

    regards,

    Joe

  8. #8
    VBAX Newbie
    Joined
    Oct 2017
    Location
    Innisfail Qld
    Posts
    2
    Location
    I have this problem above currently, but I am using VBA: Retail 7.1.1071 from Office 365.
    I have tried adding below to my macro but it still doesn't work.
    I am not that familiar with using "Name Manager" but I believe I have used it correctly see attached pic. Name manager - TransactionTask.JPG
    Any ideas


    Sub Clear_Cells()
    Range("G17").ClearContents
    Range("G22").ClearContents
    Range("G27").ClearContents
    Range("B3:B21").ClearContents
    Range("B26:B49").ClearContents
    Range("B53:B55").ClearContents
    Range("b57:b64").ClearContents
    Range("E3:E12").ClearContents
    Range("E17:E27").ClearContents
    Range("E34:E67").ClearContents
    Range("G34:G50").ClearContents
    Range("G57:G64").ClearContents
    Range("I34:I50").ClearContents
    Range("J34:J50").ClearContents
    Range("TransactionTask").MergeArea.ClearContents


    MsgBox "Form has been RESET"


    End Sub

  9. #9
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Range does not take a MergeArea. Simply
    Range("TransactionTask").ClearContents
    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'

  10. #10
    VBAX Newbie
    Joined
    Oct 2017
    Location
    Innisfail Qld
    Posts
    2
    Location

    Clearing a Merged Range of 4 columns and 2 rows

    Quote Originally Posted by mdmackillop View Post
    Range does not take a MergeArea. Simply
    Range("TransactionTask").ClearContents
    I thought I had tried this already but obviously not as it worked ..... thank you. :-)

Posting Permissions

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