Click here to Skip to main content
15,886,664 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi All,
i have a small issue comparing two values
here in excel sheet in
A B
1 a
2 b

Here my question is loop through the A and B columns if A column value is 1 i need to find the B column value is a,If A column value is 2 nee to find the B column value is b like that i need to find.
Any one can please share related information to me.
Posted
Updated 6-Oct-18 23:41pm

1 solution

You either want to loop through a defined range[^] or (far more likely!) use vlookup[^]

[EDIT]
For example if you know what you want to look up you could use this function
VB
Function GetData(lookupValue) As String
'lookupValue is passed in and is the thing that references the row
'I'm looking for e.g. from your data "1"

    'This defines the "table" that I'm looking things up in
    Dim LookUpRange As Range
    Set LookUpRange = Sheets("Lookup").Range("A1:B10")

    'This is where the data I want can be found RELATIVE to the lookupValue column
    'i.e. if I'm looking up something in Column A and the data is in Column B then this will be 2
    'it will also be 2 if I'm looking something up in Column E and the data I want is in column F
    Dim LookupColumn As Integer
    LookupColumn = 2

    Dim returnValue As String
    
    On Error Resume Next    'This is horrible but so is VLookup
    returnValue = WorksheetFunction.VLookup(lookupValue, LookUpRange, LookupColumn, False)
    On Error GoTo 0


    GetData = returnValue

End Function
which you could call like
C#
strImage = GetData(1)

However, if you want to loop through all of your data then you could use something like the code in this sub
VB.NET
Sub StepThrough()
    
    Dim StepRange As Range
    Set StepRange = Sheets("Lookup").Range("A1:A10")
    'Note this range is a single column

    Dim aCell As Range
    
    For Each aCell In StepRange
        Debug.Print aCell.Value2, aCell.Offset(columnOffset:=1).Value2
    Next

End Sub
 
Share this answer
 
v2
Comments
Ram349 11-Dec-15 6:51am    
Hi Chill,Thanks for reply to my post,i need to compare two column values if first column i am getting 1 but how can i find Second column B a value
can U please share any related information to me.
CHill60 11-Dec-15 6:58am    
Did vlookup not work? Share the code you used
Ram349 11-Dec-15 7:00am    
Dim rng As Range
Set rng = Worksheets("Lookup").Range("E3", "E637")
For Each cell In rng
MyFunction (cell)
If (S = cell) Then
Dim strShapeName1 As String

strShapeName1 = rngLookup.Rows(I).Columns(2).Value
in that i need to find the second column value
CHill60 11-Dec-15 7:57am    
This code doesn't do anything at all.
Let's start at the beginning...Is this a good description of what you're trying to do?...
You have something you want to look up in column A (e.g. from your data "2") and you want to return the value that is in column B (from your data "b") and this will be a function you call
Ram349 11-Dec-15 7:01am    
i am getting the vllokup value also

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900