Error on a Simple If Then statement

jUStPunkin

Board Regular
Joined
Mar 23, 2009
Messages
67
Hi all,

I'm a bit stumped by this, because the code seems simple enough. I have a criteria, set one way, I want certain sheets to show, set another way, I want other sheets to show.

If my value for "G_CapturedAbove" is N, it works fine.
If my value for "G_CapturedAbove" is Y, it does not hide the spout sheet. Any thoughts?

I have tried this with the ElseIf to just be a Else without the range value, but that didn't work either.

VBA Code:
    If Range("G_CapturedAbove").Value = ("N") Then
        Sheets("Spouts").Visible = True
        Sheets("Spouts2").Visible = False
        Sheets("Redistribution Calculations").Visible = False

    ElseIf Range("G_CapturedAbove").Value = ("Y") Then

        Sheets("Spouts").Visible = False
        Sheets("Spouts2").Visible = True
        Sheets("Redistribution Calculations").Visible = True

    End If
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
Hi
as you are just toggling a True / False state see if this update to your code does what you want

VBA Code:
        Dim IsNo As Boolean
        IsNo = UCase(Range("G_CapturedAbove").Value) = "N"
        Sheets("Spouts").Visible = IsNo
        Sheets("Spouts2").Visible = Not IsNo
        Sheets("Redistribution Calculations").Visible = Not IsNo

Dave
 
Upvote 0
Solution
So, that is closer. That won't display the "Spouts" sheet, but it does fix the problem of both showing when Value is Y

There is other code in this spreadsheet (I'm trying to do a lot, and not a very good code writer), so I suspect another variable is triggering the missing sheet.

I'm going to continue to troubleshoot the rest of the code, and quite possibly rewrite what I have.

Edit: Thanks for this, I actually found the other errant code that was causing issues and fixed it!
 
Upvote 0
The code works for me.

The comparison is case sensitive.
So to always compare with uppercase:
VBA Code:
  If UCase(Range("G_CapturedAbove").Value) = "N" Then
    Sheets("Spouts").Visible = -1
    Sheets("Spouts2").Visible = 0
    Sheets("Redistribution Calculations").Visible = 0
  ElseIf UCase(Range("G_CapturedAbove").Value) = "Y" Then
    Sheets("Spouts").Visible = 0
    Sheets("Spouts2").Visible = -1
    Sheets("Redistribution Calculations").Visible = -1
  End If

Or this simplified way:
VBA Code:
  Sheets("Spouts").Visible = UCase(Range("G_CapturedAbove").Value) = "N"
  Sheets("Spouts2").Visible = UCase(Range("G_CapturedAbove").Value) <> "N"
  Sheets("Redistribution Calculations").Visible = UCase(Range("G_CapturedAbove").Value) <> "N"

Or this:
VBA Code:
  Dim gc As String
  gc = UCase(Range("G_CapturedAbove").Value)
  Sheets("Spouts").Visible = gc = "N"
  Sheets("Spouts2").Visible = gc <> "N"
  Sheets("Redistribution Calculations").Visible = gc <> "N"



Note: If you have other code that affects your code, you should put it here for us to review.

--------------
Let me know the result and I'll get back to you as soon as I can.
Cordially
Dante Amor
--------------​
 
Upvote 0
Edit: Thanks for this, I actually found the other errant code that was causing issues and fixed it!
it would be other code causing that issue but glad you found & resolved it

appreciate your feedback

Dave
 
Upvote 0

Forum statistics

Threads
1,223,275
Messages
6,171,119
Members
452,381
Latest member
Nova88

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top