Highlight current Selection in Yellow - vba

I simply want the selected range to be yellow, and to return to colorless when it is de-selected. The code is the following:
Option Explicit
Public previouscell As Range
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Selection.Interior.Color = vbYellow
previouscell.Interior.ColorIndex = xlNone
Set previouscell = Selection
End Sub
The problem is setting previouscell in the first place. I tried putting it in Worksheet_Activate(), but it wouldn't work as soon as I opened the workbook (only when I changed the sheet, it would work great after that.)
So I tried declaring it as a public in ThisWorkbook.Workbook_open as well:
Option Explicit
Public previouscell As Range
Private Sub Workbook_Open()
Set previouscell = ActiveCell
ActiveCell.Interior.Color = vbYellow
End Sub
But it doesn't recognize the variable previouscell then, because I believe it is not transferred from ThisWorkbook (I did close and reopen the workbook before testing). Does anyone know what I need to change for this simple task?

(4th edit)
Use this workbook code, with your sheet code removed:
Option Explicit
Public previousCells As Range
Private Sub Workbook_Open()
SetSelectionYellow
End Sub
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
SetSelectionYellow
End Sub
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
SetSelectionYellow
End Sub
Private Sub SetSelectionYellow()
If Not previousCells Is Nothing Then previousCells.Interior.ColorIndex = xlNone
Set previousCells = Selection
previousCells.Interior.Color = vbYellow
End Sub

This seems to work if you place it in the code module of the sheet in question.
Sub Worksheet_SelectionChange(ByVal Target As Range)
Cells.Interior.ColorIndex = xlNone
ActiveCell.Interior.Color = vbYellow
End Sub

You're on the right track with the first code block. Just test to see if previouscell has been set to anything, and if not, set it to the current selection (or whatever you want to do).
Option Explicit
Public previouscell As Range
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If previouscell Is Nothing Then
Set previouscell = Selection
End If
Selection.Interior.Color = vbYellow
previouscell.Interior.ColorIndex = xlNone
Set previouscell = Selection
End Sub

In the ThisWorkbook.Workbook_Open sub, set previouscell to A1 (or some default cell that has no interior color). Then in the Worksheet_SelectionChange sub do your thing. You have everything correct, but you needed 2 parts:1) setting the initial default value for previouscell and 2) changing the interior of cells when the selection changes. You did part 2, but missed part 1.

Related

Limiting the code to run only when within a specific cell range

I'm trying to make a Spin Button that will edit the active cell, but only when it is within a specific cell range. I want the code to start up if the active cell is within the range of Sheet 1 cells J63:J97 and, not run if it is outside that range.
This is the code I have so far. It will edit the active cell, as needed. However, it is not limited to the range I need it to be.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
SpinButton1.Value = Selection.Value
End Sub
Private Sub SpinButton1_Change()
Selection.Value = SpinButton1.Value
End Sub
Try using Application.Intersect.
I have defined a separate Function to do the job.
This code is tested and it works:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If checkIntersection(Target, Range("J63:J97")) Then
SpinButton1.Value = Selection.Value
End If
End Sub
Private Sub SpinButton1_Change()
If checkIntersection(Selection, Range("J63:J97")) Then
Selection.Value = SpinButton1.Value
End If
End Sub
'Check if Range1 and Range2 are intersecting
Function checkIntersection(range1 As Range, range2 As Range) As Boolean
checkIntersection = Not Application.Intersect(range1, range2) Is Nothing
End Function
may check selection with Intersect
Private Sub SpinButton1_Change()
If Not Intersect(Selection, Range("J63:J97")) Is Nothing Then
Selection.Value = SpinButton1.Value
End If
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Intersect(Target, Range("J63:J97")) Is Nothing Then
SpinButton1.Value = Selection.Value
End If
End Sub

Macro doesnt appear on the List after copy-pasting code

I am completely new to VBA and hence have no idea what I'm doing...
Below is the description of my problem and also the code in question.
What I originally wanted to do was this:
Copy a clicked cell, Select new sheet, Select any cell, Paste as values
Now i found a code that apparently does the trick which is this:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Intersect(Target, Columns("A")) Is Nothing Then
Cancel = True
If Target.Row > 1 And Len(Target.Value) Then Worksheets("S11").Range("C2").Value = Target.Value
End If
End Sub
I say "apparently" as I am not able to test it. Here is the problem:
I saved the workbook as excel macro-enabled workbook
Clicked ALT Q - to go back to my workbook
I then ALT F8 to run it - but there is nothing there... blank....
what am I missing?
In a module, have the macro you wish to use.
public sub mymacro(r as excel.range)
If Not Intersect(r, Columns("A")) Is Nothing Then
If r.Row > 1 And Len(r.Value) Then Worksheets("S11").Range("C2").Value= r.Value
End If
end sub
public sub wbtest()
mymacro activecell
end sub
and use like so
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Intersect(Target, Columns("A")) Is Nothing then
mymacro target
end if
end sub

Running a different macro for a different cell only when changed manually

My problem is that the macros I wrote change the values of the cells triggering again a macro to change one of the other cells.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim cell As Range
For Each cell In Target
If Not Intersect(cell, Range("c2")) Is Nothing Then
Macro1
ElseIf Not Intersect(cell, Range("C3")) Is Nothing Then
Macro2
ElseIf Not Intersect(cell, Range("d8")) Is Nothing Then
Macro3
End If
Next cell
End Sub
The macros running always change the other cells, what makes it a endless loop at the moment.
Is there a way to only make manual input/ change of the cell let the macro run?
Two solutions for this :
Add Application.EnableEvents = False at the start of your _change event and set it to True at the end
Create a Public Boolean to test if you are already doing any update automatically
Something like this (solution 2) :
Public DisableEvents As Boolean
Private Sub Worksheet_Change(ByVal Target As Range)
If DisableEvents Then Exit Sub
DisableEvents = True
Dim cell As Range
For Each cell In Target
If Not Intersect(cell, Range("c2")) Is Nothing Then
Macro1
ElseIf Not Intersect(cell, Range("C3")) Is Nothing Then
Macro2
ElseIf Not Intersect(cell, Range("d8")) Is Nothing Then
Macro3
End If
Next cell
DisableEvents = False
End Sub
Sub Macro1()
If DisableEvents Then Exit Sub
'Rest of your code
End Sub

Run-Time error '1004' with my VBA for hiding and unhiding rows

I have code for hiding and unhiding rows in my sheet based on changing the value in my dropdown. Every time I change the dropdown I get Run-Time error of '1004'. I had a private Sub before and changed it to a Sub but that doesn't seem to be the solution.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rng As Range
Set rng = Target.Parent.Range("L6")
If Target.Count > 1 Then Exit Sub
If Intersect(Target, rng) Is Nothing Then Exit Sub
Application.Run "dynamic_hide"
End Sub
Sub dynamic_hide()
If Target.Range = "$S$9:$S$51" Then
If Target.Range = 0 Then Rows("F9:T51").EntireRow.Hidden = True
If Target.Value <> 0 Then Rows("F9:T51").EntireRow.Hidden = False
End If
End Sub
You have a few problems going on here:
First, the default property of a Range object is Value, so Target.Range = "$S$9:$S$51" will always be false. Use Target.Address instead.
Second, don't use Application.Run to call Subs from the same VBProject. Use Call instead.
Third, you've not let the sub dynamic_hide know what Target is since Target is only a parameter of the Worksheet_Change event subroutine. You can solve this by declaring your sub like Sub dynamic_hide(ByVal Target As Range) And then you can use it: Call dynamic_hide(Target)
Lastly, since Target is a range you don't need to use Target.Range since Target is a range so you can simply omit every .Range from Target.Range Target.Parent.Range is fine.

Create Temporary Excel Chart

Sub aaGraphing()
'
' aaGraphing Macro
'
'
Range("L948:W949,D948:D949").Select
Range("D949").Activate
ActiveSheet.Shapes.AddChart.Select
ActiveChart.ChartType = xlLineMarkers
ActiveChart.SetSourceData Source:=Range( _
"Analytics!$L$948:$W$949,Analytics!$D$948:$D$949")
End Sub
This code creates a chart of the data I want. Is there a way to way to make the created chart temporary so that when you click anywhere outside of the chart it deletes?
You could use the SelectionChange event of the worksheet to delete the chart. Below I am assuming there is only one shape - the chart - that might be on the worksheet.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Me.Shapes.Count = 1 Then
Me.Shapes(1).Delete
End If
End Sub
It is possible (I believe) to dynamically attach this event, and remove it. However, I believe that it is a little complicated.
An alternative might be to use Application.OnTime to delete it after a period of time.
Application.OnTime Now + TimeValue("00:00:40"), "ProcedureToDelete"
will run the procedure named 'ProcedureToDelete' after 40 seconds. In this procedure you might want to make sure the the selection is in the worksheet, not in the chart that you are about to delete.
You could, in the timed-procedure, check to see if the chart is currently selected. If not, delete it, otherwise set the timer again.
Regular module....
Option Explicit
Public PlotName As String
Public PlotRange As Range
Sub Tester()
AddPlot ActiveSheet.Range("B3:B7,D3:D7")
End Sub
Sub AddPlot(rng As Range)
With ActiveSheet.Shapes.AddChart
PlotName = .Name
.Chart.ChartType = xlLineMarkers
.Chart.SetSourceData Source:=Range(rng.Address())
End With
Set PlotRange = rng
Application.EnableEvents=False
rng.Select
Application.EnableEvents=True
End Sub
Sub RemovePlot(rng As Range)
If Not PlotRange Is Nothing Then
If Application.Intersect(rng, PlotRange) Is Nothing Then
On Error Resume Next
rng.Parent.Shapes(PlotName).Delete
On Error GoTo 0
End If
End If
End Sub
Sheet code module:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
RemovePlot Target
End Sub