I am working on a VBA application, where when I select an option from a drop down list (created using data validation) a few columns should populate automatically. It is working fine if I select an option from the drop down list for each cell individually. However if I drag down an option over a few rows, only the data for the top row gets populated whereas that for the remaining rows do not. How do I tackle this? This is my code under Worksheet_Change function
Private Sub Worksheet_Change(ByVal Target As range)
If Target.Column = 22 Then
ThisRow = Target.Row
On Error GoTo ExitSub
If Target.Value = "E" Then
range("W" & ThisRow).Value = range("R" & ThisRow).Value
range("X" & ThisRow).Value = ""
On Error GoTo 0
ElseIf Target.Value = "T" Then
range("W" & ThisRow).Value = ""
range("X" & ThisRow).Value = range("S" & ThisRow).Value
ElseIf Target.Value = "M" Then
range("W" & ThisRow).Value = ""
range("X" & ThisRow).Value = ""
ElseIf Target.Value = "N" Then
range("W" & ThisRow).Value = 0
range("X" & ThisRow).Value = 0
ElseIf Target.Value = "R" Then
range("W" & ThisRow).Value = range("T" & ThisRow).Value
range("X" & ThisRow).Value = range("U" & ThisRow).Value
End If
End If
Exit Sub
ExitSub:
Exit Sub
End Sub
If you're selecting multiple cells, then you have more than one row and/or column in your Target range, so statements referencing .Column and .Row are going to be ambiguous.
If you wrap this code in a loop that steps through each cell within the range, it should update the whole range, not just the first cell.
Related
I am trying to implement a functionality that obliged the user filling in column B a value from a dropdown list to also fill right after another cell column I from same row and sheet. With possibility to cancel the action, removing then his choice at column B. I am thinking to do that thanks to a message box to fill, clicking ok would then fill the cell at column I.
I found nothing about this kind of function on the internet. Could you help me finding a simple code?
Thank you
I did this that answers perfectly my need, for information
Private Sub Worksheet_Change(ByVal Target As Excel.Range)
If Target.Column = 2 Then
ThisRow = Target.Row
If Target.Value = "Sourcing" Then
inputdate = InputBox("Please enter Sourcing end date target dd/mm/yyyy", "Enter Sourcing end date", Format(inputdate, "mm/dd/yyyy"))
If inputdate = "" Or inputdate = vbNullString Then
Range("I" & ThisRow).Value = ""
Range("B" & ThisRow).Value = ""
Else
If IsDate(inputdate) Then
inputdate = Format(CDate(inputdate), "mm/dd/yyyy")
Range("I" & ThisRow).Value = inputdate
Else
MsgBox "Wrong date format"
Range("I" & ThisRow).Value = ""
Range("B" & ThisRow).Value = ""
End If
End If
ElseIf Target.Value = "Phasing out" Then
inputdate = InputBox("Please enter History target date dd/mm/yyyy", "Enter History date", Format(inputdate, "mm/dd/yyyy"))
If inputdate = "" Or inputdate = vbNullString Then
Range("I" & ThisRow).Value = ""
Range("B" & ThisRow).Value = ""
Else
If IsDate(inputdate) Then
inputdate = Format(CDate(inputdate), "mm/dd/yyyy")
Range("I" & ThisRow).Value = inputdate
Else
MsgBox "Wrong date format"
Range("I" & ThisRow).Value = ""
Range("B" & ThisRow).Value = ""
End If
End If
ElseIf Target.Value = "History" Then
Range("I" & ThisRow).Value = Date
ElseIf Target.Value = "" Then
Range("I" & ThisRow).Value = ""
End If
End If
End Sub
Had to find a trick about date format that was changing from dd/mm to mm/dd, add some condition if window is cancelled or date empty and this works fine
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
Hoping I can get help here, I am currently using the Dim Long in my VBA code but since I am referring to multiple columns the code became quite long. Now, I wanted to try named range reference instead but i cannot make it work.
This is my current code:
Dim i As Long
For i = 8 To 500
'if details is incomplete
If Range("AA" & i).Value > 0 Then
If Range("AB" & i).Value = "Error" Or Range("AC" & i).Value = "Error" Or Range("AD" & i).Value = "Error" _
Or Range("AE" & i).Value = "Error" Or Range("AF" & i).Value = "Error" Or Range("AG" & i).Value = "Error" _
Or Range("AH" & i).Value = "Error" Or Range("AI" & i).Value = "Error" Or Range("AJ" & i).Value = "Error" _
Or Range("AK" & i).Value = "Error" Or Range("AL" & i).Value = "Error" Or Range("AM" & i).Value = "Error" _
Or Range("AN" & i).Value = "Error" Or Range("AO" & i).Value = "Error" Or Range("AP" & i).Value = "Error" _
Or Range("AQ" & i).Value = "Error" Or Range("AR" & i).Value = "Error" Or Range("AS" & i).Value = "Error" _
Or Range("AT" & i).Value = "Error" Or Range("AU" & i).Value = "Error" Or Range("AV" & i).Value = "Error" _
Or Range("AW" & i).Value = "Error" Or Range("AX" & i).Value = "Error" Or Range("AY" & i).Value = "Error" Then
MsgBox "One of the mandatory field is not provided, please check all cells highlighted in yellow & make sure details is provided."
End If
Endif
I named range AA = "Validation" & range AB:AY = "Details" how can i declare it and use named range instead of writing each columns one by one?
As #Ike suggests - use the COUNTIF formula. Can be used on the worksheet or within VBA. If you want to return the addresses of each error then Find might be a better route.
Sub Test()
Dim Result As Long
Result = Errors(Sheet1.Range("AB8:AY500"))
If Result > 0 Then
MsgBox "There are " & Result & " errors in the range."
End If
End Sub
Public Function Errors(Target As Range) As Long
Errors = WorksheetFunction.CountIf(Target, "Error")
End Function
Conditional formatting can handle this. I have demonstrated for a smaller range. Feel free to apply it for your required range.
NON VBA
Formula used: =AND($AA8>0,AB8="Error")
VBA
You can use conditonal formatting in VBA as well.
I have commented the code.
Option Explicit
Sub Sample()
Dim i As Long
Dim ws As Worksheet
Dim CondTrue As Boolean
'~~> Change this to the relevant sheet
Set ws = ThisWorkbook.Sheets("Sheet1")
With ws
'~~> Check if there is even one cell which satisfies our condition
For i = 8 To 500
If .Evaluate("=AND(AA" & i & ">0,COUNTIF(AB" & i & ":AY" & i & ",""Error"")>0)") = True Then
CondTrue = True
Exit For
End If
Next i
'~~> If found then apply conditional formatting
If CondTrue Then
With .Range("AB8:AY500")
.FormatConditions.Delete
.FormatConditions.Add Type:=xlExpression, _
Formula1:="=AND($AA8>0,AB8=""Error"")"
.FormatConditions(.FormatConditions.Count).SetFirstPriority
With .FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 65535
.TintAndShade = 0
End With
.FormatConditions(1).StopIfTrue = False
End With
'~~> Show message box
MsgBox "One of the mandatory field is not provided, please check all cells highlighted in yellow & make sure details is provided."
Else
MsgBox "All Good!"
End If
End With
End Sub
IN ACTION (VBA)
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.
In one sheet i have this data
A APPLE
B BANANA
C CROW
D DEER
E EGG
F FISH
G GOAT
H HUT
In another sheet whenever i write A in a column, apple should be written in the next column.
A (i write A) then Apple should be written in the column next to it.
You'd have to use VBA Script in Excel. Starting with an example in Worksheet.Change Event (Excel), you could use the following code to achieve your goal.
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column = 1 Then
ThisRow = Target.Row
If Target.Value = "" Then
Range("B" & ThisRow).Value = ""
Range("B" & ThisRow).Show
End If
If Target.Value = "A" Then
Range("B" & ThisRow).Value = "Apple"
Range("B" & ThisRow).Show
End If
If Target.Value = "B" Then
Range("B" & ThisRow).Value = "Banana"
Range("B" & ThisRow).Show
End If
If Target.Value = "C" Then
Range("B" & ThisRow).Value = "Crow"
Range("B" & ThisRow).Show
End If
If Target.Value = "D" Then
Range("B" & ThisRow).Value = "Deer"
Range("B" & ThisRow).Show
End If
If Target.Value = "E" Then
Range("B" & ThisRow).Value = "Egg"
Range("B" & ThisRow).Show
End If
If Target.Value = "F" Then
Range("B" & ThisRow).Value = "Fish"
Range("B" & ThisRow).Show
End If
If Target.Value = "G" Then
Range("B" & ThisRow).Value = "Goat"
Range("B" & ThisRow).Show
End If
End If
End Sub
Writing 'A' in column A and row 1, you need to move off that cell to trigger the Worksheet_Change event and you should see Apple appear in column B on the same row.