Erase a cell by pressing a button in Excel - excel

I'm working in Excel. I'd like to press a button to cancel the value of the cell at the left of the button itself.
So the user experience should be the following:
When the user presses "button1", the cell at its left became 0. The same for "button2" and "button3"
How can I do?
By using a Macro?

Assign this macro to any and all buttons, and it'll delete the info. in the cell directly to the left.
Sub test()
Dim btnRow&, btnCol&
btnRow = ActiveSheet.Shapes(Application.Caller).TopLeftCell.Row
btnCol = ActiveSheet.Shapes(Application.Caller).TopLeftCell.Column
Cells(btnRow, btnCol).Offset(0, -1).ClearContents
End Sub
Or, thanks to #Rory, this can be:
Sub test()
Activesheet.Shapes(Application.Caller).TopLeftCell.offset(0,-1).ClearContents
End Sub
Note: Make sure your shapes are well placed, as this uses wherever the top left of the shape is to determine the row/column. This macro reduces the need to run three different ones, depending on where, and minimizes any If/Then type statements as it uses the Caller to determine which shape, which determines where it is.

May this help...
'Add three buttons on the sheet
'And imaging that you want to delete B4 B5 B6
'You can discover which button is, by double cliking on the desing mode
'on your excel
Private Sub CommandButton1_Click() 'This is the button on the cell C4
Range("B4").ClearContents
'Here B4 is the range of the cell you want to delete...
'This could be acomplish just pressing the button DELETE
'Same by the two other buttons... just adding 1 to the number
'B4...B5...B6
'With this you only can delete de contents of the cell...
End Sub
Private Sub CommandButton2_Click() 'This is the button on the cell C5
Range("B5").ClearContents
End Sub
Private Sub CommandButton3_Click() 'This is the button on the cell C6
Range("B6").ClearContents
End Sub

Hyperlink method is what I've done before. Like it, because it look like it can click.
Add below code on the Worksheet module, when a hyperlink clicked, it will trigger the sub
Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)
If LCase(Target.Range.Text) Like "delete*" Then
ActiveCell.Offset(0, -1).ClearContents
End If
End Sub
You can use below code to generate as much as hyperlink you want.
Sheets(XXX).Hyperlinks.Add Anchor:=Selection, Address:="", SubAddress:= _
"", TextToDisplay:="Delete"

If your using command button
Option Explicit
Sub Button1_Click()
Range("A1") = 0
End Sub
or assign to worksheet
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Intersect(Target, Range("B1")) Is Nothing Then _
Range("A1") = 0
End Sub

Related

Move the cursor to the right next cell, rather than the cell underneath

This is what I want to achieve with the code further down: If I use the RETURN key, the cell right next to the current cell should be selected, rather than the cell underneath. When I execute the code (i.e. I change between two different worksheets, the cursor moves from the current selected cell to the right next one. When then I use RETURN, I get the message as in the attached image: "Cannot run the macro 'PathToMacro\MacroName'!TRUE: The macro may not be available in this workbook or all macros may be disabled." What I want to achieve is this:
Change the behaviour of the RETURN key so that not the underneath cell, but the cell right to the currently selected cell is selected.
Please help with this. Thank you.
"Cannot run the macro 'PathToMacro\MacroName'!TRUE: The macro may not be available in this workbook or all macros may be disabled."
Option Explicit
Sub getNextCell()
activeCell.Offset(0, 1).Activate
End Sub
Private Sub Worksheet_Activate()
Dim activeCell As Range
Set activeCell = Selection
Application.OnKey "{RETURN}", Cells(activeCell.row, activeCell.column + 1).Select
End Sub
Private Sub Workbook_Open()
Application.OnKey "{RETURN}", Cells(activeCell.row, activeCell.column + 1).Select
End Sub
Private Sub Worksheet_Deactivate()
Application.OnKey "{RETURN}"
End Sub
This is the answer to my question:
The procedures are specific to the sheet.
As the sheet is entered, the cursor moves to the right
As the sheet changes to another sheet, regular behaviour is restored!
' move cursor to the right upon ENTER key
Private Sub Worksheet_Activate()
Application.MoveAfterReturnDirection = xlToRight
End Sub
' return to default behaviour
Private Sub Worksheet_Deactivate()
Application.MoveAfterReturnDirection = xlDown
End Sub

Calling a Macro from workSheet by Double Clicking in a cell

I want to call a Macro (Rpt1) in Module1 by double-clicking a cell in Sheet2. I'm not certain how to do this. I seem to have the double click event executing from Sheet2 but I don't know how to call the macro I have in Module1.
You need to paste this code into your SHEET code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Const clickADDRESS As String = "A1" '<--- or whatever cell you want it to be
If Not Intersect(Target, Me.Range(clickADDRESS)) Is Nothing Then
Call Rpt1
End If
End Sub
which will call the macro in module code (my example)
Sub Rpt1()
MsgBox "This worked"
End Sub
By sheet code here's an illustration:

How to "display an InputBox" everytime I press a specific cell in excel/vba?

I'm trying to pop-up inputBox when I press a specific Cell in Excel/VBA.
I tried to do a while loop with all the information about every cell stored in an array.
This is easy with Worksheet Selection Change
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Address = "$A$2" Then
InputBox ("HELLO")
End If
End Sub

VBA Code to autohighlight modified cells?

Currently using this code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Target.Address = "$B$1" Then Range("B9:AE53").Interior.Color = xlNone
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
Dim c As Range
If Not Intersect(Target, Range("B9:AE53")) Is Nothing Then
For Each c In Intersect(Target, Range("B9:AE53"))
Target.Interior.Color = vbYellow
Next c
End If
End Sub
Autohighlight only works when I manually edit (or F2 then enter) the cells in B9:AE53. I was hoping for something that would change the cell color if I edit the data in the orders sheet (reference for B9:AE53).
Was also hoping to transfer the event from B1 to a command button.
You can easily change the second part of your question, the one where you wish to reset the cell colors to nothing, to be executed by a button.
Change the code to this:
Sub CleanUp()
Range("B9:AE53").Interior.Color = xlNone
End Sub
Then add a button via the Forms Control Button in the developer toolbar and asign the above macro to it. This will clean up the area. Once you specifiy what the desired outcome is for the first part of you question I might be able to help with that.

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

Resources