I've got a piece of VBA code for Excel Macros which prevents users to input text in the Russian language and it works, however, it throws out an error when copying/pasting a number of cells. I think that I have to limit what Intersect applies to but not sure what is the right way to do so.
Private Sub Worksheet_Change(ByVal Target As Range)
If Intersect(Target.Cells, [A1:H1000]) Is Nothing Then Exit Sub
If LCase(Target.Cells) Like "*[а-я]*" Then
With Application
.EnableEvents = False: .Undo: .EnableEvents = True
MsgBox "Please use latin letters only"
End With
End If
End Sub
Related
I am new to VBA and am quite stuck at using Application Enable.Events function. I tried using the Application Enable.Events function to understand it better but the code that I have generated is not producing the results that I am expecting. So far my code is as follows -
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Application.EnableEvents = False
ThisWorkbook.Save
Application.EnableEvents = True
End Sub
Because Application.EnableEvents is being set to false, nothing happens if I double click on the cells in Excel. My understanding is that double clicking any cells will not impact any code between line 2 and 4. Please correct me if my understanding is wrong.
However, if I comment out the Application.EnableEvent as follows ...
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
'Application.EnableEvents = False
ThisWorkbook.Save
'Application.EnableEvents = True
End Sub
My expectation is that this would allow the workbook to save itself every time a cell is being double clicked on. However, that is not the case. If I type "? Application.EnableEvents" inside the immediate window, it would give me false.
I am suspecting that I am missing a small step to update the vba code. It would be awesome to use simple words for any explanations as I am fairly new to coding and vba.
Thank you everyone!
So a multifaceted problem:
I have created a training request sheet that I have now been asked to split into different excel sheets for different requests
Using the VBA below I am able to move 1 option to a new sheet but when try to add an additional condition, by simply reproducing the same vba for each variable, it fails.
Private Sub Worksheet_Change(ByVal Target As Range)
If Intersect(Target, Range("E:E")) Is Nothing Then Exit Sub
With Application
.EnableEvents = False
.ScreenUpdating = False
End With
If Target = "Head and Neck" Then
Target.EntireRow.Copy Sheets("HN").Cells(Sheets("HN").Rows.Count, "A").End(xlUp).Offset(1)
Rows(Target.Row).Delete
End If
With Application
.EnableEvents = True
.ScreenUpdating = True
End With
End Sub
Also when the automation works it is deleting the cells that are assigned with the dropdown menu. Is there anyway to create to maintain the dropdown whilst deleting the input data?
Many Thanks
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")=""
I want to disable cell editing (direct typing into cell) but want to update that cell through code without protecting worksheet
Does anyone have any idea?
In the worksheet's code module:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Range("A1:A10"), Target) Is Nothing Then
Application.EnableEvents = False
Target.ClearContents '// Assuming you want to keep it blank
Application.EnableEvents = True
End If
End Sub
Then in your code whenever you want to change a value, just disable events before hand:
'// Will be deleted
Range("A5").Value = "TEST"
'// Will not be deleted
Application.EnableEvents = False
Range("A5").Value = "TEST AGAIN"
Application.EnableEvents = True
I had a similar issue and found a workaround. For whatever reason I couldn't get my macros to run correctly with the sheets protected so what I did is as a code to Pop up a message box for any cells that I didn't want the user to change. Then I added "Application.DisplayAlerts = False" to the beginning of any codes that needed to modify those cells and reset the alerts back to True at the end of those codes.
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