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

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

Related

Function to return true if a cell in a range is red

I am attempting to get my function "ScanColor" to return True when any cell in the range "lockdown" has the interior color red.
If a cell in the range "lockdown" does have a cell that is red i'd like to prompt a msg box.
Thank you.
Private Sub Worksheet_Change(ByVal Target As Excel.Range)
If ScanColor(Range("lockdown")) = True Then
MsgBox "You have an invalid cell!"
End If
End Sub
Private Function ScanColor(Cells As Range) As Boolean
Dim cell As Range
For Each cell In Cells
If cell.Interior.ColorIndex = 3 Then
ScanColor = True
Exit For
End If
Next
End Function
A change from conditional formatting must be determined from the DisplayFormat property.
Private Sub Worksheet_Change(ByVal Target As Excel.Range)
If ScanColor(Range("lockdown")) Then
MsgBox "You have an invalid cell!"
End If
End Sub
Private Function ScanColor(Cells As Range) As Boolean
Dim cell As Range
For Each cell In Cells
If cell.displayformat.Interior.ColorIndex = 3 Then
ScanColor = True
Exit For
End If
Next
End Function
There is some question as to whether the Worksheet_Change will be triggered. You might want a Worksheet_Calculate instead.

Multiple cell value change to trigger macros

I have different set of values in cells G3:G4 and D15:D10000 in a single sheet. I want run two separate codes when G columns or D columns are changed. How I can identify which set of columns are changed?
out this in your worksheet code pane
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("G3:G4")) Is Nothing Then
'code when some cell in range "G3:G4" is changed
ElseIf Not Intersect(Target, Range("D15:D10000")) Is Nothing Then
'code when some cell in range "D15:D10000" is changed
End If
End Sub
Put the code below in your relevant worksheet, in the Worksheet_Change event.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim WatchRange As Range
Dim IntersectRange As Range
Set WatchRange = Application.Union(Range("G3:G4"), Range("D15:D10000"))
Set IntersectRange = Intersect(Target, WatchRange)
If Not IntersectRange Is Nothing Then
Select Case Target.Column
Case 4 ' column D
Call A
Case 7 ' column G
Call B
End Select
End If
End Sub
Below are examples of Sub A and Sub B:
Sub A()
MsgBox "Running Sub A"
End Sub
Sub B()
MsgBox "Running Sub B"
End Sub

Get row number on Cell Change in VBA

I know how to check to see if a column has changed, like so
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("H5")) Is Nothing Then
'Pop up a message saying H5 has changed
End If
End Sub
If I want to change another column on the same row, I can do this
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("H5")) Is Nothing Then
Range("A5").Value = "Look at me!"
End If
End Sub
Now what if I want to achieve the above, but for all columns in the range of row1 to the end of the table? Something like this (note, I know this wouldn't work)
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("H1:H")) Is Nothing Then
Range("A" & Target.row).Value = "Look at me!"
End If
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("H:H")) Is Nothing Then
Range("A" & Target.row).Value = "Look at me!"
End If
End Sub
Though you should note that Target can be a multi-cell range if more than one cell is updated in the same operation.
This may be safer:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim c As Range
For Each c in Target.Cells
If Not Intersect(c, Range("H:H")) Is Nothing Then
'3 ways to accomplish the update
Range("A" & c.Row).Value = "Look at me!"
c.EntireRow.Cells(1).Value = "Look at me!"
Cells(c.Row, 1).Value = "Look at me!"
End If
Next c
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

Highlight current Selection in Yellow

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.