Is there any way to block cell without block list?
Ex: I have in cell A1 the list:
Brazil
USA
Ireland
If I block the cell using "protect sheet"
I can't select the list
If I block on VBA using the code application.cutcopymode = false
It works, but if the person paste from a notepad, for example, it doesn't work
Is there any way to block it?
Private Sub Worksheet_SelectionChange(ByVal Target as Range)
If intersect(Target, range("A1")) is nothing then Exit Sub
Application.CutCopyPaste = False
End sub
I don't think you can do so (curious if I'm wrong).
What you could do however:
1) Developers > Insert > Combobox
2) Align Combobox with cell A1
3) Assign the appropriate list and a linked cell
4) On the Protection tab, select the Locked check box
5) On linked cell make sure you deselect the Locked check box
6) Protect your worksheet
Pasting values in cell A1 no longer possible, users deleting/moving/shaping your combobox is disabled AND you can still use the validation list :)
Related
I'm sorry this question may look like antoher ones, but I am new in VBA and struggle to get a proper code...
I would like to protect some data after they were verified. For exemple, I have data in Column B (or whatever column) and I check them in comparaison to manuscrit raw data. I would like to have a cell in that column where I say "Yes" to testify this was checked. After entering "Yes", I would like that all cells in the colum become locked.
I've found a code to lock an entire row (Lock rows in Excel using VBA), but whatever i try, i'm not able to modifiy it to work for a variable entire column (only to lock specific column, I'm not able to lock the column where "Yes" is typed ...)
Could someone help me ?
Thanks !
Maybe this solution can help:
Step 1: select all cells in a sheet. Goto properties (cell format) -> security and deaktivate the checkbox for lock cells (see picture below).
Step 2: protect the sheet with a password. In my example I used "billytalent".
Step 3: copy the following code into the code-area of the sheet. Therefore open the Visual Basic Editor. On the left side you will find a list with your sheets. Double click on the sheet where you want to lock cells with entry "yes". Copy the procedure into the code-area.
Private Const PASSW As String = "billytalent"
Private Sub Worksheet_Change(ByVal Target As Range)
Dim cell As Range
For Each cell In Target
'only do something, if input is in row 2
If cell.Row = 2 Then
'only do something, if someone write yes in a cell
If cell.Value = "yes" Then
'deaktivate the protection of the sheet
ActiveSheet.Unprotect Password:=PASSW
'lock the cells in the column
ActiveSheet.Columns(cell.Column).Locked = True
'activate the protection
ActiveSheet.Protect Password:=PASSW, userinterfaceonly:=True, AllowFiltering:=True, AllowFormattingCells:=True, AllowFormattingRows:=True, AllowFormattingColumns:=True
ActiveSheet.EnableOutlining = True
End If
End If
Next
End Sub
You can tweak the code you're looking at there a little and I think it will do what you want. Make sure before you start this that you set all the cells on the sheet to unprotected.
This is set to check row 3 of the sheet for Yes. Change the value on the second line if you need to.
Private Sub Worksheet_Change(ByVal Target As Range)
row_to_check = 3 ' Checking row 3 for "Yes"
If Intersect(Target, Me.Rows(row_to_check)) Is Nothing Then Exit Sub ' exit early if row 3 wasn't changed
Me.Unprotect ' unprotect the sheet
For Each r In Target.EntireColumn.Columns ' cycle through each row that has seen a change
r.Locked = r.Cells(row_to_check, 1).Value = "Yes" ' set it to protected if the second cell on that row is "Yes"
Next
Me.Protect ' reprotect the sheet
End Sub
I'm attempting to add items to an Excel Combobox, based on the value of an independent cell. So what I need is an IF statement, obviously.
So it should look something like this, (note: C1 is the independent cell):
IF C1 = "3"
AddItem "One"
AddItem "Two"
ELSE IF C1 = "4"
AddItem "Three"
.etc.
The problem is that I don't know how to properly link the ComboBox such that it knows when the independent cell value has changed to trigger a clearing of existing items in the ComboBox and repopulation of new items.
This code needs to go on the sheet with your target cell C1
Every time your cell C1 is changed, it will force the code you insert to execute. In this case, it will force an update to your dropdown list.
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Application.Intersect(Range("C1"), Range(Target.Address)) Is Nothing Then
Application.EnableEvents = False
'YOUR CODE HERE
Application.EnableEvents = True
End If
End Sub
Edit 1: (How to add items to a combo box?)
Add an ActiveX Control Combo Box
Developer Tab > Insert > ActiveX Controls > ComboBox
You can then refer to your Combo box inside your loop as follows:
Combobox1.Clear 'To clear
ComboBox1.AddItem "Text" 'To Add
I have a cell on Sheet1 that contains a drop-down list, let's say N3. The items in the drop down change, depending on the value in J3. At start, both cells are blank. Enter data in J3, and the drop down populates in N3. If I clear the contents of J3, the drop down in N3 is now empty, but the last selected value (if one was selected), still appears as a 'ghost' entry. It's a ghost entry to ME because it is old data, but I do understand the software is doing as designed. If J3 is cleared of contents, how do I get N3 to be cleared of that last selection? I am not VBA trained, but dangerous enough to handle it if that's what's needed to accomplish this. thanks!
You may consider to use the worksheet_change event.Put the below code in sheet1 code module.
Private Sub Worksheet_Change(ByVal Target As Range)
On Error GoTo err_rout
Application.EnableEvents = False
If Not Intersect(Range("J3"), Target) Is Nothing And Target.Value = vbNullString Then
Range("N3").Value = vbNullString
End If
err_rout:
Application.EnableEvents = True
End Sub
I have 5,000 part numbers contained on one sheet of an Excel workbook (Part Numbers), ranging from cell A1:A5000. I want to find a way to click on any one of those part numbers and have it automatically populate into cell D7 on another sheet of the same workbook (Price Sheet). What is the easiest way to accomplish this task?
To do it that way, you will have to write VBA code to catch every SheetSelectionChange event to see if the new selection range is in your cells A1:A5000. And then if it is, execute the VBA code to fill OtherSheet!D7.
If I recall correctly, the VBA code to do this would look something like this:
Private Sub WorkSheet_SelectionChange(ByVal Target As Range)
'Check if it is in the range A1:A5000
If Target.Column = 1 And Target.Row <= 5000 Then
'get the target worksheet
Dim TargetWS As Worksheet
Set TargetWS = Sheets("OtherSheetsName")
'copy the value of the just selected cell to D7 on the target WS
TargetWS.Cells(7, 4).Value = Target.Value
End If
End Sub
(Oops, forgot about the need for "SET" in VBA.)
You can do this without VBA:
Select the partnumbers A1:A5000 and type PartNumbers in the Name Box (to the left of the formula bar) and press carriage return (OartNumbers should now be visible in the Name Box whenever you select a1:a5000
Now go to cell D7 on Price Sheet, and use Data Validation-->List and enter =PartNumbers in the Source box
Now you can select any of the 5000 part numbers from the dropdown in cell D7
Hello: I have a set of cells on a worksheet called "Docs". The cell range is (B13:C23).
When users get taken to this page, they are meant to fill out each of these cells with a value from 0 through 6. My Question: Is there some code that I can attach to this sheet where, if a user does not fill in a cell with anything (ie. leaves it blank) and tries to leave the sheet or close the workbook, they are somehow reminded to fill it in? Or is there a way to not let them leave the sheet until it's completed? Thanks.. Allan
You can attach a macro to the change event of the form. Excel comes with built in validation but it does not work that well. For instance if someone pastes a value into the cell it does not validate what is pasted.
Start by creating a range by selecting the range of cells to be validated, right click and select "Name a Range". Note that I am testing this with Excel 2007. Say you call your range "InputRange".
Then open the VBA editor and create a procedure for the change event.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim vrange As Range, cell As Range
Set vrange = Range("InputRange")
If Intersect(vrange, Target) Is Nothing Then Exit Sub
For Each cell In Intersect(vrange, Target)
If cell.Value < 1 Or cell.Value > 6 Then
MsgBox "Invalid Entry", vbCritical
Application.EnableEvents = False
cell.ClearContents
cell.Activate
Application.EnableEvents = True
End If
Next cell
End Sub
Note you can attach to any event that suits you.
You could give these cells conditional formatting, making them red if empty.
Try writing a vba macro. Alt + F11 opens the VB Editor. Check out this SO post for VBA tutorials.
There are worksheet and workbook events that you can use. For example, Workbook_BeforeClose or Workbook_SheetChange. If you create methods for those events you can put code inside that checks that the required cells are filled.