Need macro able to compare cells content

axioma

New Member
Joined
Jul 24, 2007
Messages
3
Hello forum,

i have a sheet with column A “component code”, and column B “surface”. The sheet has thousands of rows and I usually add rows. I need a procedure able to discover the code I am dialing already exsists and to put automatically in column B the right surface value.
Any help would be greatly appreciated.
Max
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.

Scott Huish

MrExcel MVP
Joined
Mar 17, 2004
Messages
19,958
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
Hi, welcome to the board!

It's not clear to me what you are trying to do, if you are trying to lookup the values from a table, then have a look at the VLOOKUP function. Or are you trying to repeat the values from something already entered in the column?
 

axioma

New Member
Joined
Jul 24, 2007
Messages
3
Hello :) and thank you for your prompt response,

i am just trying to repeat values already entered avoiding the risk of ricalculating complex surfaces area.
 

galileogali

Well-known Member
Joined
Oct 14, 2005
Messages
748
You can to try this code

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim valor As Variant, fila As Long

If Target.Column > 1 Then Exit Sub


valor = Target.Value


If valor = Empty Then Exit Sub

Application.EnableEvents = False

fila = Target.Row


If Application.CountIf(Range("A1:A" & fila - 1), valor) >= 1 Then
    Target.Offset(0, 1) = Application.Index(Range("B1:B" & fila - 1), Application.Match(valor, Range("A1:A" & fila - 1), 0))
Application.EnableEvents = True
    End
End If

If Application.CountIf(Range("A" & fila + 1 & ":A65536"), valor) >= 1 Then
    Target.Offset(0, 1) = Application.Index(Range("B" & fila + 1 & ":B65536"), Application.Match(valor, Range("A" & fila + 1 & ":A65536"), 0))
Application.EnableEvents = True
    End
End If

Target.Offset(0, 1).Select
Application.EnableEvents = True

End Sub

GALILEOGALI
 

Scott Huish

MrExcel MVP
Joined
Mar 17, 2004
Messages
19,958
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
EDITED:

Perhaps:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim c As Range, d As Range, e As Range
Set d = Intersect(Target, Range("A:A"))
If d Is Nothing Then Exit Sub
Application.EnableEvents = False
    For Each c In d
        Set e = Range("A:A").Find(c, Range("A" & Rows.Count))
        c.Offset(, 1) = e.Offset(, 1)
    Next
Application.EnableEvents = True
End Sub
 

galileogali

Well-known Member
Joined
Oct 14, 2005
Messages
748
Hotpepper:

you have proven your solution? If the value to look for is after (underneath) Target, "FIND" finds the first value in Target's position y Target.offset(,1) is empty.
GALILEOGALI
 

Scott Huish

MrExcel MVP
Joined
Mar 17, 2004
Messages
19,958
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
My code will look above Target first, the op wanted to duplicate previously entered entries. If it hasn't been entered because it will only find it at the place that is has just been entered. it will return blank in Column B. Why I ended up leaving it this way is this:

Say you had already entered in Column A, the Values A and C, but copy and paste from a list A, B and C, the A and C values will fill in but the B value would not as that had not been entered yet.

I did assume that previous entries it would be above the latest entry.
 

Forum statistics

Threads
1,181,055
Messages
5,927,862
Members
436,573
Latest member
CMR237

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
Top