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.
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 am new to VBA programming and am trying to add text to a cell based upon its value within an If Statement. I have tried using a find statement, but it ends in a null value and ultimately, I need this to work for 3 different values. I am not sure I need to define "MyCell" as a variable. It has not worked either way. When I run the code below it says MyCell is an invalid qualifier.
Sub Macro2()
'
' Macro to insert Text into Cells
Dim i As Long
Dim MyCell As String
Worksheets("Tax").Active
For i = 2 To Range("H" & Rows.Count).End(xlUp).Row
ActiveCell.Select
MyCell = ActiveCell.Value
If MyCell.Value = "State Tax" Then
MyCell = Join("State Tax - ", Range(i, "D"))
ElseIf MyCell.Value = "SUI" Then
MyCell = Join("SUI - ", Range(i, "D"))
ElseIf MyCell.Value = "Local Tax" Then
MyCell = Join("Local Tax - ", Range(i, "D"))
End If
Next i
End Sub
You don't need myCell. If you did you would have instanced it as a range, not a string. Try this and see if it accomplished what you need.
Sub Macro2()
'
' Macro to insert Text into Cells
Dim i As Long
With Worksheets("Tax")
For i = 2 To .Range("H" & Rows.Count).End(xlUp).Row
If .Range("H" & i).Value = "State Tax" Then
.Range("H" & i).Value = "State Tax - " & .Range("D" & i).Value
ElseIf .Range("H" & i).Value = "SUI" Then
.Range("H" & i).Value = "SUI - " & .Range("D" & i).Value
ElseIf .Range("H" & i).Value = "Local Tax" Then
.Range("H" & i).Value = "Local Tax - " & .Range("D" & i).Value
End If
Next i
End With
End Sub
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.
In Excel 2007, i'm trying to accomplish the following through VBA code:
In Col-A, if the value is aa, bb, cc then values in Col-E should be updated to 100, 1000, 10000 respectively.
If the value in Col-E is 10000 then the font color in the cell should be greyed-out. (the VBA code below is performing the details in step - 1, 2)
Here is where I'm struck:
a. In Col-A, user selects value "cc", Col-E in the row updates to "10000" and the font is greyed-out.
b. When the user selects the cell in Col-E which has "10000" then, the contents in the specific cell should be cleared. If the user enters any value then, the value entered by user should be retained. Else if the user does not enter any value and navigates to another cell, then "10000" and the font is greyed-out should appear
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim LastRow As Long
Dim i As Long
LastRow = Range("A" & Rows.Count).End(xlUp).Row
For i = 2 To LastRow
If Range("A" & i).Value = "aa" Then
Range("E" & i).Value = "100"
ActiveSheet.Range("E" & i).Font.Color = RGB(0, 0, 0)
End If
Next i
For i = 2 To LastRow
If Range("A" & i).Value = "bb" Then
Range("E" & i).Value = "1000"
ActiveSheet.Range("E" & i).Font.Color = RGB(0, 0, 0)
End If
Next i
For i = 2 To LastRow
If Range("A" & i).Value = "cc" Then
Range("E" & i).Value = "10000"
ActiveSheet.Range("E" & i).Font.Color = RGB(191, 191, 191)
End If
Next i
End Sub
I think you need to provide excel with a few more info and directions than what you have done up till now. To accomplish your goal I added some extra lines to your script and it works for me.
Dim LastRow As Long
Dim i As Long
LastRow = Range("A" & Rows.Count).End(xlUp).Row
Dim blnUserActive As Boolean
blnUserActive = False
Dim rngActive As Range
Set rngActive = Range("E2:E" & LastRow)
If Not (Application.Intersect(rngActive, Target) Is Nothing) And Target.Value = "10000" Then
'user has clicked in Column E and the value there is 10000
blnUserActive = True
Target.Value = ""
End If
For i = 2 To LastRow
If Range("A" & i).Value = "aa" Then
Range("E" & i).Value = "100"
ActiveSheet.Range("E" & i).Font.Color = RGB(0, 0, 0)
End If
Next i
For i = 2 To LastRow
If Range("A" & i).Value = "bb" Then
Range("E" & i).Value = "1000"
ActiveSheet.Range("E" & i).Font.Color = RGB(0, 0, 0)
End If
Next i
For i = 2 To LastRow
' the following two lines tells the loop to ignore this step if proven true
If Range("E" & i).Value <> "" And Range("E" & i).Value <> "10000" Then GoTo IgnoreEntry
If blnUserActive Then GoTo IgnoreEntry
If Range("A" & i).Value = "cc" Then
Range("E" & i).Value = "10000"
ActiveSheet.Range("E" & i).Font.Color = RGB(191, 191, 191)
End If
IgnoreEntry:
Next i
I hope this solves your problem. Cheers.
I am creating a macro enable spreadsheet to help me keep track of support tickets that I am currently working on in my engineering position.
I have 16 columns that provide information about each support ticket, such as Date, Serial Number of the device, Device location and so forth. A new row is a new support ticket.
Let's say we are working with row 50.
Column E represents the status of the device for which the ticket has been opened. Such as Deployed, Configure, and Floor Install.
Column F represents the device type such as Exalogic, and Exadata
Column K represents whether or not I need to create a special ticket depending on the value of E "OR" the value of F. Column K's values can be either yes or no.
This is what I have thus far and it does work with the column E range.
Dim rng As Range
Dim cellE
Dim cellF
If Not Intersect(Range("E12:E100"), Target) Is Nothing Then
Application.EnableEvents = False
For Each rng In Intersect(Range("E12:E100"), Target)
Set cellE = Range("E" & rng.Row)
Set cellK = Range("K" & rng.Row)
If Range("E" & rng.Row) = "Configure" Then
Range("K" & rng.Row) = "No"
ElseIf Range("E" & rng.Row).Value Like "*Floor*" Then
Range("K" & rng.Row) = "No"
Else
Range("K" & rng.Row) = ""
End If
Next rng
Application.EnableEvents = True
End If
However, if I try adding in the F range, nothing works:
If Not Intersect(Range("E12:E100"), Range("F12:F100"), Target) Is Nothing Then
Application.EnableEvents = False
For Each rng In Intersect(Range("E12:E100"), Range("F12:F100"), Target)
Set cellE = Range("E" & rng.Row)
Set cellF = Range("F" & rng.Row)
Set cellK = Range("K" & rng.Row)
If Range("E" & rng.Row) = "Configure" Then
Range("K" & rng.Row) = "No"
ElseIf Range("E" & rng.Row).Value Like "*Floor*" Then
Range("K" & rng.Row) = "No"
ElseIf Range("F" & rng.Row).Value Like "*Exa*" Then
'Range("K" & rng.Row) = "Not Needed"
Else
Range("K" & rng.Row) = ""
End If
Next rng
Application.EnableEvents = True
End If
Any help is greatly appreciated.
To better explain my comment:
column E never intersect Column F.
so your intersect will always fail, you are currently looking for the intersection of the three ranges, when you want the intersection of target with either of the two so you could use:
If Not Intersect(Range("E12:F100"), Target) Is Nothing Then
Application.EnableEvents = False
For Each rng In Intersect(Range("E12:F100"), Target)
Dim curRow as long
curRow = rng.Row
Set cellE = Range("E" & curRow)
Set cellF = Range("F" & curRow)
Set cellK = Range("K" & curRow)
If cellE = "Configure" Or cellE.Value Like "*Floor*" Then
cellK = "No"
ElseIf cellF .Value Like "*Exa*" Then
'cellK = "Not Needed"
Else
cellK = ""
End If
Next rng
Application.EnableEvents = True
End If
If you needed non contiguous ranges you could also use
`Intersect(Range("E12:E100","F12:F100"), Target)`
as this combines the two ranges into one
If Not Intersect(Range("E12:E100","F12:F100"), Target) Is Nothing Then
Application.EnableEvents = False
For Each rng In Intersect(Range("E12:E100","F12:F100"), Target)
Dim curRow as long
curRow = rng.Row
Set cellE = Range("E" & curRow)
Set cellF = Range("F" & curRow)
Set cellK = Range("K" & curRow)
If cellE = "Configure" Or cellE.Value Like "*Floor*" Then
cellK = "No"
ElseIf cellF .Value Like "*Exa*" Then
'cellK = "Not Needed"
Else
cellK = ""
End If
Next rng
Application.EnableEvents = True
End If