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
Related
I've created a userform which has a number of conditional settings which should apply.
I have x number of labels, all refering to a given cell value in worksheet 2.
I have everythijng working properly, however I need to re-run the UserForm to apply changes to the label values. Now, I'm using
Me.Repaint
at the end of the UserForm code.
I've added the folowing code to VBA mODULE "ThisWorkbook":
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
Dim ws2 As Worksheet
Set ws2 = ThisWorkbook.Sheets("ws2")
If Target.Address(False, False) = ws2.Cells("C8").Value Then
MsgBox "Value Changed!"
End If
End Sub
However I can not get this to work. I can get it work by only refering to a single cell in the first worksheet:
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
If Target.Address(False, False) = "A2" Then
MsgBox "Value Changed!"
'repaint userform!
End If
End Sub
I would of course replace the messagebox with a Me.Repaint or something similar to repaint my form if any value changes.
Is there a guru here, which could help me resolve this issue? I want my userform Label caption values to update if the value of cell C8 in ws2 changes.
The below code was the result of debugging, and answered my question. Thanks to #Dean for helping out.
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
If Sh.Name = "ws2" Then
If Target.Address(False, False) = "C8" Then
MsgBox "Value Changed!"
End If
End If
End Sub
The code below worked fine so long as the "MainSubroutine" only needed to be triggered when cell G14 was clicked on, but now I need to widen the trigger event to whenever any target cell on the sheet is clicked on and it has the specific string, "Click to Learn More".
So how do I write that? I tried altering line 2 to "If Selection.Count = 1 AND selection.value = "Click to Learn More" Then..." but obviously that didn't work.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Selection.Count = 1 Then
If Not Intersect(Target, Range("G14")) Is Nothing Then
Call MainSubroutine
End If
End If
End Sub
This should do it? If it can contain the string use Instr or similar.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If target.Count = 1 Then
If Target.value="Click to learn more" Then
Call MainSubroutine
End If
End If
End Sub
i have few sheet, and a need for once of it that a specific cell (A1) is equal "1" the label color become green once the change their value.
I declare in a ThisWorkBook the following function:
Public Function ColorLabel(LabelName)
Set Foglio = Sheets(LabelName)
Set Target = Foglio.Range("A1")
If Target = "1" Then
Foglio.Tab.ColorIndex = 4
Else
Foglio.Tab.ColorIndex = xlNone
end if
End Function
So, in every sheet i define the following code
Private function Worksheet_Change(ByVal Target As Range)
ColorLabel(ActiveSheet.CodeName)
end function
but I get the following error
Compilation error. Expected variable or routine and not form
What's wrong?
Someone help me?
Thanks!
Your Public Function ColorLabel(LabelName) should be placed in a Module, not inside one of the Sheets or the Workbook. Usualy we place there code that is related only to Sheets events or Workbook events.
see image for Public Function ColorLabel(LabelName) code
Your Private function Worksheet_Change(ByVal Target As Range) should be in the Workbook_SheetChange event (in the Workbook module):
Code:
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
ColorLabel (Sh.Name)
End Sub
You could improve your code, by not calling ColorLabel function on every change made to any cell in any sheet, by checking if the Target is inside Range("A1"), see code below:
Improved Code:
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
' call the ColorLabel function only if value in Cell A1 was modified
If Not Intersect(Target, Range("A1")) Is Nothing Then
ColorLabel (Sh.Name)
End If
End Sub
If the function is defined in the ThisWorkbook module, you should call it this way:
ThisWorkbook.ColorLabel Me.Name
and by the way, as it is written, ColorLabel shoud be a Sub not a function (it does not return anything).
However, better design would be:
move the sub to an independent code module
or
rename the sub into an event handler using Workbook_SheetChange
Why do you need to pass the name of the worksheet at all? Just pass a reference to the worksheet itself:
Public Function ColorLabel(Foglio As Worksheet)
Dim Target As Range
Set Target = Foglio.Range("A1")
If Target.Value = "1" Then
Foglio.Tab.ColorIndex = 4
Else
Foglio.Tab.ColorIndex = xlNone
End If
End Function
'If this is the event handler you want...
Private Function Worksheet_Change(ByVal Target As Range)
ColorLabel Me
End Function
'Or in ThisWorkbook:
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
ColorLabel Sh
End Sub
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
I am trying to write a macro where changing any column
should automatically save worksheet.
My Excel sheet expands till G25.
I tried this but its not working:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Target.Worksheet.Range("G25")) Is Nothing Then
ActiveWorkbook.Save
End Sub
I have save it under ThisWorkBook.
Any help is appreciated.
Under ThisWorkbook that handler is called Workbook_SheetChange and it accepts two arguments, Sh (of type Object) and Target (a Range). So, your code won't work there.
If you put your code in the sheet you want (Sheet1, for instance) instead of in the ThisWorkbook, put the End If and change the range to "A1:G25" (representing a square from row 1 column A to row 25 column 25), it should work.
This did:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Target.Worksheet.Range("A1:G25")) Is Nothing Then
MsgBox "changed"
End If
End Sub
For completeness, under ThisWorkbook, this will work:
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
If Not Intersect(Target, Target.Worksheet.Range("A1:G25")) Is Nothing Then
MsgBox "changed"
End If
End Sub
The other common reason why an Event doesn't work is that EnableEvents has been set to False
I would code your problem like this as
typically you need to work with the range of interest that is being tested. So creating a variable rng1 below serves both as an early exit point, and a range object to work with. On a sheet event Target.Worksheet.Range("A1:G25") will work but it is lengthy for it's actual use
If you do have a range to manipulate then making any further changes to the sheet will trigger a recalling of the Change event and so on, which can cause Excel to crash. So it is best to disable further Events, run your code, then re-enable Events on exit
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rng1 As Range
Set rng1 = Intersect(Target, Range("A1:G25"))
If rng1 Is Nothing Then Exit Sub
Application.EnableEvents = False
'work with rng1
Application.EnableEvents = True
End Sub