473,398 Members | 2,113 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

If/Then with 'Or' not Returning Expected Results

I *think* this is the right section as it's a VBA issue, sorry if it's not!

So I'm tryign to get an excel file to open with all but certain worksheets hidden. I have found some code to make ONE sheet visible and all others hidden, and have tried to edit it to get the three I want to show by using 'or' in the If/Then statement, but it's still only showing one worksheet. Here.s the code I've got to:

Expand|Select|Wrap|Line Numbers
  1. Private Sub Workbook_Open()
  2. Dim wsSheet As Worksheet
  3. Application.ScreenUpdating = False
  4.     For Each wsSheet In Worksheets
  5.     If wsSheet.Name = "Allocations" Or wsSheet.Name = "Contact_Info" Or wsSheet.Name = "Mailout" Then
  6.     Else
  7.     wsSheet.Visible = False
  8.     End If
  9.     Next wsSheet
  10. Application.ScreenUpdating = True
  11. End Sub
Thanks for any help!
Feb 5 '14 #1

✓ answered by ADezii

You do not event need the Iff...Else...End If Construct. Just a slight adjustment will do the trick for you:
Expand|Select|Wrap|Line Numbers
  1. Private Sub Workbook_Open()
  2. Dim wsSheet As Worksheet
  3.  
  4. Application.ScreenUpdating = False
  5.  
  6. For Each wsSheet In Worksheets
  7.   With wsSheet
  8.     .Visible = (wsSheet.Name = "Allocations" Or wsSheet.Name = "Contact_Info" _
  9.                 Or wsSheet.Name = "Mailout")
  10.   End With
  11. Next wsSheet
  12.  
  13. Application.ScreenUpdating = True
  14. End Sub

8 1387
Luk3r
300 256MB
Edit**: During your iteration you should do away with your Else statement and use If Not.

Example:
Expand|Select|Wrap|Line Numbers
  1. If Not wsSheet.Name = "Allocations" Or wsSheet.Name = "Contact_Info" Or wsSheet.Name = "Mailout" Then
  2. wsSheet.Visible = False
  3. End If
Feb 5 '14 #2
I'm getting a compile error with that? Saying it's expecting 'Then' or Goto'...
Feb 5 '14 #3
ADezii
8,834 Expert 8TB
You do not event need the Iff...Else...End If Construct. Just a slight adjustment will do the trick for you:
Expand|Select|Wrap|Line Numbers
  1. Private Sub Workbook_Open()
  2. Dim wsSheet As Worksheet
  3.  
  4. Application.ScreenUpdating = False
  5.  
  6. For Each wsSheet In Worksheets
  7.   With wsSheet
  8.     .Visible = (wsSheet.Name = "Allocations" Or wsSheet.Name = "Contact_Info" _
  9.                 Or wsSheet.Name = "Mailout")
  10.   End With
  11. Next wsSheet
  12.  
  13. Application.ScreenUpdating = True
  14. End Sub
Feb 5 '14 #4
NeoPa
32,556 Expert Mod 16PB
NeoBrainless:
"I'm getting a compile error with that? Saying it's expecting 'Then' or Goto'..."
That's because Luk3r omitted the [ CODE ] tags when posting and you copied the code as it appeared - on separate lines, whereas it is, in fact, all on one (except the If ... & End If lines).

This only becomes clear when the [ CODE ] tags are used of course ;-)
Feb 6 '14 #5
NeoPa
32,556 Expert Mod 16PB
As for the question, there have already been some pointers on how your code might benefit from certain changes, yet I don't see why it wouldn't work in your original format. Certainly ADezii's suggestion is cleaner and to be recommended, but that's not to say yours shouldn't have worked.

In Access there's a nice Eval() function that gives a back-door to using SQL's In() construct, where you can specify a list of values, but it seems Excel doesn't support that one :-( (At least if it does the new versions obscure it well with their quite attrocious new web-based help systems - urghh).
Feb 6 '14 #6
@NeoPa - actually Luk3r's code is now different, the original used AndAlso which after it didn't work I did some research and realised (as he clearly did!) that AndAlso isn't a function in VBA...

I have made a workaround, but it's not pretty - I basically just said:
'Hide everything' then 'unhide these specific sheets'.

I'll have a play now with these other ideas and see which works best for me - I'd much rather a nice clean version!

Thanks for the tips everyone!
Feb 6 '14 #7
Ok, so Luk3r's new code partially works, but ONLY the 'Allocations' sheet is showing up, not any of the 'Or' sheets...

The code from ADezii does the trick very nicely, however. Think I'll go with that as it's MUCH neater than my current code! Thanks!
Feb 6 '14 #8
NeoPa
32,556 Expert Mod 16PB
I'm afraid Luk3r does seem to have a habit of posting "guessware". Unfortunate really. Their level of knowledge seems fairly basic ATM it seems.
Feb 7 '14 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: andrelamonde | last post by:
After hours of searching, I can only find posts that tell me I'm coding this situation correctly. I'm looking for more eyes to review this code! My Code (I've tried many listed variations of...
10
Dököll
by: Dököll | last post by:
I don't want you nice guys to waste your too much of your time, and confused me in the process. I am hoping you can explain it to me like I am 2 years old, that's it. Hoping to get a handle...
7
by: daveeboi | last post by:
..I have been strugling to get this part of my site working correctly even though I can't see anything wrong with my code. I am trying to search a database and display paged results. But everytime...
2
by: audiokarate | last post by:
I have like atleast 15 errors that say class, interface, or enum expected and I have no idea what that means. Can someone tell me what it is and how I might be able to fix it? Thank you. - Manny...
1
by: jonnyothan | last post by:
The following code isn't producing expected results: class Selectable { bool IsSelected() const; }; class Unit : public Selectable { };
0
by: kirthi kumari | last post by:
i donot find any errors,but for almost 18lines of the code am getting an error saying "class,interface or enum expected error".please look at the code below package NQ; import...
2
by: ngeywa | last post by:
I am new in java and need your assistance. i have the code below which keep throwing error "class or interface expected". public class Candidates { String candidateName; String...
7
by: shathaaaa | last post by:
hi every body here i try to run program written in java (client server chat Application ) but when i try to compile it i get this error " RMIChatServerImpl.java.java:6: calss,interface,or...
0
by: supriyakhankar | last post by:
Hello friends, I want to implement the ECDH key exchange algorithm. I have the code but i am getting the error as class or interface expected. please help me. which packages i need to add and how...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.