To preface the situation, I am new to VBA programming so any help would be greatly appreciated.
I have two columns; one where the user can input a dollar value ("AL") and another where the user can input a percent value ("AK"). The object is to enable the user to input either value (% or $) and have the other value calculate. For instance, if the user inputs 10% in "AL", the applicable $ value will generate in "AK" and vice versa.
Below is the code I've come up with thus far but it isn't working. Any thoughts/suggestions would be greatly appreciated! Thank you!
Private Sub Worksheet_Change(ByVal Target As Range)
Dim cell As Range
Set cell = Range("AK9:AL50")
'Application.EnableEvents = False Application.EnableEvents = True'
If Not Application.Intersect(cell, Range(Target.Address)) Is Nothing Then
If Target.Column = 37 Then ' Value in first column changed
Range("AL" & Target.Row).Value = Range("AK" & Target.Row).Value / Range("V" & Target.Row).Value
Exit Sub
ElseIf Target.Column = 38 Then ' value in second column changed
Range("AK" & Target.Row).Value = Range("AL" & Target.Row).Value * Range("V" & Target.Row).Value
Exit Sub
'Application.EnableEvents = False Application.EnableEvents = True'
End If
End If
End Sub
You need to remove the Exit Subs
And the Application.EnableEvents = True needs to be outside the if.
The first time you ran it with the Application.EnableEvents = False line enabled it turned off the events and since you exited the sub before turning them back on it stayed off and the sub was never called again.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim cell As Range
Set cell = Range("AK9:AL50")
Application.EnableEvents = False
If Not Application.Intersect(cell, Range(Target.Address)) Is Nothing Then
If Target.Column = 37 Then ' Value in first column changed
Range("AL" & Target.Row).Value = Range("AK" & Target.Row).Value / Range("V" & Target.Row).Value
ElseIf Target.Column = 38 Then ' value in second column changed
Range("AK" & Target.Row).Value = Range("AL" & Target.Row).Value * Range("V" & Target.Row).Value
End If
Application.EnableEvents = True
End If
End Sub
My guess is right now your events are disabled.
Run this code after putting the correct code above in your sheet:
Sub foooo()
Application.EnableEvents = True
End Sub
This will turn the events back on. It is only needed once.
You can have a better use of your Worksheet_Change parameters, like Target.
1.Instead of:
Range("AL" & Target.Row).Value
you can use:
Target.Offset(, 1).Value
2.Instead of:
Range("AK" & Target.Row).Value
you can use:
Target.Value
3.Also Range(Target.Address) actually is Target
Code
Private Sub Worksheet_Change(ByVal Target As Range)
Dim cell As Range
Set cell = Range("AK9:AL50")
Application.EnableEvents = False
If Not Application.Intersect(cell, Target) Is Nothing Then
If Target.Column = 37 Then ' Value in first column changed
Target.Offset(, 1).Value = Target.Value / Range("V" & Target.Row).Value
ElseIf Target.Column = 38 Then ' value in second column changed
Target.Offset(, 2).Value = Target.Value * Range("V" & Target.Row).Value
End If
End If
Application.EnableEvents = True '<-- RESTORE SETTING OUTSIDE THE IF
End Sub
Related
I have the below code for entering a function and copying the orientation and borders of the above line.
But in this it only accept numeric values, how can i modify the code so that i can enter alpa numeric values in that cell.
Below is the code
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("B:B")) Is Nothing Then
If IsNumeric(Target.Value) Then ' Check if cell contains a numeric value
If Target.Value <> "" Then
Range("A" & Target.Row).Formula = "=IF(B" & Target.Row & "<>"""",ROW()-ROW($A$15)+1,"""")"
' Copy border, border color and orientation from row above
With Range("A" & Target.Row & ":H" & Target.Row)
.Borders.LineStyle = .Offset(-1, 0).Borders.LineStyle
.Borders.Color = .Offset(-1, 0).Borders.Color
.Orientation = .Offset(-1, 0).Orientation
End With
Else
' Check if entire row in column B is empty
If WorksheetFunction.CountA(Range("B" & Target.Row & ":H" & Target.Row)) = 0 Then
' Delete entire row
Rows(Target.Row).Delete
Else
' Clear contents of column A to H for the row where value was deleted in column B
Range("A" & Target.Row & ":H" & Target.Row).ClearContents
End If
End If
End If
End If
End Sub
Here's a small Function you could add to your code, to give it IsAlphaNumeric functionality.
Function IsAlphaNumeric(t) as Boolean
Dim i as Long
IsAlphaNumeric = True
For i = 1 To Len(t)
If Not (Mid(t, i, 1) Like "[A-z0-9]") Then
IsAlphaNumeric = False
Exit For
End If
Next
End Function
You can use it like this:
If IsAlphaNumeric(Target.Value) Then ' Check if cell contains alpha-numeric value
if i double click in column g of any row that is defined i need the symbol to be shown in each column such as h,i,j,k,l,m of the row double clicked in column g
my vba code is
If Not Intersect(Target, Range("h3:m20")) Is Nothing Then
If Target.Value = "Ð" Then
Target.Value = "Ï"
Exit Sub
End If
If Target.Value = "x" Then Target.Value = "Ð"
If Target.Value = "Ï" Then Target.Value = "x"
If Target.Value = Empty Then Target.Value = "Ð"
End If
If Not Intersect(Target, Range("G3:G20")) Is Nothing Then
If Target.Value = "Ð" Then
Range("G" & Target.Row & "M" & Target.Row).Value = "Ï"
Exit Sub
End If
If Target.Value = "x" Then Range("G" & Target.Row & "M" & Target.Row).Value = "Ð"
If Target.Value = "Ï" Then Range("G" & Target.Row & "M" & Target.Row).Value = "x"
If Target.Value = Empty Then Range("G" & Target.Row & "M" & Target.Row).Value = "Ð"
End If
End Sub
this is not working...please help....thank you
Your Range references are missing a colon. You also mentioned in your explaination:
"if i double click in column g of any row that is defined i need the symbol to be shown in each column"
Right now you checking against an Intersect with the wrong Range. So try the below instead:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
'Only activate when its a cell in range `G3:G20`
If Intersect(Target, Range("G3:G20")) Is Nothing Then Exit Sub
'Do something here with your values and IF statements
Target.Offset(0, 1).Resize(1, 6).Value = "TEST" 'Purely to test
End Sub
Not a finished code, but you'll be able to add the missing pieces to the puzzle at the bottom.
I have the code below which, in a simple request form, gives requestor an option to add a line for the same user.
When "Yes" is selected from a drop-down menu, a new line populates with the same Name and Alias used in the previous row, while other rows below it would move down by one row accordingly.
The code to ADD a new line (works fine) is as follows:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Dim LastRow As Long
With ThisWorkbook.Worksheets("AWS Applications")
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
If Not Intersect(Target, .Range("F7:F" & LastRow)) Is Nothing And Target.Value = "Yes" Then
Application.EnableEvents = False
.Rows(Target.Row + 1).EntireRow.Insert
.Range("A" & Target.Row & ":C" & Target.Row).Copy .Range("A" & Target.Row + 1 & ":C" & Target.Row + 1)
Application.EnableEvents = True
End If
End With
End Sub
I modified the above code as follows so it does remove a row below if the "No" option is selected. And it is working properly:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim LastRow As Long
With ThisWorkbook.Worksheets("AWS Applications")
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
If Not Intersect(Target, .Range("F7:F" & LastRow)) Is Nothing And Target.Value = "Yes" Then
Application.EnableEvents = False
.Rows(Target.Row + 1).EntireRow.Insert
.Range("A" & Target.Row & ":C" & Target.Row).Copy .Range("A" & Target.Row + 1 & ":C" & Target.Row + 1)
Application.EnableEvents = True
End If
If Not Intersect(Target, .Range("F7:F" & LastRow)) Is Nothing And Target.Value = "No" Then
Application.EnableEvents = False
.Rows(Target.Row + 1).EntireRow.Delete
Application.EnableEvents = True
End If
End With
End Sub
However, I want to make sure that a below row is deleted after selecting "No" only in cases where the below row that is to be deleted contains same data as the row above. As it is now, it removes the below line in any case, i.e. even if the requestor previously didn't click "Yes", and that's not a desired outcome.
I've been trying to modify the "No" condition as follows but still struggling:
If Not Intersect(Target, .Range("F7:F" & LastRow)) Is Nothing And Target.Value = "No" Then
If Range("A" & Target.Row & ":C" & Target.Row).Value = Range("A" & Target.Row + 1 & ":C" & Target.Row + 1).Value Then
Application.EnableEvents = False
.Rows(Target.Row + 1).EntireRow.Delete
Application.EnableEvents = True
End If
End If
Could you please help?
FOLLOW-UP:
The code I'm having now is this:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim LastRow As Long
With ThisWorkbook.Worksheets("AWS Applications")
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
If Not Intersect(Target, .Range("F7:F" & LastRow)) Is Nothing And Target.Value = "Yes" Then
Application.EnableEvents = False
.Rows(Target.Row + 1).EntireRow.Insert
.Range("A" & Target.Row & ":C" & Target.Row).Copy .Range("A" &
Target.Row + 1 & ":C" & Target.Row + 1)
Application.EnableEvents = True
End If
If Not Intersect(Target, .Range("F7:F" & LastRow)) Is Nothing And Target.Value = "No" Then
AllOk = True
For Each xCel In UpperRow.Cells
If AllOk And (xCel.Value <> xCel.Offset(1, 0).Value) Then
AllOk = False
End If
Next xCel
If AllOk Then
Application.EnableEvents = False
.Rows(Target.Row + 1).EntireRow.Delete
Application.EnableEvents = True
End If
End If
End With
End Sub
I keep getting '424' error "Object required" and the debug highlights this: For Each xCel In UpperRow.Cells
Could you please help? Apologies I'm a beginner in this...
As an indicative answer
AllOk = True
for each xCel in UpperRow.Cells
if AllOk and (xCel.Value <> xCel.Offset(1,0).Value) then
AllOk = False
End If
Next xCel
IF AllOk then
' Delete the Row
End If
You'll need to fill in some details and maybe some error checking - not a full answer
I'm trying to fire an onChange event when value entered to column A.
Now I want this, if I enter any value from Column A to Column AS, the event will fire and if I remove any value from same columns it will work as Code is written.
Also if I copy and paste a multiple data it's not working, also if I'm removing the multiple data it's not working.
Can anyone help on this? Below is the code.
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Excel.Range)
Dim currentRow As Integer
If Not Intersect(Target, Columns("A")) Is Nothing Then
If Target.Value <> "" Then
currentRow = Target.Row
Target.Parent.Range("A" & currentRow & ":AS" & currentRow).Interior.ColorIndex = 15
Target.Parent.Range("A" & currentRow & ":AS" & currentRow).Borders.LineStyle = xlContinuous
End If
If Target.Value = "" Then
currentRow = Target.Row
Target.Parent.Range("A" & currentRow & ":AS" & currentRow).Interior.ColorIndex = 0
Target.Parent.Range("A" & currentRow & ":AS" & currentRow).Borders.LineStyle = xlNone
End If
End If
End Sub
Target.Value only has a value if a single cell is selected. If you select more than one cell it becomes an array and your If statement will always evaluate to False.
Here is one way to change your code. I was in a bit of a hurry so it could probably be done much better but should get you started.
Private Sub Worksheet_Change(ByVal Target As Excel.Range)
If Not Intersect(Target, Columns("A")) Is Nothing Then
If Application.WorksheetFunction.CountA(Target) = 0 Then
' Empty Range
For Each rw In Target.Rows
Target.Parent.Range("A" & rw.Row & ":AS" & rw.Row).Interior.ColorIndex = 0
Target.Parent.Range("A" & rw.Row & ":AS" & rw.Row).Borders.LineStyle = xlNone
Next rw
Else
' Not Empty
For Each rw In Target.Rows
Target.Parent.Range("A" & rw.Row & ":AS" & rw.Row).Interior.ColorIndex = 15
Target.Parent.Range("A" & rw.Row & ":AS" & rw.Row).Borders.LineStyle = xlContinuous
Next rw
End If
End If
End Sub
I running a macro that pops up a message if the user inputs a value in column E having column D empty. therefore the user has to input value in D and then in E. once the user inputs a value in D, by Vlookup formula the sheet will display a number in column F.
The second macro should then check if value of column F is not equal to value input in column E, if not equal popup a message.
First part is working but not the second. any idea please. thanks
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = Cells(Target.Row, 5).Address And Target.Value <> "" And Cells(Target.Row, 4).Value = "" Then
MsgBox "Input value in column D"
Cells(Target.Row, 4).Select
Target.Clear
End If
Call Macro2
End Sub
Sub Macro2()
If Target.Address = Sheets(1).Cells(Target.Row, 5).Address And Target.Value <> "" And Target.Value <> Sheets(1).Cells(Target.Row, 6).Value Then
MsgBox "E and F don't match"
End If
End Sub
If the second one is the problem, then pass the Target to it:
Private Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = False '<--- Consider removing this line
If Target.Address = Cells(Target.Row, 5).Address _
And Target.Value <> "" _
And Cells(Target.Row, 4).Value = "" Then
MsgBox "Input value in column D"
Cells(Target.Row, 4).Select
Target.Clear
End If
Macro2 Target
Application.EnableEvents = True '<--- Consider removing this line
End Sub
Sub Macro2(Target As Range)
If IsError(Target) Then
MsgBox Target.Address & "is an error!"
ElseIf IsError(Sheets(1).Cells(Target.Row, 6)) Then
MsgBox Sheets(1).Cells(Target.Row, 6).Address & " is an error!"
ElseIf Target.Address = Sheets(1).Cells(Target.Row, 5).Address _
And Target.Value <> "" _
And Target.Value <> Sheets(1).Cells(Target.Row, 6).Value Then
MsgBox "E and F don't match"
End If
End Sub
However, it could be that Target.Clear is making a loop within the Worksheet_Change, because it changes the worksheet once again. Depending on whether this is ok or not ok, you may consider writing Application.EnableEvents = False and Application.EnableEvents = True at the start or at the end of the Sub.