Vba to mark Check Box if specific cells contains specific imput - excel

i need to create check box to be checked only if another specific cell contains specific input and unchecked if this input is not existing, noting that i tried below code
Private Sub Worksheet_Ca()
Dim DestS As Worksheet
Set DestS = ThisWorkbook.Worksheets("rest")
Dim DestSh As Range
Set DestSh = DestS.Range("B2")
If DestSh.Value = "Device" Then
DestS.CheckBoxes("Reset").Value = xlOn
Else
DestS.CheckBoxes("Reset").Value = xlOff
End If
End Sub
Also the check box i used is part of the forms toolbar not activeX and i changed the name of the check box to 'reset'
The error i got using debug is
method of checkbox of object _worksheet failed

Please, copy the next event code in the worksheet "rest" code module:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("B2")) Is Nothing Then
If Target.value = "Device" Then
CheckBoxes("Reset").value = True
Else
CheckBoxes("Reset").value = False
End If
End If
End Sub
When you change the "B2" cell value in "Device", the check box will be checked. If you change its value in something else, the check box will be unchecked...
In order to copy the code in that sheet module, please activate sheet "rest" execute right click on the sheet name, choose View Code and copy the above code in the window which opens...

Related

Get clipboard value for data validation

I have an Excel file that are used by end users, where, in a specific range of cells, if a change is made, a change event macro is triggered.
What I do with this macro is to check if the last action is any type of pasting.
What I need is to, somehow, get in a variable the content the user has copied (clipboard?) and then execute a function or procedure which checks the validy of the data. If it's correct, it will paste the values mantaining the conditional format, and if wrong it will undo the operation and disable the events.
So far, I think I am close to have everything but I am missing the knowledge to get in VBA the clipboard content in a variable.
I would appreciate general feedback as well.
PD: I have stated range(B:B) to keep it simple, in reality I will have a function for each column because the validation changes based (but that's on me,I just need to have 1 correct in order to replicate the logic).
Private Sub Worksheet_Change(ByVal Target As Range)
lastAction = Application.CommandBars("Standard").FindControl(ID:=128).List(1)
If Left(lastAction, 5) = "Paste" Then
If Not Application.Intersect(Target, Range("B:B")) Is Nothing Then
validation (Application.Intersect(Target, Range("B:B")))
End If
Else
End If
End Sub
Function validation(cell) As Boolean
Dim check As Boolean
check = Application.WorksheetFunction.VLookup(cell, MDM.Range("AK2:AK86"), 1, False)
If check = True Then
ActiveSheets.PasteSpecial Paste:=xlPasteAllMergingConditionalFormats
Application.CutCopyMode = False
Else
With Application
.EnableEvents = False
.Undo
End With
Application.EnableEvents = True
End If
End Function
I need to do this validation because if the user paste the value from another excel, it will remove both the conditional formatting and the data validation for that column.
I've used the clsClipboard class described at the following link
http://www.la-solutions.co.uk/content/CONNECT/MVBA/MVBA-Clipboard.htm
Copy the VBA class module code to a file named clsClipboard.cls, then import new Class module into your project.
Usage:
Sub test()
Dim CB As New clsClipboard
Dim myVar As String
CB.SetText "this is a test"
myVar = CB.GetText()
Debug.Print myVar
Set CB = Nothing
End Sub

Disable a specific form control button from running its assigned macro even if it is clicked

I would like to be able to prevent a specific button ("Button 4925") from running its assigned macro even when it is clicked. Basically, when you click it, it would either do nothing or show a message that says " This is an essential item that cannot be deleted"
In other words, I would like to be able to exit the sub only if the clicked button is in cell A12. Otherwise, run the code as normal. I don't know how to do that considering that I am a very beginner in VBA.
Some information:
The button is a form control button. Not an Active X one. It gets copied and pasted by another macro on the sheet. The assigned macro is written under a Standard Module.
The assigned macro function is to delete a relative range of rows. Here is the code:
Sub Delete_Button()
' Delete_Button Macro
' Step 1: Select the cell under the clicked button
Dim r As Range
Dim s As Object
Set r = ActiveSheet.Buttons(Application.Caller).TopLeftCell
r.Select
' Step 2: delete all buttons relative to the selected cell from step 1
StartCell = ActiveCell.Offset(-5, 0).Address
EndCell = ActiveCell.Offset(0, 0).Address
For Each s In ActiveSheet.DrawingObjects
If Not Intersect(Range(StartCell, EndCell), s.TopLeftCell) Is Nothing Then
s.Delete
End If
Next s
' Step 3: delete the rows relative to the selected cell from step 1
ActiveCell.Offset(-7, 0).Rows("1:9").EntireRow.Select
Selection.Delete Shift:=xlUp
ActiveCell.Offset(-4, 0).Range("A1").Select
End Sub
You must 'tell' to the code, in a way, that it must not delete the range.
So, I would suggest you to create a Private variable on top of the module keeping the button code (in the declarations area):
Private stopButCode As Boolean
Than, you must make this variable True. Use a Check box, or a piece of code in another control to make it True.
The Button code must be adapted in a way like following:
If Not stopButCode Then
'delete whatever is to be deleted
Else
MsgBox "Deletion not allowed..."
Exit Sub
End If
Edited:
If you want the code not working only if the button will be on cell "A12", you can use adapt your code as following:
Dim r As Range
Dim s As Object
Set r = ActiveSheet.Buttons(Application.Caller).TopLeftCell
If r.Address = "$A$12" then Exit Sub
'here follows your existing code...

VBA code to hide/unhide multiple rows in another worksheet based on cell output from another worksheet

I want to use VBA code for the following: based on a cell output (either "TRUE" or "FALSE" in cell W20, which changes based on whether a user checks a box or not), I want rows to be hidden/unhidden on another worksheet called "Analysis" worksheet based on the code below. I right clicked the "Summary" tab where cell W20 and the checkbox are located -> view code -> wrote the code below, but it's not working. I am very new to VBA, please help, it would be very appreciated, thanks in advance
Sub Worksheet_Change(ByVal Target As Range)
Set Target = Range("$W$20")
If Target.Value = "FALSE" Then
Sheets("Analysis").Rows("54:55").EntireRow.Hidden = True
Else
Sheets("Analysis").Rows("54:55").EntireRow.Hidden = False
End If
If Target.Value = "FALSE" Then
Sheets("Analysis").Rows("94:95").EntireRow.Hidden = True
Else
Sheets("Analysis").Rows("94:95").EntireRow.Hidden = False
End If
If Target.Value = "FALSE" Then
Sheets("Analysis").Rows("134:135").EntireRow.Hidden = True
Else
Sheets("Analysis").Rows("134:135").EntireRow.Hidden = False
End If
End If
End Sub
Excel doesn't recognize the modification of a cell value caused by the manipulation of form controls. I assume this is because Excel links a form control to a value and applies the changes itself, therefore not considering it a user change in the spreadsheet.
In order to make your code work, you will have to move your code into a module as a click event of your form control. Click the Developer tab and then Visual Basic. Once Visual Basic is open, you will notice on the left pane that you have a list of your worksheets within a VBA Project bearing the name of your workbook and your macro is currently within one of them. Once you found it, delete the code of your macro. Then, right-click on the name of your workbook and then select Insert > Module...
On the coding pane, insert this modified code:
Sub NameOfYourFormControlCheckBox_Click()
Dim MyRange As Range
Set MyRange = Range("$W$20")
If MyRange = False Then
Sheets("Analysis").Rows("54:55").EntireRow.Hidden = True
ElseIf MyRange = True Then
Sheets("Analysis").Rows("54:55").EntireRow.Hidden = False
End If
End Sub
Make sure that the name of the sub is the name of your form control (checkbox) followed by an underscore and then "click".
Hope this helps!

Change Worksheet When a Cell Value and a Checkbox Change

I have a Workbook with multiple Sheets. I have a menu page (Worksheet) with multiple user choices (Enter a new order, update an order, etc.) Each choice has a check box beside it and depending on which check box is checked, cells F4:F21 change from 0 to 1 and, cell B1 changes to the name of the Worksheet where I want to go. I have the following VBA in the Main Menu worksheet but when I click a check box, nothing happens. Any ideas why?
CODE
Private Sub Worksheet_Activate()
ClearMenuForm
End Sub
Private Sub Worksheet_Change (ByVal Target As Range)
Dim sh As String
If Not Intersect(Target, Range("F4:F21")) Is Nothing Then
sh = Cells(1, "B").Value
Sheets(sh).Select
End If
End Sub
Clicking a check box does not activate the event Worksheet_Change (see this). That is why nothing happens.
Try changing one of the cells instead to see the effect.
What I think you want to do is assign an action to your Checkbox(es). You can do this in two ways:
Right clicking on the checkbox, and Assign Macro...
You have to create the associated macro, which will likely contain parts of the code that you already wrote, and/or calls to subs you have. You may bring the VBE (Alt+F11), insert a module in your VBA project, and write your Sub, e.g.,
Sub CheckBox1_Click()
MsgBox "Checkbox 1a has changed"
End Sub
Via VBA (e.g., this). With the sample code below, you would execute InitCBs, and that would associate CheckBox1Change with the checkbox (it actually assigns actions for both checkboxes in the figure; action for checkbox 2 is CheckBox2Change). You may also set InitCBs to be executed when opening the file.
Sub CheckBox1Change()
MsgBox "Checkbox 1b has changed"
End Sub
Sub InitCBs()
Dim cb As CheckBox
For Each cb In ActiveSheet.CheckBoxes
With cb
Dim action As String
'action = "CheckboxChange"
action = Replace(cb.Name, " ", "") & "Change"
.OnAction = action
End With
Next cb
End Sub
You've defined sh as a String. Since there is no sheet named "1", for example, your code will generate a "Subscript out of Range" runtime error. Try changing sh to a Long.
Dim sh As Long

Pop-Up Error Message (macro) - to update by checkbox selection - Excel 2010

I have a macro which is basically working as I want (alerting the user when two conflicting checkboxes are selected) - code below. The 1 in G2 is the value generated to indicate this case.
The error message fires on SelectionChange, but this appears to be only when another cell is selected by mouse. The worksheet contains a series of checkboxes for the user to select from, and the intention is for the user to only use the checkboxes, never needing to select or input directly into cells. In which case, the error message would never fire even when the scenario described has occurred.
Is there a way of having a msgbox macro trigger by the update of any checkbox on the sheet?
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Range("G2") = 1 Then
MsgBox "ERROR - Select AND Reject checked"
End If
End Sub
Also, I would like to extend the range to apply to all the cells in column G, I just can't seem to get this to work for me. I have seen a few examples citing "G:G" but I have so far only got this to work for one cell.
Apologies in advance for any glaring errors, I've used Excel for a while now - but I'm brand new to using VBA.
Mutually exclusive options are usually indicated with option buttons (also known as radio buttons) instead of checkboxes. Is there any reason you're not using option buttons for this task?
As far as calling the same code for all checkboxes, the checkboxes would have to be Form Controls (not ActiveX Controls), and you could assign them to this macro:
Sub CheckBox_Clicked()
Dim chk As CheckBox
Set chk = ActiveSheet.CheckBoxes(Application.Caller)
MsgBox chk.Name
End Sub
And lastly, for your SelectionChange event to monitor an entire column, it would look similar to this:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim ClickedCell As Range
Dim rngClicked As Range
Application.EnableEvents = False
Set rngClicked = Intersect(Columns("G"), Target)
If Not rngClicked Is Nothing Then
For Each ClickedCell In rngClicked.Cells
If ClickedCell.Value = 1 Then MsgBox "ERROR - Select AND Reject checked"
Next ClickedCell
End If
Application.EnableEvents = True
End Sub

Resources