+ Reply to Thread
Results 1 to 10 of 10

Using Range.find() doesn't work when the text you look for is in merged cells

Hybrid View

  1. #1
    Registered User
    Join Date
    10-03-2011
    Location
    Quebec, Canada
    MS-Off Ver
    Excel 2010
    Posts
    29

    Exclamation Using Range.find() doesn't work when the text you look for is in merged cells

    Hi,

    I have some VBA code that loops until it finds the cells with "Total / day :" text in it (since from that line, no more data is available).

    In my test, if that cell is B30 and it's a regular cell (not merged with other cell), the code snipet below doesn't go into the IF (the desirable action).

    But if I merge the cell B30 with C30 and D30 to make it look better, the code snipet below do go into the IF, which is bad.

    If I select the merged cell, the address of the cell is still B30.

    Set oRange = Range("B:B").Find(what:="Total / Day :")
    If (oRange Is Nothing) Then
        isValid = False
    End If
    I tried the find() method with different param (lookat=xlPart, searchorder with rows and columns, etc.) with no success.

    Any idea what I'm doing wrong?

  2. #2
    Registered User
    Join Date
    10-03-2011
    Location
    Quebec, Canada
    MS-Off Ver
    Excel 2010
    Posts
    29

    Re: Using Range.find() doesn't work when the text you look for is in merged cells

    Actually, the code snippet is used in a validation function that I call before I start the processing. That function make sure everything is there before I start processing the sheet.

  3. #3
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Using Range.find() doesn't work when the text you look for is in merged cells

    Whilst not 'wrong' per se, the main problem, as you suspect, are the merged cells. Avoid them like the plague, they are just a total nuisance and cause all sorts of problems, not just this one.

    Because you're searching just one column B then the resultant error with the oRange variable means it is always nothing. You could in theory, assuming you have merged say B30:C30, use

    Set orange = Range("B:C").Find(what:="Total / Day :")
    but I urge you to get into the habit of steering clear of merged cells. There's a perfectly good 'Center Across Selection' in the Cell Format Alignment tab which does essentially the same as merge cells but avoids the many problems.

    Regards
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  4. #4
    Registered User
    Join Date
    10-03-2011
    Location
    Quebec, Canada
    MS-Off Ver
    Excel 2010
    Posts
    29

    Re: Using Range.find() doesn't work when the text you look for is in merged cells

    What I find "amusing", is that when I loop through all cell in column "B", and check against this exact same text, it will tell me when the cell contains the text, but when I'm using a built-in function, it won't.

    Right now, it would be too time consuming unmerge the cells as many people use the templates. I tried using B:E as the range, but it still doesn't find it.

  5. #5
    Forum Expert
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2019
    Posts
    4,168

    Re: Using Range.find() doesn't work when the text you look for is in merged cells

    hi nfuids, can you post sample workbook and code that is not working correctly?

  6. #6
    Registered User
    Join Date
    10-03-2011
    Location
    Quebec, Canada
    MS-Off Ver
    Excel 2010
    Posts
    29

    Re: Using Range.find() doesn't work when the text you look for is in merged cells

    Yeah, I will try and do that today or tomorrow!

    the code is on a seperate workbook, I'll combine both for the sake of this forum!

    Thanks

  7. #7
    Registered User
    Join Date
    10-03-2011
    Location
    Quebec, Canada
    MS-Off Ver
    Excel 2010
    Posts
    29

    Re: Using Range.find() doesn't work when the text you look for is in merged cells

    Just for the record, I couldn't find the time to produce a sample workbook, so I make a workaround...

    Since by looping through column B manually and checking if the text is present works, I created a loop for 300 rows that check if the text is present.
    Largest worksheet has like 100 rows. So it shouldn't be a problem.

        For ctp = 0 To 300
            If (Not (LCase(timeSheet.Cells(ROW_DEBUT_CODE_PROJET + ctp, 2)) = "total / jour :")) Then
                foundEndLine = False
            Else
                foundEndLine = True
                Exit For
            End If
        Next ctp
    
        If (Not foundEndLine) Then
            isValid = False
        End If
          
        AreDataValid = isValid

  8. #8
    Registered User
    Join Date
    10-03-2011
    Location
    Quebec, Canada
    MS-Off Ver
    Excel 2010
    Posts
    29

    Re: Using Range.find() doesn't work when the text you look for is in merged cells

    as far as I'm concerned, you can mark this post solved. I can't find how to do this.

  9. #9
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Using Range.find() doesn't work when the text you look for is in merged cells

    Quote Originally Posted by nfuids View Post
    as far as I'm concerned, you can mark this post solved. I can't find how to do this.
    Hi,
    It's not difficult. It's the first item in the FAQ which is available from the header bar.

  10. #10
    Registered User
    Join Date
    10-03-2011
    Location
    Quebec, Canada
    MS-Off Ver
    Excel 2010
    Posts
    29

    Re: Using Range.find() doesn't work when the text you look for is in merged cells

    9. Acknowledge the responses you receive, good or bad. If your problem is solved, please say so clearly, and mark your thread as Solved: Click the Edit button on your first post in the thread, Click Go Advanced, select [SOLVED] from the Prefix dropdown, then click Save Changes. If more than two days have elapsed, the Edit button will not appear -- ask a moderator to mark it.


    I'm here by asking a moderator to mark this thread as SOLVED.

    Regards

+ 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.6.0 RC 1