+ Reply to Thread
Results 1 to 5 of 5

Else If Statments VBA

  1. #1
    Registered User
    Join Date
    01-30-2013
    Location
    Dublin, Ireland
    MS-Off Ver
    Excel 2003
    Posts
    33

    Wink Else If Statments VBA

    Probably a very simple question - but Im new to this, Im trying to get an If then Elseif statement to complete a value in a cell. It currently works for 2 possible variables to be entered into Cell F8 - Im trying to get this logic to work for 11 possible variables - more than 0.3 less than 0.4 producing a value in cell G8 of "<0.4 " etc - 0.3, 0.4 , 0.5, 0.6, 0.7, 0.8, 0.9

    Sub UnitSize()

    If Range("F8") <= 0.3 Then 'If the range is below 0.3 then display <0.30 in cell G8 '

    Range("G8") = "<0.3"

    ElseIf Range("F8") >= 0.3 < 0.4 Then 'If the range is above 0.3 and below 0.4 then display <0.4 in cell G8

    Range("G8") = "<0.4"

    ElseIf Range("F8") >= 0.4 < 0.5 Then

    Range("G8") = "<0.5"

    End If

    End Sub

    It seems to work for the first two Elseifs - 0.3 & 0.4 - but not 0.5 - I was wondering what the obvious mistake is and if this is the best way to approach this as I want to continue with the logic to 0.6, 0.7, 0.8 etc

    Thanks

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Else If Statments VBA

    This is how you would write the ElseIfs
    Please Login or Register  to view this content.
    Mind you, you could probably use a formula for this.
    If posting code please use code tags, see here.

  3. #3
    Valued Forum Contributor
    Join Date
    08-13-2012
    Location
    Gardony, Hungary
    MS-Off Ver
    Excel 2003
    Posts
    558

    Re: Else If Statments VBA

    Just change the syntax a little bit and it should work.
    Please Login or Register  to view this content.

  4. #4
    Registered User
    Join Date
    01-30-2013
    Location
    Dublin, Ireland
    MS-Off Ver
    Excel 2003
    Posts
    33

    Re: Else If Statments VBA

    Brilliant! Thanks for this . It was something that was used in a formula but the options have just increased so I thought it would be better to hide everything behind a button in VB so that no one would be able
    to unknowingly edit it as happens with formulas.

  5. #5
    Valued Forum Contributor
    Join Date
    08-13-2012
    Location
    Gardony, Hungary
    MS-Off Ver
    Excel 2003
    Posts
    558

    Re: Else If Statments VBA

    Glad it works.

    Please don't forget to mark the thread solved to please the mods. :P

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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