I'm running a Worksheet_Change sub that checks if the changed cell is in a specific column and then autofits that column.
Public Const startCol = 7 '(declared in a separate module)
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column = startCol Then
Target.Columns.AutoFit
End If
End Sub
My problem is, this macro is only run if something is put in the cell or the cell's value changes, not if the cell's value is cleared or the cell is deleted entirely.
Try this code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column = startCol Then
Columns(startCol).AutoFit
End If
End Sub
The Target is a range, you can then check if the range's value is equal to "" empy string ->
If Target.Value2 = "" Then MsgBox ("Emptied cell " & Target.Address)
Here is a kind-of solution, if you are wanting worksheet changes in column 7 AND deletion of contents in column 7 (via the delete key) then this kinda might be reasonable:
In ThisWorkbook section
Private Sub Workbook_Open()
Application.OnKey "{DELETE}", "ColFit"
End Sub
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Application.OnKey "{DELETE}"
End Sub
In a module
Sub ColFit()
Select Case TypeName(Selection)
Case "Range"
Selection.ClearContents
Worksheets("Sheet1").Columns("G:G").AutoFit
Case "ChartArea"
ActiveChart.Parent.Delete
Case "PlotArea"
ActiveChart.Parent.Delete
End Select
End Sub
On the Worksheet
Private Sub Worksheet_selectionChange(ByVal Target As Range)
Worksheets("Sheet1").Columns(startCol).AutoFit
End Sub
save and reopen, now some more bases are covered ;)
also consider(in ThisWorkbook):
Private Sub Workbook_WindowActivate(ByVal Wn As Window)
Application.OnKey "{DELETE}", "ColFit"
End Sub
Private Sub Workbook_WindowDeactivate(ByVal Wn As Window)
Application.OnKey "{DELETE}"
End Sub
which lets you work with multiple books without issues
Related
I want to run a macro on a specific sheet, in my case the sheet is called "Tablet".
If a cell value in "Tabelle1" changes, I want to run this macro in the "Tablet" sheet.
Code in my Tabelle1:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$C$2" Then
Call Delete_OptionB1
End If
End Sub
This part works.
Macro Code:
Sub Delete_OptionB1()
'
' Delete_OptionB1 Makro
'
With Worksheets("Tablet")
.Range("M2:AD2").Select
Selection.ClearContents
End With
End Sub
This wont do the job. Any suggestions how I get this working?
In your code using a with block
With Worksheets("Tablet")
.Range("M2:AD2").Select
Selection.ClearContents
End With
You are selecting .Range("M2:AD2").Select but then clearing the contents of the selection on whatever sheet may be active when you Delete_OptionB1. Change to include a . - .Selection.ClearContents.
Even better, get rid or the With...End With and Select altogether. A single line will do it all:
Sub Delete_OptionB2()
'
' Delete_OptionB1 Makro
'
Worksheets("Tablet").Range("M2:AD2").ClearContents
End Sub
Instead of …
Target.Address = "$C$2"
… better use the Application.Intersect method to make it work if Target is more than one cell (this can happen when you copy/paste a range):
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Target.Parent.Range("C2")) Is Nothing Then
Delete_OptionB1 'you don't need the Call statement
End If
End Sub
If Delete_OptionB1 is not in a public module but in a workbook use eg Tablet.Delete_OptionB1
Make Delete_OptionB1 public, and avoid using .Select and Selection. (also see How to avoid using Select in Excel VBA)
Public Sub Delete_OptionB1() 'make it public
ThisWorkbook.Worksheets("Tablet").Range("M2:AD2").ClearContents
End Sub
Place this in the Tabelle1 worksheet code area:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("$C$2")) Is Nothing Then
Application.EnableEvents = False
Call Delete_OptionB1
Application.EnableEvents = True
End If
End Sub
Place this in a standard module:
Sub Delete_OptionB1()
'
' Delete_OptionB1 Makro
'
With Worksheets("Tablet")
.Range("M2:AD2").ClearContents
End With
End Sub
I'm triyin to achieve that Excel "says" the value of a cell when placing on it.
I've got this code but i'm able to hear cells' value only after i select another one. Can you help me?
Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = True
Set Target = ActiveCell
Application.Speech.Speak (Target)
End Sub
If you want Excel to "say" whatever you enter in a cell, first run this short macro, then make entries:
Sub WhatDidYouEnter()
Application.Speech.SpeakCellOnEnter = True
End Sub
and if you want Excel to "say" whatever you have Selected, then the Event Macro:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
With ActiveCell
If .Value <> "" Then Application.Speech.Speak (.Value)
End With
End Sub
will do the trick.
Try this out:
Option Explicit
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Debug.Print Target.Address
End Sub
Then change it to speak, and not to debug. :)
Can we have Worksheet_Change & Worksheet_SelectionChange in the same worksheet.
If Yes what is the precedence?
Thank you
Jean
Sure you can:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Debug.Print Target.Row
End Sub
Private Sub Worksheet_SelectionChange(ByVal myTarget As Range)
Debug.Print myTarget.Address
End Sub
The selection change happens first, since you change the selection before changing a cell.
You could test this super easy by sticking the following in your worksheet's VBA:
Private Sub Worksheet_Change(ByVal Target As Range)
MsgBox "Change"
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
MsgBox "selection change"
End Sub
You'll find that the events generally don't fire at the same time unless you enter new text in a cell and hit enter to move to a new cell. In that case, the "Change" event will fire first, then the "Selection Change" will fire.
I have this code in a standard module, which works fine:
Public Sub AutofitRows()
ThisWorkbook.Worksheets("Data").Cells.EntireRow.AutoFit
End Sub
Then I have this code in the Data worksheet module, which doesn't autofit all the rows for some reason:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$B$3" Then
Call AutofitRows
End If
End Sub
Can somebody please explain how to correct this?
Thanks.
Try below code :
Public Sub AutofitRows()
Dim rng As Range
Set rng = ThisWorkbook.Worksheets("Data").Rows("3:3")
rng.AutoFit
End Sub
Assuming your below code resides on Worksheets("Data").Also the procedure will be called when value in cell B3 will change.
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$B$3" Then
Call AutofitRows
End If
End Sub
In excel 2000, is it possible to bind a vba function to be executed when a cell is clicked with the mouse?
Found a solution:
Make a named range for the cells you want to capture clickevent
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Application.EnableEvents = False
Application.ActiveSheet.Cells(1, 1).Value = Target.Address
If Not Intersect(Target, Range("MyNamedRange")) Is Nothing Then
' do your stuff
Range("A1").Select
Endif
Application.EnableEvents = True
End Sub
You can bind to a cell double click.
Open VBA, goto the worksheet you want to wire up the event
Select WorkSheet in the dropdown on the top left and BeforeDoubleClick in the top right
The check the Target.Address is equal to the address of the cell you care about and call the function you wish.
Something like this:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Target.Address(True, True, xlA1) = "$A$1" Then
Call MyDemo
End If
End Sub
Private Sub MyDemo()
MsgBox "Hello"
End Sub