Question : Macro to check a database sheet and revert with value

Gd day,

I'm in need of a MACRO in Excel which can automatically insert a data in a cell by obtaining this data from another sheet;

Example should be like that;

database excel sheet to be as follow;

A             B
      
LIVERPOOL      21.000,00
NEW YORK      6.512,00
LONDON            11.000,00
ROTTERDAM      9.750,00
MILAN            33.100,00
BARCELONA      12.100,00



So what I need is;

I will be using another sheet, and on this sheet when I write into a cell (actually to cell number C3)
LIVERPOOL, I want MACRO to check coloumn A of the database sheet / find LIVERPOOL / convert it to 21000 when I press enter on the cell number C3

HOpe above is explanatory and looking kindly forward to hearing your kind asssitance which is badly needed

Answer : Macro to check a database sheet and revert with value

Hi,
The code is executed everytime you select another Cell. For that reason its name must be on of the names recognized by excel as Event Trigger and  'Worksheet_SelectionChange2' is not one of them.
Anyway here is the extensible code that you can customize to suit your needs. Just add/delete lines in code with the cells you need.
Of course, as etech0 said above,  you could use the VLOOKUP formula but you have asked for a macro. See  the attached file for an example on that too. Feel free to choose any method you like!

Cristi
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Dim LastRowText As Long
    Dim intRow As Long
    Dim strCol As String
    Dim strTemp() As String
    Dim i As Long
    '
    'just for safety
    If strPrevCell = "" Then
        strPrevCell = ActiveCell.Address
        Exit Sub
    End If
    '
    ' :: add below linest with all the cells you need (e.g. "$C$10", "$D$12", etc)
    '
    If strPrevCell = "$C$3" Or _
       strPrevCell = "$C$4" Or _
       strPrevCell = "$C$5" Then
            strTemp = Split(strPrevCell, "$")
            intRow = CLng(strTemp(2))
            strCol = strTemp(1)
        '
        ' :: find the last row in column A
        '
        LastRowText = Sheets(1).Cells(ActiveSheet.Rows.Count, "A").End(xlUp).Row
        '
        ' :: loop through the values in the column A
        '
        For i = 1 To LastRowText
            '
            'if value in column A is the same with value in our Cell then put in our Cell the value in column B
            If UCase(Sheets(1).Cells(i, "A")) = UCase(Cells(intRow, strCol)) Then
                Cells(intRow, strCol) = Sheets(1).Cells(i, "B")
                Exit For
            End If
        Next
    End If
    strPrevCell = ActiveCell.Address
End Sub
Random Solutions  
 
programming4us programming4us