I'm trying to unload a userform ("DataEntry") when the active cell is not in the appropriate sheet (index number 2). I am getting a "Type Mismatch '13" after the messagebox, and I don't know why, can anyone help?
This sub is in a module, not the userform. I have tried setting up a sub in the userform and calling that (single line sub "Unload Me", but I get the same result.
Sub unloadform()
If ActiveSheet.Index <> 2 Then
MsgBox "The schedule must be selected to enter data"
Unload DataEntry
Exit Sub
End If
End Sub
Update:
I occurred to me to put this code in the userform initialize sub, (I don't want the userform to load if the correct sheet is not selected). Now I get a Runtime ErrorĀ '91'
Tried replacing Unload Me with Unload DataEntry (the name of the userform), (same result), and replacing it with Unload VBA.Userforms("DataEntry"), which throws a Runtime '13' error.
Private Sub UserForm_Initialize()
If ActiveWorkbook.ActiveSheet.Index <> 2 Then
MsgBox "Schedule must be selected to enter data"
Unload Me
Else:
'other code if correct sheet is selected
End If
End Sub
Related
In an Excel sheet, NOT on a UserForm - Trying to come up with a code to be able to drop down a combo box, i.e. to get it to open. Might use it to trigger with hover over a shape. But, get a "Compile error: Invalid use of Me keyword", and without "Me." get "Run-time error '424': Object required"
Sub ActivateCB1()
ActiveSheet.ComboBox1.Activate
Me.ComboBox1.DropDown
End Sub
Me only works in a code module attached to (eg) a worksheet or userform
Sub ActivateCB1()
With ActiveSheet.ComboBox1
.Activate
.DropDown
End With
End Sub
The VBA code below is meant to allow only certain users to tick & un-tick a checkbox. However, the problem is that if I check the box and then close the spreadsheet, when I re-open the excel file the 'tick' is no longer there. It's like the code does not save the 'tick' action. Basically, if I check the box I want that to stay like that, even after closing the spreadsheet. In the VBA code below i added ThisWorkbook.Save in order to save the "Tick" action but it simply saves the spreadsheet instead of saving the "Tick" in the check box. Could you please advise what's wrong in my code? I've asked this question before but unfortunately nobody seemed to be able to find a solution. thanks so much
Private Sub CheckBox1_Click()
If CheckBox1.Value = True Then
If (UCase(Environ("username")) = "TESTNAME") Then
'Do nothing
Else
'Uncheck because user not matching
CheckBox1.Value = False
MsgBox ("You are not authorized to tick this box.")
End If
End If
ThisWorkbook.Save
End Sub
on my sheet1 I have a commandButton (bShow) and in the code behind:
Private Sub bShow_Click()
UserForm1.Show
End Sub
then I have a UserForm named UserForm1 with a checkbox (named checkBox1) and a button named saveButton, and with a code behind:
Private Sub saveButton_Click()
Sheets(1).Cells(1, 1).Value = UserForm1.CheckBox1.Value
End Sub
with this setup in cell("A1") appears TRUE or FALSE depending on the checkbox state
i hope it helps
EDIT1:
by opening the Form reading the value from the sheet:
Private Sub UserForm_Activate()
UserForm1.CheckBox1.Value = Sheets(1).Cells(1, 1).Value
End Sub
EDIT2:
be aware of error handling (eg.: what if cell value is neither TRUE nor FALSE) But that I leave to you
hie
am looking for a vba code for controlling printing of excel sheet using activex check box in excel.
The code should only print the excel sheet when the checkbox is checked
and disable printing when unchecked.
i have tried the event below but it still prints and the msgbox is not showing
Private Sub Workbook_BeforePrint(Cancel As Boolean)
Cancel = True
MsgBox "CANNOT PRINT, check box 1 is unchecked", vbOKOnly, "Error"
End Sub
any suggestions?
You need to actually test if the checkbox is checked or not. Also, you need to place this code inside the ThisWorkbook module in the VBE.
Change sheet and checkbox name as needed:
Private Sub Workbook_BeforePrint(Cancel As Boolean)
If Sheets("Sheet1").CheckBox1.Value = False Then
Cancel = True
MsgBox "CANNOT PRINT, check box 1 is unchecked", vbOKOnly, "Error"
End If
End Sub
im having some weird things happen with some code in excel
Private Sub CommandButton2_Click()
Dim thiswb As Workbook
Set thiswb = ActiveWorkbook
Call EnterDataToSS(thiswb)
Me.Hide
BeamNoFind.Show vbModal
End Sub
the called code basically drops some values into a spreadsheet.
any left over values are populated to LISTBOX1 on BeamNoFind Userform.
Then on the BeamNoFind userform there is a button and the LISTBOX1. when you select and item from listbox1, and click the button, a third userform opens (VBMODELESS) to allow placement of the value.
below is the code of the button to show the third userform.
Private Sub CommandButton2_Click()
Dim Selected_Length As String
Dim Selected_Name As String
Dim Selected_Length_index As Integer
Selected_Length_index = BeamNoFind.ListBox1.ListIndex
With BeamNoFind.ListBox1
If .ListIndex > -1 Then
Selected_Name = .Column(0, .ListIndex)
Selected_Length = .Column(1, .ListIndex)
CellInputForm.beam_length_label.Caption = Selected_Name & " [ " & Selected_Length & " ] "
BeamNoFind.Hide
'ChngDataSrc.Hide
'Unload ChngDataSrc
CellInputForm.Show vbModeless
Else
MsgBox "No selection", vbExclamation, "Oops!"
End If
End With
End Sub
the weird thing is, when i click the button to show my modeless userform, to place the data in a cell, the initial macro is being triggered that drops you into the first userform.
Where i have the commented code 'ChngDataSrc.Hide' and 'Unload ChngDataSrc' are my attempts to stop the userform from displaying when i dont want it to.
When i unload the form, i then get an error instead of the form displaying, the error is with the initial macro:
Sub get_scheduling_data(control As IRibbonControl)
ChngDataSrc.Show
End Sub
It has something to do with vbModeless because if i replace "vbModeless" from "CellInputForm.Show vbModeless" line with "vbModal", it shows correctly, without the unwanted form (ChngDataSrc). but then the function of the form (select cell, press ok button, value placed in selected cell) is gone.
I found a solution, but its not a real solution,
i placed
ChngDataSrc.Hide in the Activate sub of the CellInputForm userform.
So when CellInputForm.show vbModeless is run, and the ChngDataSrc userform pops up unwantedly, it is then hidden again.
Id rather find out why it is being showed in the first place, but this fix seems to work for now.
I have an Excel-VBA user form which is used to get user input. I need to display this user form when an error occurs so that the user can redefine the input value.
Pseudo-code:
Sub ()
userform.Show
Call Execute
End Sub
Sub Execute()
Validate the input
If input is wrong
MsgBox "reselect the input"
-here I need to disply the userform-
End sub
I tried GoTo Userform which gives me a Label Not Defined error. Any suggestions?
I have created a UserForm :
On command button click, the code checks for the input, and if the input is wrong, it displays an error in message box and then redirects to the userform.
Private Sub CommandButton1_Click()
If TextBox1.Value = "" Then
MsgBox ("Please provide the value")
UserForm1.Show
UserForm1.Repaint
End If
End Sub
PS : Change the ShowModal property of the Userform to "False".