Results 1 to 8 of 8

Thread: [RESOLVED] if else statement

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Oct 2011
    Posts
    255

    Resolved [RESOLVED] if else statement

    hi
    im using thses line of code to pull stuff of an excel sheet
    Code:
           
     oRng = oWSSheet2.Cells.Find("Class B", , _
    Excel.XlFindLookIn.xlValues, Excel.XlLookAt.xlPart, _
    Excel.XlSearchOrder.xlByRows, Excel.XlSearchDirection.xlNext, False)
            ' MsgBox("Value '" & app oRng.Value & "' found at '" & oRng.Address)
                Me.classb = oRng.Offset(0, 42).Value
                oRng = Nothing
    but sometime some spreadsheets wont have the word class b in it but i get an error if it doesnt. i want to put in an if statement so if its null to go to next piece of code.

    how can this be done in vb

    if i try this
    if orng is not null then
    Me.classb = oRng.Offset(0, 42).Value
    else
    ....
    but that's not quite right

  2. #2

    Thread Starter
    Addicted Member
    Join Date
    Oct 2011
    Posts
    255

    Re: if else statement

    when i try this i get an error as my values are in Decimal
    Code:
       ' MsgBox("Value '" & app oRng.Value & "' found at '" & oRng.Address)
            If Me.classb = String.Empty Then
                Me.classb = 0.0
            Else
    
                Me.classb = oRng.Offset(0, 42).Value
            End If
    anyway around this

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

    Re: if else statement

    Try this

    Code:
    If Not oRng Is Nothing Then
    
    End If
    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

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: if else statement

    Quote Originally Posted by koolsid View Post
    Try this

    Code:
    If Not oRng Is Nothing Then
    
    End If
    Or rather:
    Code:
    If oRng IsNot Nothing Then
    
    End If
    Functioanlly equivalent but much more natural, which is why IsNot was added to the language.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780

    Re: if else statement

    Quote Originally Posted by rjhe22 View Post
    Code:
           
     oRng = oWSSheet2.Cells.Find("Class B",
    To note, you appear to be using the double-dot notation, which is notorious for stopping the release of the object, meaning that Excel does not close when your code is completed.

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

    Re: if else statement

    @JM: Yup.

    @Grimfort: There is nothing wrong with the usage of two dot rule. You might want to see this?
    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
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780

    Re: if else statement

    Quote Originally Posted by koolsid View Post
    @Grimfort: There is nothing wrong with the usage of two dot rule. You might want to see this?

    I don't think there is enough evidence in that blog to backup that statement. I have tried (there are samples I have posted on this forum) using all kinds of methods including forcing GC and ReleaseComObject, and the clear-up was not successful in all circumstances. I won't go into a debate on it here, but from my own experience in this area, it is something to avoid, or at least understand if you get the issue.

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Oct 2011
    Posts
    255

    Re: if else statement

    that worked thanks
    also will look into the dot stuff thanks

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