How to set a cell value depeinding on a specefic cell value - excel

I am trying to automate the excel sheet by checking the value of a specific cell, and setting multiple columns with a value.
For example if A1 cell is "2" then I would like all the cells from B2:D54 to be the value equal to 0. I have a code written but it looks like it is wrong.
Private Sub Worksheet_Change(ByVal Target As Range)
//this selects the cell C3 and checks if the value if is 0619
If Range("C3")="0619"
Dim example As Range
Set example =Range("E3:AE53")
example.Value = 0
End if
End Sub
Edit - Added Then, but still not working.
Private Sub Worksheet_Change(ByVal Target As Range)
//this selects the cell C3 and checks if the value if is 0619
If Range("C3")="0619" Then
Dim example As Range
Set example =Range("E3:AE53")
example.Value = 0
End if
End Sub

You are missing Then in the line with If
Comments in VBA are started with ', not // so this will not parse correctly.
If Range("C3")="0619" Bear in mind Excel will remove leading zeros from numbers. Only have leading zeros if you will be formatting the value as text.
Edit: If Range("C3").Value better than If Range("C3")
Private Sub Worksheet_Change(ByVal Target As Range)
'this selects the cell C3 and checks if the value if is 0619
If Range("C3").Value = "0619" Then
Dim example As Range
Set example = Range("E3:AE53")
example.Value = 0
End If
'
Range("A1").Select
End Sub

only perform the work if C3 is changed.
if C3 is a number formatted as 0000 (leading zero) then use If Range("C3").Text ="0619" Then
as mentioned in comments, disable events or your routine will try to run on top of itself when you change the values in the cells.
event driven sub procedures should have error control
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
if not intersect(target, range("C3")) is nothing then
on error goto safe_exit
application.enableevents = false
select case range("C3").text
case "0619"
Range("E3:AE53") = 0
case else
'do nothing
end select
End if
safe_exit:
application.enableevents = true
End Sub
I've changed your criteria check to a Select Case statement to make it easier to accommodate additional conditions.

Related

Event to change font size if character counter greater then 100

I have this function where anytime a cell inside the specific range changes, calls a function.
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("A1:L60")) Is Nothing Then
Call fit_text
End If
End Sub
The function fit_text changes the font size of the value of the active cell.
Sub fit_text()
MsgBox ActiveCell.Characters.Count
If ActiveCell.Characters.Count > 100 Then
ActiveCell.Font.Size = 8
Else
ActiveCell.Font.Size = 10
End If
End Sub
PROBLEM: whenever I change the value of a cell where the character count is bigger then 100, the font size remains 10 and the message box that tells the value of the count shows 0, but whenever I run it on vba the message box shows the correct value and changes the font size if the count is bigger then 100. I need it to be automatic. CanĀ“t change the height or the width of the cells
Note that Excel can automatically shrink the font size to fit into the cell. Therefore select your cell, press Ctrl+1 go to the Alignment tab and select Shrink To Fit.
To fix your code:
Don't use ActiveCell. Use Target or the Intersect range instead. The ActiveCell might not be the cell that was changed. And also Target can be multiple cells so you need to loop through all the changed cells and test each cell individually.
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Dim AffectedRange As Range
Set AffectedRange = Intersect(Target, Target.Parent.Range("A1:L60"))
If Not AffectedRange Is Nothing Then
Dim Cell As Range
For Each Cell In AffectedRange 'loop through all changed cells
MsgBox Len(Cell.Value)
If Len(Cell.Value) > 100 Then
Cell.Font.Size = 8
Else
Cell.Font.Size = 10
End If
Next Cell
End If
End Sub
ActiveCell is the one active after the Change event. You can pass Target from the event to your method fit_text, so that it will always refer to the changed cells:
Private Sub Worksheet_Change(ByVal target As Range)
If Not Intersect(target, Range("A1:L60")) Is Nothing Then
Call fit_text(target)
End If
End Sub
Sub fit_text(target As Range)
MsgBox ActiveCell.Address(False, False)
MsgBox target.Characters.Count
' If ActiveCell.Characters.Count > 100 Then
' ActiveCell.Font.Size = 8
' Else
' ActiveCell.Font.Size = 10
' End If
If target.Characters.Count > 100 Then
target.Font.Size = 8
Else
target.Font.Size = 10
End If
End Sub
You will also want to include a check for when Target is more than a single cell; in which case you will probably want your procedure to check each cell's content.
The problem is the 'ActiveCell'.
For example when you edit the Cell A1 and press enter, the ActiveCell you are using in fit_text is not A1, but A2.
This however can easily fixed, by just passing the Cell from the Worksheet_Change to fit_text.
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("A1:L60")) Is Nothing Then
'Pass the Target to 'fit_text'
Call fit_text(Target)
End If
End Sub
Sub fit_text(Cell)
'Instead of using ActiveCell, use Cell (which is the passed Target)
MsgBox Cell.Characters.Count
If Cell.Characters.Count > 100 Then
Cell.Font.Size = 8
Else
Cell.Font.Size = 10
End If
End Sub

Pull data from a corresponding cell in row A in last edited row and insert into cell A1

I am looking for a script that will pull data from last edited cell into Cell B1 of active sheet and also look up data from cell in column A and then display it in cell A1.
So far I've got this to pull last edited cell into B1 and it works fine but I cannot figure out how to then go back from that point to row A and display the other info.
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Target.Worksheet.Range("F13:W9910")) Is Nothing Then
ActiveSheet.Range("B1").Value = Target.Value
End If
End Sub
In the attached picture if I add any numbers in section called trays completed (in red) to display in B1 and then look up number in Sap column and display the number in cell A1
Something like this:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Me.Range("F13:W9910")) Is Nothing Then
Me.Range("B1").Value = Target.Value
Me.Range("A1").Value = Target.Entirerow.Cells(1).Value
End If
End Sub
Note when in a sheet code module you can refer to the worksheet using Me
Also be aware that Target might be >1 cell and your code might need to handle that.
This will take the corresponding value in A and place it in A1. I only added one line of code to yours.
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Target.Worksheet.Range("F13:W9910")) Is Nothing
Then
ActiveSheet.Range("B1").Value = Target.Value
ActiveSheet.Range("A1").Value = ActiveSheet.Range("A" & Target.Row)
End If
End Sub

How to prevent pasting over validation drop down cell in Excel VBA

I need to stop the user from pasting over my validation drop down cell. I have read and tried various solutions, none of which work just right. This code I have checks if the pasted value follows validation rules, but it doesn't work if the entire cell is pasted over my validation cell (it seems that this event fires after the paste, so the validation gets erased together with the previous cell):
Private Sub Worksheet_Change(ByVal Target As Range)
Dim Cell As Range
For Each Cell In Range("D2:F13")
If Not Cell.Validation.Value Then
MsgBox "Value violates validation rule"
Application.Undo
Exit Sub
End If
Next
Ideally the code would check if the value of the cell that's being pasted matches validation dropdown options and only allows to paste the value (not the formatting) into the cell.
Thanks!
You can disable the Cut\Copy in the specific cell:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
' TARGET IS YOUR VALIDATION CELL
If Target.Column = 1 And Target.Row = 1 Then
Application.CutCopyMode = False
End If
End Sub
Or more complex you can try to check the clipboard of the user on SelectionChange
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
' TARGET IS YOUR VALIDATION CELL
If Target.Column = 1 And Target.Row = 1 Then
Set MyData = New DataObject
MyData.GetFromClipboard
'In MyData.GetText you have the clipboard data in text format
If MyData.GetText <> "what you want" then
'...
End if
End If
End Sub
In this case you must add a reference to Microsoft Forms 2.0 Object Library. You can find it in this path: C:\Windows\System32\FM20.DLL

If cell value is not equal to zero then message box

I have written this code to automatically pop up a message box when the value of a cell is not equal to zero. This cell depends on the value of cell A minus the value of cell B.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Range("H60") <> 0 Then
MsgBox "Not Equal zero!!!!"
End If
End Sub
However, when the cell H60 is zero, the message box still continues to pop up. Why?
use .Value2
Private Sub Worksheet_Change(ByVal Target As Range)
If Range("H60").Value2 <> 0 Then
MsgBox "Not Equal zero!!!!"
End If
End Sub
I would however recommend you put some sort of test in there to only fire on certain cell changes, you don't want it firing every time anything is changed on the sheet.
You mentioned the cell relies on 2 other cells, you could have your test on those with something like this:
Private Sub Worksheet_Change(ByVal Target As Range)
If target.address = "$I$60" or target.address = "$J$60" then
If Range("H60").Value2 <> 0 Then
MsgBox "Not Equal zero!!!!"
End If
end if
End Sub
This will make it only fire if I60 or J60 are what was changed on the sheet, you can obviously change these to other cell references if you need, I assumed your formula is using I60 and J60

VBA: How to trigger a worksheet event function by an automatic cell change through a link?

My problem is the following:
The function below triggers an "if then function" when i manually change the value in cell D9. What should I do to get it to work with an automatic value change of cell D9 trough a link.
In other words if i where to link cell D9 to cell A1 and change the value of A1 can i still make the function below work?
Private Sub Worksheet_Change(ByVal Target As range)
If Target.Address = "$D$9" Then
If range("C12") = 0 Then
Rows("12:12").Select
Selection.RowHeight = 0
Else:
Rows("12:12").Select
Selection.RowHeight = 15
End If
End Sub
How about something like this:
Private Sub Worksheet_Change(ByVal Target As Range)
On Error Resume Next
Dim rngDependents As Range
Set rngDependents = Target.Dependents
If Target.Address = "$D$9" Then
MsgBox "D9 has changed"
ElseIf Not Intersect(rngDependents, Range("$D$9")) Is Nothing Then
MsgBox "D9 has been changed indirectly"
End If
End Sub
try to make your function then in other cell use the function with input the link to the cell d9.
When you change the value at cell d9 your function will be evaluated.

Resources