I've been searching and can't find what the cause of my error is.
I'm creating a VBA application in Excel to assist in processing financial reports. I have the basic functionality complete and am trying to build an Admin function to allow end users to log in and change the multiplier in the equation.
I have 2 userforms, frmLogin and frmAdmin.
frmLogin - is very basic, however when it tries to do frmAdmin.Show i get the run-time 424 error.
frmLogin
Private Sub LoginButton_Click()
If Me.txtUsername.Value = "Admin" Then
If Me.txtPassword.Value = "password" Then
Unload Me
frmAdmin.Show
Exit Sub
Else
MsgBox "Sorry, Incorrect Login Details"
Me.txtUsername.SetFocus
End If
Else
MsgBox "Sorry, Incorrect Login Details"
Me.txtUsername.SetFocus
End If
End Sub
frmAdmin
Private Sub Save_Click()
Workbook_Refresh
SaveBackup
SaveSettings
CloseForm
End Sub
Private Sub Update_Click()
civa.Value = Sheets("Settings").Range("b1")
oral.Value = Sheets("Settings").Range("b2")
End Sub
Private Sub UserForm_Initialize()
Settings.Range("b2") = civa.Value
Settings.Range("b3") = oral.Value
End Sub
Private Sub SaveSettings()
ActiveWorkbook.Save SaveChanges:=True
End Sub
Private Sub CloseForm()
Application.Goto Worksheets("Home Screen").Range("D4")
Unload Me
End Sub
WorkbookRefresh is called from Module1.
Any help will be greatly appreciated.
Steven
Settings.Range("b2") = civa.Value doesn't make sense in the UserForm_Initialize() event.
As civa is a control on the form, it doesn't have a value yet, so you can't use it to assign a value to a cell in your sheet.
Instead, I think you meant civa.Value = Settings.Range("b2")
Related
I did macro that creates a graph but need to make the file password protected.
Of course, when I protect the file, the macro will stop working.
I inserted the below into my code to unprotect the file, run the code, then protect the file again. As I the code is a function, I had to create two sub procedures, which is maybe the reason why the trick is not functioning.
Any idea how I can fix this?
Option Explicit
Sub protection()
Worksheets("Sheet1").Unprotect "abc123"
End Sub
Function (here is my function code)
End Function
Sub protection2()
Worksheets("Sheet1").protect "abc123"
End Sub
I guess you want to start one sub procedure to do the trick. My example unprotects your sheet, let the function do its magic and protects the sheet.
Option Explicit
Sub protection()
Worksheets("Sheet1").Unprotect "abc123"
Call Function (here may be values for your arguments)
Worksheets("Sheet1").protect "abc123"
End Sub
Function (here may be prameters)
the function code belongs here
End Function
On a protected sheet you can't changed cells which are locked. You may wan't to play around with the attached code:
Option Explicit
Sub SheetSetup()
Range("B3:C7").Locked = False
Range("E3:F7").Locked = True 'This is default
End Sub
Sub Sample_ProtectedSheet()
ClearValues
ChangeAllValues_on_ProtectedSheet
MsgBox ("Only values in ""B4:C7"" are set to ""yes""!")
End Sub
Sub Sample_UnprotectedSheet()
ClearValues
ChangeAllValues_on_UnprotectedSheet
MsgBox ("All values set to ""yes""!")
End Sub
Function ChangeAllValues_on_UnprotectedSheet()
Call Unprotect
On Error Resume Next
Range("B4:C7").Value = "yes"
Range("E4:F7").Value = "yes"
On Error GoTo 0
End Function
Function ClearValues()
Call Unprotect
On Error Resume Next
Range("B4:C7").Value = ""
Range("E4:F7").Value = ""
On Error GoTo 0
End Function
Function ChangeAllValues_on_ProtectedSheet()
Call Protect
On Error Resume Next
Range("B4:C7").Value = "yes"
Range("E4:C7").Value = "yes"
On Error GoTo 0
End Function
Sub Protect()
Worksheets("Sheet1").Protect "abc123"
End Sub
Sub Unprotect()
Worksheets("Sheet1").Unprotect "abc123"
End Sub
I put this code into the module page
Option Explicit
Dim correct As Boolean
Sub setCorrect()
correct = True
End Sub
Sub checkCorrectTrue()
If correct Then
MsgBox "OK"
Else
MsgBox "NO"
End If
End Sub
Then when I call these 2 subs from a sheet my variable correct never switch to True
Private Sub CommandButton4_Click()
Call setCorrect
Call checkCorrectTrue
End Sub
Because you're using Dim correct As Boolean it is only available in that module. To be able to use the variable across modules you need to declare it using Public
Try using
Option Explicit
Public correct As Boolean
Sub setCorrect()
correct = True
End Sub
Sub checkCorrectTrue()
If correct Then
MsgBox "OK"
Else
MsgBox "NO"
End If
End Sub
So, I'm kind of an amateur at VBA.
I'm doing an userform for many users to use at my company.
I need the user to pick a Date.
First, I tried with DTPicker, but that didn't work on the other computer I tried, so my collegue suggested I used a MonthView instead.
I put it in the form, dropping it from the Toolbox, and used this simple code:
Private Sub UserForm_Initialize()
MonthView1.Value = Date
End Sub
Private Sub cmdClose_Click()
Unload Me
End Sub
Private Sub MonthView1_DateClick(ByVal DateClicked As Date)
On Error Resume Next
Dim cell As Object
For Each cell In Selection.Cells
cell.Value = DateClicked
Next cell
Unload Me
End Sub
Worked perfectly.
Then I tried to open the file in another computer and got the 'some objects are not available, blah blah' message. The calendar didn't show up even though I tried registering the OCX file and stuff, and I even could put a new MonthView calendar in the form, but the "existing" one didn't load.
Sooo, I tried adding the object with code, to try and avoid this, I used this code:
Private Sub UserForm_Initialize()
Dim PruebaCal As Object
Set PruebaCal = Controls.Add("MSComCtl2.MonthView.2", "PruebaCal")
PruebaCal.Value = Date
End Sub
Private Sub cmdClose_Click()
Unload Me
End Sub
Private Sub PruebaCal_DateClick(ByVal DateClicked As Date)
On Error Resume Next
Dim cell As Object
For Each cell In Selection.Cells
cell.Value = DateClicked
Next cell
Unload Me
End Sub
This worked for the 'some objects are not available' problem, it loaded perfectly in both computers, but the Click event is not working and I have no idea how to fix that, maybe when adding controls with code the triggering events are different? I don't know.
In excel-vba i have a form named frmTemplateSelector when I select one of the two options and click on "done", the form doesn't hide!
I am calling frm.show from another module, where in depending on choice, other pop ups will appear.
Private Sub btnDone_Click()
frmTemplateSelector.Hide
Exit Sub
End Sub
Private Sub btn1_Click()
name = "XYZ"
End Sub
Private Sub btn2_Click()
name = "ABC"
End Sub
You can either Unload or Hide an UserForm.
Hide literally only hide the UF
Unload hide and reset all variables in the UF
Private Sub btnDone_Click()
Unload frmTemplateSelector 'or Unload Me
Exit Sub
End Sub
You can also change the UF's name to Me in this code, as it is the UF's code module, like this :
Private Sub btnDone_Click()
Me.Hide
Exit Sub
End Sub
I have a userform with a button on it. I want the button to "refresh" the form, in other words I want the form to end, and then reopen.
The code below won't work because the form/macro has already ended, but I want the button to perform this task or similar.
sub command1.click()
end
userform1.show
end sub
I have checked and tried most or all the options I can choose from for a userform to "refresh". Is this even possible?
Here is a very simple way. Simply Re-Initialze the Userform using the UserForm_Initialize
Private Sub UserForm_Initialize()
TextBox1.Text = "" '<~~ Just an Example
'
'~~> Put here the code to re-initialize the userform which will refresh it
'
End Sub
Private Sub CommandButton1_Click()
TextBox1.Text = "Sid"
MsgBox "Re-Initialzing the Userform"
UserForm_Initialize
End Sub
Private Sub CommandButton1_Click()
Unload Me
UserForm1.Show
End Sub