简体   繁体   English

VBA - 检查单元格值是否在列中

[英]VBA - Check if cell value is in a column

I have 2 sheets on Excel.我在 Excel 上有 2 张纸。

Sheet 1 contains a list of ~500 universities, with their global rankings in column A and university names in column B表 1 包含约 500 所大学的列表,其全球排名在 A 列,大学名称在 B 列

Sheet 2 contains a list of individuals' names in column A, their university in column B and their exam scores in column C.表 2 在 A 列中包含个人姓名列表,在 B 列中包含他们的大学,在 C 列中包含他们的考试成绩。

I would like to write a VBA code that checks sheet 2 if the individual's university is in the top 50 rank (in sheet 1, row 1 to row 50).我想写一个 VBA 代码,如果个人的大学在前 50 名中(在表 1,第 1 行到第 50 行),则检查表 2。 If it is, I will multiply their score in column C by 1.15.如果是,我会将他们在 C 列中的分数乘以 1.15。

Thank you all in advance!谢谢大家!

Try this (i did not check in live)试试这个(我没有现场签到)

Sub checkUniversity()
Dim shInd As Worksheet
Dim rgInd As Range


Set shInd = Worksheets("Individuals")  ' put here name of your sheet

rgInd = shInd.Range("a1").CurrentRegion
lastR = rgInd.Rows.Count

With shInd
    For j = 2 To lastR ' assuming that row1 is header, and data starts in row2
    
        .Cells(j, 3).Value = .Cells(j, 3).Value * isTop50(.Cells(j, 2).Value)
    Next j
End With
End Sub

Function isTop50(university As String)
Dim shUniver As Worksheet
Set shUniver = Worksheets("universities")  'put here name of your sheet with universities
isTop50 = 1

For i = 2 To 51  '('2 is first row - 1st university, 51 row of 50th university)
    If shUniver.Cells(i, 2).Value = university Then
        isTop50 = 1.15
        Exit Function
    End If
Next i
End Function

after short time: why you want to do this with vba while you just have normal funcitons which you can use in eg column D of sheet individuals短时间内:为什么你想用 vba 做这个,而你只有正常的功能,你可以在表格个人的 D 列中使用

=if(isna(vlookup(B2, [sheetUniversities]!B2:B51,1,0)), C2, C2*1.15)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM