• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Alter VBA code

Jet Fusion

Member
Hi :)

Please can someone assist

in changing part of a code to show the following:
1. To have a password with letters and number

Change part of a code to show the following in the pop dialog:
2. Greeting message with - Current user name and & - Good morning (<12:00) / Good afternoon (>12:00) depending on time of day.
3. Day of week & Date & Tim.

Below part of the code which shows the password & pop dialog once logging in

Code:
If user <> "" And Not IsNumeric(user) And Code <> "" And IsNumeric(Code) Then
For Each PName In Sheet2.Range("H8:H108")
If PName = CLng(Code) And PName.Offset(0, -1) = user Then
MsgBox "Welcome Back: – " & user
AddData.Value = user
AddData.Offset(0, 1).Value = Now
'Add usernmae to the worksheet
Current.Value = user


Thanks in advance
Jet
 
Hi Jet, happy to meet you again:),
try this code:
Code:
If user <> "" And Not IsNumeric(user) And Code <> "" Then
For Each PName In Sheet2.Range("H8:H108")
If PName = Code And PName.Offset(0, -1) = user Then

If Right(Now, 8) < "12:00:00" Then
MsgBox user & " - Good Morning - " & Format(Now, "dddd dd/mm/yyyy hh:mm")
Else
MsgBox user & " - Good Afternoon - " & Format(Now, "dddd dd/mm/yyyy hh:mm")
End If

AddData.Value = user
AddData.Offset(0, 1).Value = Now
'Add usernmae to the worksheet
Current.Value = user
 
Hey Keetoowah

Happy to meet you again too and much appreciate your help you are :awesome:

Thank you will give it a try and let you know. Thanks and have a good one ;)
 
no great advantage; you can shorten:
Code:
If Right(Now, 8) < "12:00:00" Then
MsgBox user & " - Good Morning - " & Format(Now, "dddd dd/mm/yyyy hh:mm")
Else
MsgBox user & " - Good Afternoon - " & Format(Now, "dddd dd/mm/yyyy hh:mm")
End If
a bit to:
Code:
MsgBox user & " - Good " & IIf([mod(now(),1)] < 0.5, "Morning", "Afternoon") & " - " & Format(Now, "dddd dd/mm/yyyy hh:mm")
 
Hi

I can't seem to get the "To have a password with letters and numbers" to work I tried what you had @Keetoowah unless it's something else in the rest of the code preventing it that i'm not picking up.

@p45cal could we add evening aswell, say after 18:00, please.

Code:
MsgBox user & " - Good " & IIf([mod(now(),1)] < 0.5, "Morning", "Afternoon") & " - " & Format(Now, "dddd dd/mm/yyyy hh:mm")
 
Code:
MsgBox user & " - Good " & Application.Index([{"Morning","Afternoon","Evening"}], Application.Match([mod(now(),1)], [{0,.5,.75}])) & " - " & Format(Now, "dddd dd/mm/yyyy hh:mm")
 
I can't seem to get the "To have a password with letters and numbers" to work ...
Hi Jet Fusion,

I believe the problem is not in the code, please note that the password must match the one set somewhere in your file. The only change I made to your code was the elimination of the control that forces you to enter only numbers - And IsNumeric(Code)
 
@Keetoowah Hi again :DD

I took some of the code from Sample

If you could please change and highlight which I need to change so the code will accept - maybe if we can letters, numbers and special characters, that would be :awesome:

Also I noticed that with this code it does not keep sheets hidden when the file is opened it shows what the last user had opened, any ideas how to fix that. I do have hyperlinks on the sheets. What should happen is that when a user opens the file it should have the login form and DashBoard (which currently) works but it should hide all others sheets until that user logs on and only the sheets that use is allowed to access should show.

Basically as per the link I sent but I am not sure if it's the Hyperlinks and Hidden sheets not playing nice? The rest of the stuff in the code I have altered and it works.

Code:
Option Explicit
Private Trial As Long
Private Sub cmdCheck_Click()
'Declare the variables
Dim AddData As Range, Current As Range
Dim user As Variant, Code As Variant
Dim PName As Variant, AName As Variant
Dim ws As Worksheet, ws2 As Worksheet, ws3 As Worksheet
Dim result As Integer
Dim TitleStr As String
Dim msg As VbMsgBoxResult

'Variables
user = Me.txtUser.Value
Code = Me.txtPass.Value
TitleStr = "Password check"
result = 0
Set Current = Sheet2.Range("O8")

'Error handler
On Error GoTo errHandler:
'Destination location for login storage
Set AddData = Sheet2.Cells(Rows.Count, 2).End(xlUp).Offset(1, 0)
'Check the login and passcode for the administrator
If user <> "" And Not IsNumeric(user) And Code <> "" And IsNumeric(Code) Then
For Each AName In Sheet2.Range("T8:T108")
'If AName = Code Then 'Use this for passcode text
If AName = CLng(Code) And AName.Offset(0, -1) = user Then ' Use this for passcode numbers only
MsgBox "Welcome Back: – " & user & " " & Code
'record user login
AddData.Value = user
AddData.Offset(0, 1).Value = Now
'Add usernmae to the worksheet
Current.Value = user
'Change variable if the condition is meet
result = 1
'Unload the form
Sheet2.visible = True
Sheet2.Select
Unload Me
'Show the navigation form
'frmNavigation.Show
Exit Sub
End If
Next AName
End If

'Check user login with loop
If user <> "" And Not IsNumeric(user) And Code <> "" And IsNumeric(Code) Then
For Each PName In Sheet2.Range("H8:H108")
'If PName = Code Then 'Use this for passcode text
If PName = CLng(Code) And PName.Offset(0, -1) = user Then ' Use this for passcode numbers only
MsgBox "Welcome Back: – " & user & " " & Code
'record user login
AddData.Value = user
AddData.Offset(0, 1).Value = Now
'Add usernmae to the worksheet
Current.Value = user

'unhide worksheet for user
If PName.Offset(0, 1) <> "" Then
Set ws = Worksheets(PName.Offset(0, 1).Value)
ws.visible = True
End If

'unhide worksheet for user
If PName.Offset(0, 2) <> "" Then
Set ws2 = Worksheets(PName.Offset(0, 2).Value)
ws2.visible = True
End If

'unhide worksheet for user
If PName.Offset(0, 3) <> "" Then
Set ws3 = Worksheets(PName.Offset(0, 3).Value)
ws3.visible = True
End If

'show sheet tab if hidden
ActiveWindow.DisplayWorkbookTabs = True

'Change variable if the condition is meet
result = 1
'Unload the form
Unload Me
'Show the navigation form
'frmNavigation.Show
Exit Sub
End If
Next PName
End If

'Check to see if an error occurred
If result = 0 Then
'Increment error variable
Trial = Trial + 1
'Less then 3 error message
If Trial < 3 Then msg = MsgBox("Wrong password, please try again", vbExclamation + vbOKOnly, TitleStr)
Me.txtUser.SetFocus
'Last chance and close the workbook
If Trial = 3 Then
msg = MsgBox("Wrong password, the form will close…", vbCritical + vbOKOnly, TitleStr)
ActiveWorkbook.Close False
End If
End If
Exit Sub
'Error block
errHandler:
MsgBox "An Error has Occurred " & vbCrLf & "The error number is: " _
& Err.Number & vbCrLf & Err.Description & vbCrLf & _
"Please notify the administrator"
End Sub



Much appreciated :cool:;) sorry for the long message o_O
 
look for comments in the code:
'Use this for passcode text
and:
' Use this for passcode numbers only
(Anything after the first apostrophe in a line of code makes everything after that apostrophe a comment (so it isn't executed).)
At the beginning of these lines there may or may not be an apostrophe.
All you need to do is to remove the very first apostrophe (only) from any line with the comment Use this for passcode text at the end of it
and add an apostrophe at the very beginning of any line with the comment Use this for passcode numbers only at the end.

I can see 2 instances of this in the code - there may be more.
 
Hi @p45cal

Thanks for your reply, ok I went thro, I did not change the following line of code as per @Keetoowah

Code:
If user <> "" And Not IsNumeric(user) And Code <> "" Then

Thanks guys, hopefully I can get the sheet thing sorted then I'm done with my project.

Much appreciate the help :awesome::awesome:
 
Yes I tried it and it gave me an error then when I changed it, it wouldn't allow only numbers, I had to change it to either text or text and a number. Not sure how to do a create password button for the user on the login form, maybe an update password, and also to log it, will search around on here if someone has had this query.

@p45cal I did notice you also can't use special characters, is there a way to add this?

Thanks again :awesome:;)
 
Last edited:
Could you attach a redacted/de-sensitised workbook so I can see what's going on?
Confirm also that, for the moment, you're not looking for the user to add/change a login and password.
 
Yes not adding the add/change password now.

I did the same as the sample file as per link above post #11 I didn't copy the message box changes that all works fine, just to have a look at the special characters with number and letters for now.

Thanks a lot :cool:
 

Attachments

  • Template-Login-Check Test .xlsm
    52.7 KB · Views: 3
Last edited:
Could you confirm that you want to allow letters and numbers in both user and password fields?

If not, then specify what you want to allow in both these fields.
 
No the user name will be letters only, but the password should be mixed of special characters, numbers and letters.
 
OK, what the passwords and codes are, is determined only by what's in the Login sheet put there manually by you, an administrator. This is where you determine what combinations of letters/characters/special characters are allowed.
There are 2 sets of usernames and passcodes; one for admins (where all the sheets are made visible) and one for non-admins where only up to 3 sheets are made visible.
The admins username/passwords start at S8:T8 of the Login sheet, and the non-admins username/passwords start at G8:H8 (where you can add up to 3 named sheets you want to make visible in the 3 cells to the right).

I've put my username of p45cal in both as an admin and as a non-admin. If I login with the passcode 111 I'm an admin, if I enter instead the passcode abc12&3 I'm a regular non-admin user.

You can delete the Interface sheet altogether.

See how you get on with the attached.
 

Attachments

  • Chandoo46076Template-Login-Check Test .xlsm
    51.9 KB · Views: 8
Ok cool that works great, :awesome:

I redid this file for you and it works with sheets hiding but in my file it doesn't hide the sheets when someone else logs in that doesn't have access to certain sheets :eek:o_O.

I will have to redo my file will go thro this code and see where I went wrong, and amend it according to this as this file works as it should.

Thank you once again, that's me for today, but not for good :p
 
but in my file it doesn't hide the sheets
You need to be looking at the VisibleFalse macro:
Code:
Sub VisibleFalse()
Dim ws As Worksheet
GetSheets
'loop through each sheet
For Each ws In Worksheets
  'use the case statement to exclude sheets by sheet name
  Select Case ws.Name
      'add the name of the sheets to exclude:
    Case "Interface"    ',"January"
      'do nothing
    Case Else
      ws.visible = xlVeryHidden    'hide the sheet
  End Select
Next ws
End Sub
and checking that it gets called:
Code:
Private Sub Workbook_Open()
GetSheets
VisibleFalse
Showme
End Sub
Check also that the VisibleTrue macro isn't being called inappropriately elsewhere.
Otherwise, send me the file.
 
Back
Top