Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,510
Default If statement works here, case doesn't


Try the following

Select Case InStr(sWsName, "Monthly")
Case Is 0
'Code here if "Monthly" does exist in name
Case 0
'Code here if "Monthly" NOT in name
End Select
Next ws

You could also use Case Else in lieu of Case 0 or leave it out altogether if
you don't need any code for sheet names that do NOT contain Monthly.

--
Regards,

OssieMac


"salgud" wrote:

The code below uses a Sheet_Change event to allow or not allow other
changes to the sheet. It applies to some sheets in the workbook, but not to
others. The Select Case statement determines which is which and bypasses
the process of looking up a password for the appropriate sheets.

I tried to write a case statement to circumvent the process for all sheets
with the word "Monthly" in their title, but couldn't make it work. I
finally put the entire Case loop inside a If statement and it works fine.

' PUBLIC VARIABLES FOR THIS MODULE ARE DECLARED IN
MODPASSWORDCHANGE
Private Sub Workbook_SheetChange(ByVal sh As Object, ByVal Target As Range)
'Activated by a change in any worksheet in the workbook - gets password
from user
' verifies password, and unprotects worksheet

Dim vResponse As Variant
Dim sWsName As String

Set wsPwrdNames = ThisWorkbook.Sheets("Passwords")
Set rShNames = wsPwrdNames.Range("ShNames")
Set rPwrdEnt = wsPwrdNames.Range("bPwrd")

If rPwrdEnt.Value = "True" Then Exit Sub 'EXIT
sWsName = ActiveSheet.Name

If InStr(sWsName, "Monthly") = 0 Then < CAN THIS BE DONE WITH ANOTHER CASE?

'If the sheet name is "Totals" or "(Code Key)" skip to end of sub
Select Case sWsName

'If the spreadsheet name contains "monthly", skip the password code
Case Is = "TOTALS", "(Code Key)", "Provider Wtg"
Application.EnableEvents = False

Case Else
Set rFoundShName = rShNames.Find(sWsName, _
LookIn:=xlValues, _
LookAt:=xlWhole)

If rFoundShName Is Nothing Then
MsgBox "There is no password listed for this sheet!",
vbExclamation, "Missing Password"
GoTo Errhndlr 'EXIT
End If

wsPwrdNames.Visible = True

PwrdForm:

ufPwrdEntry.Show

If sPwrd = rFoundShName.Offset(0, 1).Value Then
bPwrdEntrd = True
Application.EnableEvents = False
rPwrdEnt.Value = bPwrdEntrd
Application.EnableEvents = True
Else
vResponse = MsgBox("Incorrect Password! Click OK to try again,
Cancel to exit", _
vbOKCancel)

If vResponse = vbCancel Then

Errhndlr:
ufPwrdEntry.Hide
Application.EnableEvents = False
On Error Resume Next
Application.Undo
On Error GoTo 0
Application.EnableEvents = True
wsPwrdNames.Visible = False
End 'EXIT
Else
GoTo PwrdForm
End If
End If

wsPwrdNames.Visible = False
End 'EXIT
wsPwrdNames.Visible = False
Application.EnableEvents = False
On Error Resume Next
Application.Undo
On Error GoTo 0
Application.EnableEvents = True
Application.ScreenUpdating = True
End Select
End If
End Sub

So is this an instance where a Case statement isn't appropriate, or am I
just mis-applying it?
.

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can this be done as a Case statement? salgud Excel Programming 9 September 24th 09 03:31 PM
VBA Case statement Ken[_4_] Excel Programming 4 January 26th 09 10:07 PM
Case Statement jlclyde Excel Discussion (Misc queries) 3 December 4th 08 05:04 PM
Case Statement Help stck2mlon Excel Programming 3 June 2nd 04 01:44 PM
Case statement smi Excel Programming 2 October 18th 03 02:20 PM


All times are GMT +1. The time now is 05:26 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"