+ Reply to Thread
Results 1 to 7 of 7

Thread: Looping only first cell in a merged cell

  1. #1
    Valued Forum Contributor
    Join Date
    07-07-2005
    Location
    England
    MS-Off Ver
    2003 Pro
    Posts
    338

    Looping only first cell in a merged cell

    Hi, I have a named range which looks at several cells (most of which are merged). I am looping to find out if the cell as a value in it or not but its not working as I expect.

    For example, cell A1:A5 is merged. I am asking my loop to look at cell A1 only, but it insists on checking A2,A3,A4 & A5 as well.

    Can anyone advise a way of avoiding this please?

    Sub SetMandatoryBoxes()
    Range("F6,B8,F8").Select
    ActiveWorkbook.Names.Add Name:="Mandatory", RefersTo:=Selection
    End Sub
    F6 is merged through to R6.
    B8:D8
    F8:H8

    How can I get it to only look at the first cell in the merge?

    Thanks in advance
    John
    Last edited by johncassell; 11-17-2011 at 04:37 AM.

  2. #2
    Forum Guru 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Vienna, VA, USA
    MS-Off Ver
    Excel 2003, 2007
    Posts
    2,257

    Re: Looping only first cell in a merged cell

    You are not showing the code for the loop. You are assuming the problem is that fact that the cells are merged, but I am guessing that the problem is in how you coded it. I did what you describe and it only iterates on the specified cells.

    Are you iterating over a selection? If you do this, it will iterate through all cells in the merged range. When you select any cell in a merged range, you select the whole merged range.
    Range("F6").Select
    For Each c In Selection
       ' Every cell in the merged range will be processed
    Next c
    
    For Each c In Range("F6")
       ' Only cell F6 will be processed
    Next c
    In most cases where you see Select or Selection appearing in VBA code it needs a second look.
    Making the world a better place one fret at a time
    ||||||

    If someone helped you, please click on the star icon at the bottom of their post

    If your problem is solved, please update the first post:
    EDIT, Go Advanced button, set Prefix to SOLVED

    [code]
    ' Enclose code in tags like this
    [/code]

    Don't attach a screenshot
    --just attach your Excel file! It's easier and will let us experiment with your data, formulas, and code.

  3. #3
    Valued Forum Contributor
    Join Date
    07-07-2005
    Location
    England
    MS-Off Ver
    2003 Pro
    Posts
    338

    Re: Looping only first cell in a merged cell

    Sorry, should have posted the loop (and thanks for the reply)

    Basically checks to see if any of the cells have a string length of less than 2 and then colours the cell
    Sub CheckAllBoxes()
    For Each CELL In Range("Mandatory")
    CELLADD = CELL.Address
    If Len(CELL.Value) < 2 Then
    CELL.Interior.ColorIndex = 3
    CELL.Font.ColorIndex = 5
    Count = Count + 1
    End If
    Next
    If Count > 0 Then
    MsgBox "Please complete all mandatory fields. The missing fields will be highlighted red."
    End If
    End Sub
    thanks
    John

  4. #4
    Valued Forum Contributor
    Join Date
    10-22-2011
    Location
    Ufa, Russia
    MS-Off Ver
    Excel 2010
    Posts
    522

    Re: Looping only first cell in a merged cell

    maybe this:
    Sub SetMandatoryBoxes()
    Dim r As Range
    Set r = Union([b8].MergeArea.Cells(1), [f6].MergeArea.Cells(1), [f8].MergeArea.Cells(1))
    'Range("F6,B8,F8").Select
    ActiveWorkbook.Names.Add Name:="Mandatory", RefersTo:=r
    End Sub

  5. #5
    Valued Forum Contributor
    Join Date
    07-07-2005
    Location
    England
    MS-Off Ver
    2003 Pro
    Posts
    338

    Re: Looping only first cell in a merged cell

    Hi, I've now took note of what 6StringJazzer was saying and have now scrapped the named range and instead of looping the selection, I am now just looping the actual range and its working perfect

    For Each cell In Range("F6:F6,B8:B8")...
    didn't need to try your code Nilem but thank you for your response.

    John

  6. #6
    Forum Guru 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Vienna, VA, USA
    MS-Off Ver
    Excel 2003, 2007
    Posts
    2,257

    Re: Looping only first cell in a merged cell

    Quote Originally Posted by johncassell View Post
    Basically checks to see if any of the cells have a string length of less than 2 and then colours the cell
    I took a second look at this--did you know that you can do this with conditional formatting without VBA code?

  7. #7
    Valued Forum Contributor
    Join Date
    07-07-2005
    Location
    England
    MS-Off Ver
    2003 Pro
    Posts
    338

    Re: Looping only first cell in a merged cell

    Hi, yes I am using that as well. All boxes start off as a pale yellow. When user enters some data it turns bright green (conditional formatting). When they hit the 'Send' macro button this is when it says 'Some boxes are empty' and then fills them all in red to make it obvious.

    John

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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.2.0