VBA Excel Worksheet Change Autofit Rows - excel

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

Related

Merging multiple Worksheet_SelectionChange

I have got no problem in running one Private Sub Worksheet_SelectionChange , but when i add multiple Worksheet_SelectionChange events it is not running. Later, i came to know that it is not possible running different worksheet selection change events in the same sheet.
I am having four different Private Sub Worksheet_SelectionChange events, trying to merge them with the help of various sites but none worked to me, as per my understanding.
Could i get some help,
1.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim cells1 As Range
Set cells1 = ActiveSheet.Range("B1:B27")
If Not (Intersect(Target, cells1) Is Nothing) Then
ActiveSheet.Range("B30").Value = Target.Value
End If
End Sub
2.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim cells2 As Range
Set cells2 = ActiveSheet.Range("C1:C27")
If Not (Intersect(Target, cells2) Is Nothing) Then
ActiveSheet.Range("C30").Value = Target.Value
End If
End Sub
3.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim cells3 As Range
Set cells3 = ActiveSheet.Range("S1:S27")
If Not (Intersect(Target, cells3) Is Nothing) Then
ActiveSheet.Range("S30").Value = Target.Value
End If
End Sub
4.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim cells4 As Range
Set cells4 = ActiveSheet.Range("T1:T27")
If Not (Intersect(Target, cells4) Is Nothing) Then
ActiveSheet.Range("T30").Value = Target.Value
End If
End Sub
I appreciate your help.
Thank you.
You can use a switch (select case) within your change event to allow options for which will occur.
Mock-up:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Row > 27 then Exit Sub
Select Case Target.Column
Case 2, 3, 19, 20
Cells(30,Target.Column).Value = Target.Value
End Select
End Sub
I have added the Exit Sub check for if the row > 27, as your ranges are 1:27, for each of the Columns. This replaces the Intersect() check.
You perform the same action based on the Target.Column, so that is the only other parameter to verify and utilize.

How to trigger code when cell value is cleared?

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

Add a value to all cells in a column in all worksheets

I have found this macro on a website and it does exactly what I need it to do with the exception of I need to copy it down for all the rows in my spreadsheet and all sheets. Can anyone help me with that?
Private Sub Worksheet_Change(ByVal Target As Range)
If Intersect(Range("A1"), Target) Is Nothing Then Exit Sub
[B1] = [B1] + [A1]
End Sub
In ThisWorkbook, put your code in the Workbook_SheetChange event to make it change when any worksheet is changed. For example:
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
'put your code here
End Sub
So for your above code, it would be this:
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
If Intersect(Range("A1"), Target) Is Nothing Then Exit Sub
[B1] = [B1] + [A1]
End Sub

Excel to voice cells' value whenever it is selected with VBA

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. :)

Excel : How do I call the "active cell" to a cell

I have Excel 2013
Currently i have a code that takes any cell i click on and shows its contents to another cell as well.
My digging through the internet came up with the code below.
How do i call this to a cell with a custom formula instead of editing the code every time i want to change the location (b15) in this case?
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
If Not Intersect(Target, ActiveSheet.UsedRange) Is Nothing Then
Range("B15").Value = Target.Value
End If
End Sub
Thank you in advance.
In a standard module include:
Public Function ShowMe() As Variant
Application.Volatile
ShowMe = ActiveCell.Value
End Function
and in the worksheet code area include:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Application.Calculate
End Sub

Resources