Combining vba

bptaw

Board Regular
Joined
Feb 27, 2017
Messages
69
Office Version
  1. 2016
Platform
  1. Windows
I have some vba code associated with a login form, password and expiry date:

Private Sub cmdLogin_Click()

Dim user As String
Dim password As String
user = Me.txtUserID.Value
password = Me.txtPassword.Value

If [ISREF('ExpiryDate'!A1)] Then
If Date > Sheets("ExpiryDate").[A1] Then
MsgBox "This Trial version has now expired and the Workbook will close."
ActiveWorkbook.Close SaveChanges:=False
Else: MsgBox "This is a trial version and will expire on " & Sheets("ExpiryDate").[A1]
End If
Else
Worksheets.Add.Name = "ExpiryDate"
Sheets("ExpiryDate").[A1] = Date + 10
Sheets("ExpiryDate").Visible = xlVeryHidden
MsgBox "This is a trial version and will expire on " & Sheets("ExpiryDate").[A1]
End If

Application.ScreenUpdating = False

If (user = "admin" And password = "admin") Or (password = "tsmith") Then

Unload Me
' Unhide all worksheets in the workbook
Worksheets("TS").Visible = xlSheetVisible
Worksheets("Cost Your Hemp").Visible = xlSheetVisible
Worksheets("Maize").Visible = xlSheetVisible
Worksheets("Summary Page").Visible = xlSheetVisible
Worksheets("Water Calculations").Visible = xlSheetVisible


' Hide, Unhide a specific shape by name
Dim ws As Worksheet
Set ws = ActiveSheet ' Change to the desired worksheet
ws.Shapes("Login2").Visible = False
On Error GoTo 0 ' Reset error handling

Sheets("Cost Your Hemp").Select
Cells(2, 3).Select

Application.ScreenUpdating = True
Else

MsgBox "Invalid login credentials. Please try again."

End If

End Sub

I also have vba code in the ThisWorkbook to delete sheets when the date has expired. I think these could be combined, but I'm not sure how to do it.

Private Sub Workbook_Delete_Sheets()
Dim oneSheet As Worksheet
Dim workingNames As Variant, oneName As String
Dim lastDay As Date

lastDay = Sheets("ExpiryDate").[A1]

Select Case Date
Case Is < lastDay
Rem OK
GoSub HideHidden
Case Is = lastDay
Rem warning
MsgBox "This is the last day that you can use this worksheet"
GoSub HideHidden
Case Else
Rem delete stuff
GoSub KillSheets
End Select
Exit Sub
KillSheets:
ThisWorkbook.Sheets("Getting Started").Visible = xlSheetVisible
Application.DisplayAlerts = False
For Each oneSheet In ThisWorkbook.Sheets
If oneSheet.Name <> "Getting Started" Then
oneSheet.Visible = xlSheetHidden
oneSheet.Delete
End If
Next oneSheet
Application.DisplayAlerts = True
ThisWorkbook.Save
Return
HideHidden:
For Each oneSheet In ThisWorkbook.Sheets
If oneSheet.Name <> "Getting Started" Then
oneSheet.Visible = xlSheetVisible
End If
Next oneSheet
ThisWorkbook.Sheets("Getting Started").Visible = xlSheetVeryHidden
Return
End Sub


Any help?
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
I think these could be combined, but I'm not sure how to do it.
Why do you think they should be (or need to be) combined into one?
I don't think that is necessary. People often separate their code into multiple procedures to make it easier to maintain and allow for easy re-use for specific tasks.

If you want to, it is easy to call one procedure from the other. It is quite common.
See: Calling Sub and Function procedures (VBA)
 
Upvote 0
Solution

Forum statistics

Threads
1,215,076
Messages
6,122,984
Members
449,092
Latest member
Mr Hughes

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