Results 1 to 13 of 13

Thread: [RESOLVED] HELP determine if a certain cell is merged

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Feb 2008
    Location
    Waterloo, ON
    Posts
    251

    Resolved [RESOLVED] HELP determine if a certain cell is merged

    I have an application that reads data from tables in excel. The tables have entries which usually take up one line, then the next line is blank and merged across the table, so in order to get to the next entry I advance 2 rows.

    The problem arises when entries take up two rows. In this case advancing 2 rows brings you to a blank, merged row. What I want to do is after advancing two rows, determine if that row is merged and if it is advance another row. Is there a way to determine if a certain cell address is part of a larger merged cell?

    Thanks,
    --Rikki

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: HELP determine if a certain cell is merged

    if not range("C3").mergecells then
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Feb 2008
    Location
    Waterloo, ON
    Posts
    251

    Re: HELP determine if a certain cell is merged

    Ah k thanks. Another problem: I'm trying to determine whether "No Activity for this Account" is present in the worksheet so I am using the find method in an if statement as follows:

    Code:
    If Not oWorkSheet.Cells.Find("No Activity for this Account") = Nothing Then
    This works fine if it is not present, but if it is it throws the error "Run-time exception thrown : System.InvalidCastException - Operator '=' is not defined for type 'Range' and 'Nothing'." If I change it to this:

    Code:
    If Not oWorkSheet.Cells.Find("No Activity for this Account").Equals(nothing) Then
    Then it works if it is present, but if it isn't it gives the error "Referenced object has a value of 'Nothing'."

    How Can I get this to work all the time?

    Thanks,
    --Rikki
    Last edited by Rikki_UW; Oct 30th, 2008 at 08:06 AM.

  4. #4
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: HELP determine if a certain cell is merged

    I do it this way. Please amend as applicable...

    vb Code:
    1. Sub Test()
    2.  
    3. 'Switch off Error
    4. On Error Resume Next
    5.  
    6. If Not Sheets("Sheet1").Cells.Find(What:="No Activity for this Account", After:=ActiveCell, LookIn _
    7.         :=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
    8.         xlNext, MatchCase:=False, SearchFormat:=False) Then
    9.     MsgBox "Found"
    10. Else
    11.     MsgBox "Not Found"
    12. End If
    13.  
    14. 'Switch Error back on
    15. On Error GoTo 0
    16.  
    17. End Sub
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Feb 2008
    Location
    Waterloo, ON
    Posts
    251

    Re: HELP determine if a certain cell is merged

    I can't switch off errors because the method also contains a try...catch statement, and it's giving me a type mismatch error.

  6. #6
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: HELP determine if a certain cell is merged

    You don't have to switch off and on errors as it seems. you can switch it off just before the IF and switch it on after ENDIF. don't use the code that I have given above... that is to show you how I tested it in excel... use your own code between

    vb Code:
    1. On Error Resume Next
    2. 'your if code
    3. On Error GoTo 0
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Feb 2008
    Location
    Waterloo, ON
    Posts
    251

    Re: HELP determine if a certain cell is merged

    I switched it back to my if code. This is what I have:

    Code:
                    On Error Resume Next
                    If Not Not oWorkSheet.Cells.Find("No Activity for this Account") = Nothing Then
                        noActivity = oWorkSheet.Cells.Find("No Activity for this Account", nextFind).Row()
                    End If
                    On Error GoTo 0
    But both the error off and error on lines are underline with the error "Method cannot contain both a 'try' statement and an 'On Error' or 'Resume' statement." Same with the try statement which follows a few lines after errors are switched back on.

    Also, which version should I be using? "= nothing" or ".equals(nothing)"?
    Last edited by Rikki_UW; Oct 30th, 2008 at 09:52 AM.

  8. #8
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: HELP determine if a certain cell is merged

    Try this, since it is working when the string is found...
    If Not oWorkSheet.Cells.Find("No Activity for this Account").Equals(nothing) Then
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Feb 2008
    Location
    Waterloo, ON
    Posts
    251

    Re: HELP determine if a certain cell is merged

    Tried that but it gives the error "Referenced object has a value of 'Nothing'." if it is not present.

    I'm trying putting them both in separate try catch statements so that one of them will always work.

  10. #10
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: HELP determine if a certain cell is merged

    can you upload your workbook?
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Feb 2008
    Location
    Waterloo, ON
    Posts
    251

    Re: HELP determine if a certain cell is merged

    That worked, thanks for the help though. If there's a better way to do it though I'm definitely open to suggestions.

    --Rikki

  12. #12
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: [RESOLVED] HELP determine if a certain cell is merged

    any object can not = nothing
    should be
    If Not oWorkSheet.Cells.Find("No Activity for this Account") Is Nothing Then
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  13. #13

    Thread Starter
    Addicted Member
    Join Date
    Feb 2008
    Location
    Waterloo, ON
    Posts
    251

    Re: [RESOLVED] HELP determine if a certain cell is merged

    Ah that works, thanks westconn1.

    Sorry koolsid, missed your post. No I can't, too much confidential information.

    --Rikki

Posting Permissions

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



Click Here to Expand Forum to Full Width