View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
GS GS is offline
external usenet poster
 
Posts: 364
Default Commandbutton to Workbook Open

Hi Richard,

You need to entirely restructure what you're doing for it to work how you
want it to! If you're interested in how, read on:

First:
Move all the code to a standard module. Enter something like the following
in that module:


Option Explicit

'Declare the following global variable so it can be accessed from anywhere
in your code
'The button on UserForm1 will set its value to TRUE if clicked
Public bRemindMeLater As Boolean


'Put your code in its own procedure, something like this:
Sub CheckVacationDays()

Dim L As Long, M As Long
Dim Ans as Variant

L = Month(Date) * 10000 + Year(Date)
M = GetSetting("Demo", "Drafts", "Month", 0)

If L = M Then Exit Sub

If Month(Date) = 4 Then
If Day(Date) = 24 And Day(Date) < 27 Then
bReminMeLater = False '**Optional: It's default value is FASLE, but
this line ensures it!!
UserForm1.Show
End If

'Here is the solution to your problem!
'Read the value placed in the variable by the button on UserForm1
If bRemindMeLater Then GoTo InputLater

Ans = MsgBox("Do you qualify for Bank Day?", vbYesNo)
If Ans = vbYes Then
MsgBox ("1 Day has been added to your Bank Days")
SaveSetting "Demo", "Drafts", "Month", L '**Notice no parenthesis
used here!!
Worksheets("Vacation").Range("C3").Value = 1
Else
Worksheets("Vacation").Range("C3").Value = 0 '**Do you really need
this??
End If

InputLater:
End If

End Sub


Second:
Enter the following in ThisWorkbook: '**Replaces what you have now!!

Option Explicit

Private Sub Workbook_Open()
CheckVacationDays
End Sub


Third:
With UserForm1, implement some good practices for naming controls so they're
easier to associate with.
For example, you could rename CommandButton2 to: cmdRemindMeLater
Enter the following in the code behind the userform:

Private Sub cmdRemindMeLater_Click()
bRemindMeLater = True '**Tell us the user clicked the button
Unload Me '**If you want to dismiss the userform here!!
End Sub


I think this should work how you want.
Good luck!
GS