VBA to move cell selection - vba

I need a VBA to move the cell selection down one row/cell in column E everytime cell A6 changes. please help! thank you!
I already have a VBA code to keep a cell selection in the same cell if A5 changes. is it possible to have two of these VBA change codes in the same sheet?

The answer to your question is yes. You just need to add criteria on which change event to capture. For example, something like below will work for you:
Private Sub Worksheet_Change(ByVal Target As Range)
On Error GoTo forward
Application.EnableEvents = False
If Not Intersect(Target, Me.[A5]) Is Nothing Then
'/* your code to keep selection */
ElseIf Not Intersect(Target, Me.[A6]) Is Nothing Then
'/* your code to move cell selection down */
End If
forward:
Application.EnableEvents = True
End Sub

Related

VBA is not updating automatically after input

I have created a Excel workbook for my work where I collect information. In this workbook I have a sheet where details in various currencies can be filled in. Based on the selection by the preparer the value in cell B5 will change to either USD or LC. In case the value in cell B5 will be USD, columns C and E should be hidden. The issue in here is that this code will not immediately unhide the columns. After clicking on a random cell, the columns are hidden. Please let me know if there is a solution for this issue whereby the columns are hidden without clicking on a random cell each time. Thank you.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Range("B5").Value = "USD" Then
Union(Columns("C"), Columns("E")).EntireColumn.Hidden = True
ElseIf Range("B5").Value = "LC" Then
Union(Columns("C"), Columns("E")).EntireColumn.Hidden = False
End If
End Sub
Thank you all for your comments. Let me further elaborate on my issue. In principle my VBA code works. The only thing is that after cell B5 is changed to USD, initially nothing happens. After I click on a random cell in this sheet, the VBA code works and hide the columns. The same is applicable in case the value in cell B5 is LC. Then again the VBA code does not work immediately. After clicking on a random cell in the sheet the columns are unhided.
A small update with respect to cell B5. So cell B5 contains a formula that is linked to listed cell in another sheet. After a value is selected from the list in another sheet, cell B5 will determine through the IF functions if the value in B5 will be LC or USD.
I am now afraid that after the preparer select in the listed cell a value, it will not click on a random cell in designated sheet resulting that he or she will see the wrong information.
Please let me know if you require further information. Thank you.
PS. I am not very strong in creating VBA codes.
You're using the wrong event.
SelectionChange fires when you select a different cell, etc.
Change fires immediately after a cell's contents change.
Private Sub Worksheet_Change(ByVal Target As Range)
If Range("B5").Value = "USD" Then
Union(Columns("C"), Columns("E")).EntireColumn.Hidden = True
ElseIf Range("B5").Value = "LC" Then
Union(Columns("C"), Columns("E")).EntireColumn.Hidden = False
End If
End Sub
Alternate Solution:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target = Range("B5") Then Union(Columns("C"), Columns("E")).EntireColumn.Hidden = (Target.Value = "USD")
End Sub
(adapted from #Vityata's comment below)
More Information:
MSDN : Worksheet.SelectionChange Event (Excel)
MSDN : Worksheet.Change Event (Excel)
A few changes to your code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$B$5" Then
If Range("B5").Value = "USD" Then
Union(Columns("C"), Columns("E")).EntireColumn.Hidden = True
Else
Union(Columns("C"), Columns("E")).EntireColumn.Hidden = False
End If
Application.Calculate
End If
End Sub
1: the right event is Change (when a value changes in the sheet), not SelectionChange (when the selection changes).
2: Your code works on my side, so I guess it's not working on your side because you have some calculation off. I've added the line Application.Calculate which will refresh the spreadsheet even if your calculations are set to manual.
3: I've added an If Target.Address condition to check only when the cell that changed is the right one.
4: I've added just Else instead of ElseIf cell = "LC", because it's faster and because I think it's cleaner (if the user removes completely the value of the currency and before it was USD, nobody will unhide the columns C and E).
Keep it simple,
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim rng As Range
Set rng = Range("B5")
If rng.Value = "USD" Then Columns("C:E").EntireColumn.Hidden = True
If rng.Value = "LC" Then Columns("C:E").EntireColumn.Hidden = False
End Sub

Excel/VBA changing date when there are changes to the workbook

is it possible in Excel to insert today's date into a Workbook/sheet only when the data inside the sheet is changed.
So what I mean by this is that there is a workbook with several sheets and if somebody makes changes to this sheets I want to have my date updated that is in a specific cell. For better understanding lets just say that this cell is C5 of the first worksheet.
Thank you in advance
Try the code below:
Private Sub Worksheet_Change(ByVal Target As Range)
' check that the cell changed is not "C5"
If Intersect(Target, Range("C5")) Is Nothing Then
Application.EnableEvents = False
Range("C5").Value = Date
End If
Application.EnableEvents = True
End Sub

unlock specific area in a protected excel sheet with vba

I have to unlock a specific range (D6:BC116) in a excel sheet. It should be able for other people editing this specific area. So it should be unlocked for them.
At first I manually protect the whole sheet without any code. And after that I want to unprotect the specific area for editing. But something always goes wrong. I have these two codes. The first code has the hidden property it only hides empty cells. The other code I am trying to unprotect specific area I want to edit after protecting the whole sheet.
I am not really sure if the problem is in the first code because of the hidden property? And I am not sure if they are in a relation?
Private Sub Worksheet_Change(ByVal Target As Range)
For Each cell In Range("B6:B112")
If cell.Value <> "" Then
cell.EntireRow.Hidden = False
Else
cell.EntireRow.Hidden = True
End If
Next cell
End Sub
Sub UnlockCells()
Worksheets("Sheet1").Range("D6:BC116").Locked = False
Worksheets("Sheet1").Protect
End Sub
And when I execute this I always get "Index out of range"
Thanks!
I think you need to unprotect before unlocking.
Like this:
With Worksheets("Sheet1")
.Unprotect "MyLongAndSecurePassword"
.Range("D6:BC116").Locked = False
.Protect
End with
Concerning the first part of the code - make sure that you use a variable, which is not named cell, because cell is used by the VBEditor. Name your variable rngCell, myCell or anything else but cell. And declare it like this: Dim rngCell as Range.
Last point - lock your worksheet and try to hide and unhide manually the rows. Is it possible? If not, you know the reason for the error.
Edit:
To check whether the sheet is protected, try this in the Worksheet_Change:
Private Sub Worksheet_Change(ByVal Target As Range)
If Worksheets("Sheet1").ProtectContents Then Exit Sub
For Each cell In Range("B6:B112")
If cell.Value <> "" Then
cell.EntireRow.Hidden = False
Else
cell.EntireRow.Hidden = True
End If
Next cell
End Sub

Highlight Individual Cell based on a change of cell

I want to highlight an individual cell based upon it being changed. The code below gives me a debug error. The range I am using is A7:AH500 if any of this individual cells changes, then highlight that cell that changed. And also can I add this to a module, or does this have to go on the sheet directly?
If Not Intersect(Target, Range("A7:AH500")) Is Nothing Or _
Not Intersect(Target, Range("A7:AH500")) Is Nothing Then
Target.Interior.ColorIndex = 3
End If
From other thread to complete this off;
Okay try this - Delete what you've just added to the ThisWorkbook object. Then with the drop-downs select 'Workbook' from the left hand one, and 'SheetChange' from the right hand one. It should insert some code. Inside the Sub (before the End Sub code) - Add the code
Target.Interior.ColorIndex = 3
The code to detect a change in a cell must be in the sheet. The code that executes whatever changes you want can be in a module.
So you could have in each sheet (if more than one):
Private Sub Worksheet_Change(ByVal Target as Range)
If Not Intersect(Target, Range("A7:AH500")) Is Nothing Or _
Not Intersect(Target, Range("A7:AH500")) Is Nothing Then
Call UpdateFormat(Target)
End If
End Sub
And in a module
Sub UpdateFormat(p_rngCell As Range)
p_rngCell.Interior.ColorIndex = 3
End Sub
This will allow you to make any changes to actual logic only once across multiple sheets.

Unlock cell on a condition from adjacent cell

I have two columns but the codition I would like is to be evaluated from one cell to another.
The first column has cells which have a drop down validation with names, and the second will activate only if a certain name from the adjacent cell is selected.
so far i only found this code but it does not seem to work:
Private Sub Worksheet_Change(ByVal Target As Range)
If Range("A1").Value = "Car" Then
Range("B1").Locked = False
Else
Range("B1").Locked = True
End If
End Sub
I would need this code go from (for example) A1:A10 and B1:B10.
I hope I am making sense. If there is a way to do it without VBA, that would be great.
Thanks for the help.
The Target parameter tells you the range that is being changed.
You need to do something like the following:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rng As Range
Set rng = Intersect(Range("A1:A10"), Target)
If rng Is Nothing Then
' Not updating the range we care about
Exit Sub
Else
rng.Offset(0, 1).Locked = ... whatever ...
End If
End Sub
Note that your target range can be more than one cell (e.g. when using copy/paste), so you need to handle and test this case.
Calling Intersect returns you the intersection of the target range and the range you are interested in testing (A1:A10 in this sample).
You can then access the corresponding adjacent cell(s) using .Offset(0,1)
That code snippet works perfectly for me.
Did you place that code in the proper WorkSheet object? It won't work if you just put it into a VBA module. When you are in the Visual Basic Editor, look for a directory on the left side of the screen labeled "Microsoft Excel Objects". In that directory should be a WorkSheet object for every sheet in your file. Double-click on one of these to edit the code for that WorkSheet. This is where your code snippet should go.
Private Sub Worksheet_Change(ByVal Target As Range)
If Range("A1").Value = "Car" Then
Range("B1").Locked = False
Me.Unprotect ("password")
Else
Range("B1").Locked = True
Me.Protect ("password")
End If
End Sub
Use Me.Protect so the .Locked method does something. You should probably unlock every other cell though.