I'm trying to clear the contents of a merged cell based on the selection from a data validation list (the list is also in merged cells). And yes, "Don't use merged cells!" is great advice (I prefer "center across") but would complicate things in this case. I've tried naming the merged cells, using "MergeArea," etc. but I haven't been successful in finding a solution yet.
Below is the latest iteration I have. Please note I've been trying to make just one selection ("Yes." in this case) work before adding in the second selection (or "blank" in this case).
Private Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = False
If Range("Max_Advance").MergeArea Then
If Range.Validation.Type = 2 Then
Range("Days_Needed").MergeArea.ClearContents
End If
End If
exitHandler:
Application.EnableEvents = True
Exit Sub
End Sub
Can anyone help? I've also attached a picture to visually show what I'm working with:
Does this work for you?
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address(0, 0) = "A2" Then
On Error GoTo exitHandler
Application.EnableEvents = False
If LCase(Target.Value) <> "no. please enter." Then
Range("Days_Needed").MergeArea.ClearContents
End If
End If
exitHandler:
Application.EnableEvents = True
Exit Sub
End Sub
You can usually clear the contents of a merged cell by treating it as if it was the first cell in the merge.
for example:
Range("A2")=""
Related
There are similar posts relating to my question, but I have struggled to adapt them to my problem. I currently have the following code which works just fine:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = False
Application.ScreenUpdating = True
Set Target = Range("B4")
If Target.Value = "Yes" Then
Call DropDownListOn
Else
Call DropDownListOff
End If
Application.EnableEvents = True
End Sub
and the macros that are being called are:
Option Explicit
Sub DropDownListOn()
Sheet1.Activate
Sheet1.Range("A5").Value = "Plot default probability"
Sheet1.Range("B5").Validation.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Formula1:="Yes, No"
End Sub
and
Option Explicit
Sub DropDownListOff()
Sheet1.Range("A5").Value = ""
Sheet1.Range("B5").Validation.Delete
End Sub
Essentially what it does is as follows: Cell B4 is dropdown list. If "Yes" is chosen, then by calling the macro "DropDownListOn", it generates a new dropdown list in cell B5. This part works just fine; but, say, that once the dropdown list in cell B5 appears, I want to call another macro if B5 is chosen to be "Yes" (e.g., generating another dropdown list in cell B6). Given that only one target can be assigned to each sheet, this does not seem to be very straightforward. There seem to be suggestions for ways around this obstacle on Stackoverflow, but I struggle to adopt these to my own case. Any help and/or sample code will very much be appreciated.
Please, try the next updated event:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address(0, 0) = "B4" Then
If Target.Value = "Yes" Then
Call DropDownListOn 'it let it creating the drop-down in "B5", TRIGGERING the change event
'but delete the charts ONE BY ONE!
Else
Application.EnableEvents = False
Call DropDownListOff 'the event is not triggered here.
'if you want to also clear "B6", you should place such a code line in the above Sub...
Application.EnableEvents = True
End If
ElseIf Target.Address(0, 0) = "B5" Then
Application.EnableEvents = False
'Place here the sub able to create the drop-down validation in "B6"
'it let it creating something else, but without triggering the event
Application.EnableEvents = Trud
End If
End Sub
I have some merged cells with data validation drop down lists I tried the following method to make them automatically expand when on focus it applies on single cells, but when I tried to apply on merged cells, writing their range it doesn't work.
On focus after tabbing of Excel drop down, automatically show list for selection
first code
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
On Error GoTo Err1:
If Target = Range("d10") Then
Application.SendKeys ("%{UP}")
End If Err1:
'do nothing End Sub
[it worked perfectly][1]
when I merged cells it doesn't work, even I tried to change range to all merged cell it doesn't work either.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
On Error GoTo Err1:
If Target = Range("d10:n10") Then
Application.SendKeys ("%{UP}")
End If Err1: 'do nothing End Sub
Any idea how make it work?
Thanks in advance.
Merged cells lead to all kinds of problems and should be avoided. You could use
If Target.Address = "$A$1:$C$3" Then
or better yet
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Intersect(Target, Range("A1")) Is Nothing Then
MsgBox "merged cells are bad for your health"
End If
End Sub
I have a macro that checks a cell once it has been changed and formats the input of that cell to an accepted format. This was done to make it easier for the user by allowing them to be able to input the data in multiple ways and have the cell come out correctly.
I have:
Private Sub Worksheet_Change(ByVal Target As Range)
Set rCells = Range("C3:C4, G9:G24, C60:C61, G66:G81")
Application.EnableEvents = False
If Not Application.Intersect(rCells, Range(Target.Address)) Is Nothing Then
Format(Range(Target.Address))
End If
Application.EnableEvents = True
End Sub
...to catch the change and then format the input to the correct format.
I want the user however to be able to leave the cell blank.
What can I add to this block to allow the user to clear a cell using DEL or BACKSPACE without triggering format()?
I have tried:
If IsEmpty(Target) Then Exit Sub
But that doesn't seem to do it.
Thank you for your help!
You can use this line to check whether all cell in Target range are empty:
If WorksheetFunction.CountBlank(Target) = Target.Cells.Count Then Exit Sub
also as I mentioned in comments
change Format(Range(Target.Address)) to Format(Target)
don't forget to change function name Format in real code to something like My_Format since there is built-in function with this name:)
Test for =""
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Value = "" Then Exit Sub
Set rCells = Range("C3:C4, G9:G24, C60:C61, G66:G81")
Application.EnableEvents = False
If Not Intersect(rCells, Target) Is Nothing Then
Format (Target)
End If
Application.EnableEvents = True
End Sub
After some googling I finally found some code where I could prevent users from placing formulas inside cells. It works great, that's until I protected the sheet. Can anyone tell me what I'm doing wrong? I'm really new to VB.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Application.EnableEvents = False
On Error Resume Next
Range("I39").SpecialCells(xlCellTypeFormulas).ClearContents
On Error GoTo 0
Application.EnableEvents = True
End If
End Sub
The entire code for my sub is as follows. I need to stop users from pasting in the cells and putting formulas in them.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Intersect(Target, Range("C26")) Is Nothing Then
Application.CutCopyMode = True
Application.EnableEvents = False
On Error Resume Next
Range("C26").SpecialCells(xlCellTypeFormulas).ClearContents
On Error GoTo 0
Application.EnableEvents = True
End If
End Sub
Here is a version that facilitates formula checking over a range of cells:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rNoFormulas As Range
Set rNoFormulas = Range("C26:I26")
If Intersect(Target, rNoFormulas) Is Nothing Then Exit Sub
If Target.HasFormula Then
Application.EnableEvents = False
Target.ClearContents
MsgBox "formulas not allowed in cell " & Target.Address
Target.Select
Application.EnableEvents = True
End If
End Sub
If you want to allow data entry in cell C26, but not formula entry, then use the Change Event:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rNoFormulas As Range
Set rNoFormulas = Range("C26")
If Intersect(Target, rNoFormulas) Is Nothing Then Exit Sub
If rNoFormulas.HasFormula Then
Application.EnableEvents = False
rNoFormulas.ClearContents
MsgBox "formulas not allowed in cell C26"
rNoFormulas.Select
Application.EnableEvents = True
End If
End Sub
If you just want to protect certain cells only, no vba code is need.
follow this step :
Open sheet that contains cells or columns that you want to protect, press ctrl while selecting those cells or column to be protect, then right click, choose format cells, choose protection tab and uncheck the locked option. those cells or column will not be locked although you have protected the sheet. default setting is all cells in the sheets is locked so you must choose which cells you want to unlock while protecting the sheet. you may record a macro if you still want to use vba. hope this help
Is there a simple way to get Excel to automatically execute a macro whenever a cell is changed?
The cell in question would be in Worksheet("BigBoard").Range("D2")
What I thought would be a simple Google inquiry is proving to be more complicated - every sample involved intersects (whatever those are) or color formatting or any other number of things that appear to be irrelevant.
Yes, this is possible by using worksheet events:
In the Visual Basic Editor open the worksheet you're interested in (i.e. "BigBoard") by double clicking on the name of the worksheet in the tree at the top left. Place the following code in the module:
Private Sub Worksheet_Change(ByVal Target As Range)
If Intersect(Target, Me.Range("D2")) Is Nothing Then Exit Sub
Application.EnableEvents = False 'to prevent endless loop
On Error Goto Finalize 'to re-enable the events
MsgBox "You changed THE CELL!"
Finalize:
Application.EnableEvents = True
End Sub
Another option is
Private Sub Worksheet_Change(ByVal Target As Range)
IF Target.Address = "$D$2" Then
MsgBox("Cell D2 Has Changed.")
End If
End Sub
I believe this uses fewer resources than Intersect, which will be helpful if your worksheet changes a lot.
In an attempt to find a way to make the target cell for the intersect method a name table array, I stumbled across a simple way to run something when ANY cell or set of cells on a particular sheet changes. This code is placed in the worksheet module as well:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Cells.Count > 0 Then
'mycode here
end if
end sub
In an attempt to spot a change somewhere in a particular column (here in "W", i.e. "23"), I modified Peter Alberts' answer to:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Target.Column = 23 Then Exit Sub
Application.EnableEvents = False 'to prevent endless loop
On Error GoTo Finalize 'to re-enable the events
MsgBox "You changed a cell in column W, row " & Target.Row
MsgBox "You changed it to: " & Target.Value
Finalize:
Application.EnableEvents = True
End Sub
I was creating a form in which the user enters an email address used by another macro to email a specific cell group to the address entered. I patched together this simple code from several sites and my limited knowledge of VBA. This simply watches for one cell (In my case K22) to be updated and then kills any hyperlink in that cell.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim KeyCells As Range
' The variable KeyCells contains the cells that will
' cause an alert when they are changed.
Set KeyCells = Range("K22")
If Not Application.Intersect(KeyCells, Range(Target.Address)) _
Is Nothing Then
Range("K22").Select
Selection.Hyperlinks.Delete
End If
End Sub