如何comapare两列值 [英] How to comapare two columns values

查看:70
本文介绍了如何comapare两列值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

i有一个小问题比较两个值

这里的excel表格

AB

1 a

2 b



这里我的问题是循环通过A和B列如果A列值为1我需要查找B列值是a,如果A列值是2 nee找到B列值就像我需要找到的那样。

任何人都可以请我分享相关信息。

解决方案

您要么循环定义的范围 [ ^ ]或(更有可能!)使用 vlookup [ ^ ]





例如,如果您知道要查找的内容,可以使用此功能

 函数 GetData(lookupValue)作为 字符串 
传递了lookupValue,并引用了行
' 我正在寻找例如来自您的数据1

' 这定义了表我正在寻找
Dim LookUpRange As 范围
设置 LookUpRange =表格( 查找)。范围( A1:B10

' 这是我想要的数据所在的位置RELAND到lookupValue列
' ie如果我在A列中查找某些内容并且数据在B列中,那么这将是2
' 如果我在E列中查找内容并且我想要的数据在F列中
Dim LookupColumn 作为 整数
LookupColumn = 2

Dim returnValue As String

On 错误 恢复 下一步 ' 这很糟糕,但VLookup也是如此
returnValue = WorksheetFunction.VLookup(lookupValue,LookUpRange,LookupColumn, False
On 错误 GoTo 0


GetData = returnValue

结束 功能

您可以像

 strImage = GetData(  1 



但是,如果你想循环遍历所有数据,那么你可以使用像此子代码

  Sub  StepThrough()

Dim StepRange As 范围
设置 StepRange =表格( 查找)。范围( A1:A10
' 注意此范围是单列

Dim aCell 作为范围

对于 每个 aCell In StepRange
Debug.Print aCell.Value2,aCell.Offset(columnOffset:= 1).Value2
下一步

结束 Sub


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.

解决方案

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

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

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

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


这篇关于如何comapare两列值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆