Getting a cell value based on a pre-existing cell value dynamically

abdelfattah

Well-known Member
Joined
May 3, 2019
Messages
1,429
Office Version
  1. 2019
  2. 2010
Platform
  1. Windows
hi, experts
i write data manually in column a, b what i want if i write duplicated data in column a then bring value in column b for instance i write theses data

a b
aa 123
bb 1234
cc 12345
then if i repeat this
aa should bring 123
if is possible do that by macro in worksheet event change
thanks advance
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Are you going to always be entering data in column A down the column, so that you would only ever need to look at the rows above the one you just entered?
 
Upvote 0
If my above assumption is correct, try this:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    Dim r As Long
    Dim rng As Range
    Dim val As String

'   Exit if multiple cells updated at once (i.e. Copy/Paste)
    If Target.CountLarge > 1 Then Exit Sub
    
'   Exit if entry not in column A or in row 1 or 2
    If (Target.Column > 1) Or (Target.Row <= 2) Then Exit Sub
    
'   Exit if column A empty
    If Target.Value = "" Then Exit Sub
    
'   Build range to lookup
    r = Target.Row
    Set rng = Range("A2:B" & r - 1)
    
'   Perform vlookup on entry
    On Error GoTo err_exit
    val = Application.WorksheetFunction.VLookup(Target.Value, rng, 2, 0)
    
'   Put looked up value in column B, if column B is blank
    If Cells(r, "B") = "" Then Cells(r, "B") = val

    Exit Sub
    
err_exit:
    Err.Clear

End Sub
The code could be shortened, but I wanted to make it clear what each part is doing.
 
Upvote 0
thanks joe for this code works excellently but i have problem when i add a new data not duplicated it gives me error

1004 Error with WorksheetFunction.VLookup?

When you enter formulas into Excel, do you use commas or semi-colons as argument separators?
If semi-colons, you may need to change that formula to:
VBA Code:
val = Application.WorksheetFunction.VLookup(Target.Value; rng; 2; 0)
 
Upvote 0
yes when i use formula it is semi-colons despite for this it gives me compile error
 
Upvote 0
Go into the VB Editor, and from the Tools menu, select References and see if a library that looks like the following is selected:
Microsoft Excel 16.0 Object Library

If it is, can you post your actual data?
 
Upvote 0
yes it is
and this is my data
1 Microsoft Excel جديد (2).xlsx
AB
1itemval
2aa123
3bb1234
4cc12
5dd89
6ee76
sheet1
 
Upvote 0
If it works with duplicated values, try this mod to Joe's code
Rich (BB code):
Private Sub Worksheet_Change(ByVal Target As Range)

    Dim r As Long
    Dim rng As Range
    Dim val As Variant

'   Exit if multiple cells updated at once (i.e. Copy/Paste)
    If Target.CountLarge > 1 Then Exit Sub
    
'   Exit if entry not in column A or in row 1 or 2
    If (Target.Column > 1) Or (Target.Row <= 2) Then Exit Sub
    
'   Exit if column A empty
    If Target.Value = "" Then Exit Sub
    
'   Build range to lookup
    r = Target.Row
    Set rng = Range("A2:B" & r - 1)
    
'   Perform vlookup on entry
    On Error GoTo err_exit
    val = Application.VLookup(Target.Value, rng, 2, 0)
    If IsError(val) then Exit Sub
'   Put looked up value in column B, if column B is blank
    If Cells(r, "B") = "" Then Cells(r, "B") = val

    Exit Sub
    
err_exit:
    Err.Clear

End Sub
 
Upvote 0
Solution
thanks fluff for mod the code and thanks joe for the code now it works as what i want
 
Upvote 0

Forum statistics

Threads
1,215,477
Messages
6,125,030
Members
449,205
Latest member
Eggy66

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top