vba Excel 宏来比较两列 Ax、Bx 和当真时在不同的列 Dx 中打印值 Cx
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19156181/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Excel macro to compare two columns Ax, Bx and when true print Value Cx in different column Dx
提问by user2842098
I have an excel report with 60k rows and 30+ columns. I want to compare value H1
with all the values in column B:B
and when there is a match, I want to print the value Nx
to a different column BF:BF
or sheet2.A:A
, whichever is simple and then move onto Hx
and so on.
我有一个包含 60k 行和 30 多列的 Excel 报告。我想将值H1
与列中的所有值进行比较B:B
,当匹配时,我想将该值打印Nx
到不同的列BF:BF
或sheet2.A:A
,以简单的为准,然后继续Hx
等等。
Please help me out.Thanks in advance.
请帮帮我。提前致谢。
回答by LS_???
Why use macros?
为什么要使用宏?
[BF2] =IF($H=B2; N2; "")
[H2] =BF2
回答by Takedasama
This code will check each cell in column B with the values stored in cell H1 of the same sheet. When the values match it will fill the coresponding cell (by row) in column BF with the column "N" value: Just fill your sheets names and eventually set a smaller row count (to 10000)
此代码将使用存储在同一工作表的单元格 H1 中的值检查 B 列中的每个单元格。当值匹配时,它将用列“N”值填充 BF 列中的相应单元格(按行):只需填写您的工作表名称并最终设置较小的行数(至 10000)
Sub CompareValues ()
Dim Wks as Worksheet: Set Wks = Sheets("YourWorkSheetName")
' in this worksheet the code will do the lookup and copy values
Dim Wks2 as Worksheet: Set Wks2 = Sheets("YourOtherWorkSheetName")
' in this sheet (2) the code will optionally copy the values
CompareValue = Wks.Range("H1").value
Dim I as integer
for i = 1 to 10000 ' you can set a smaller value thow
If Wks.Range("B"&i) = CompareValue then
Wks.Range("BF"&i).Value = Wks.Range("N"&i)
' to fill the value into another sheet simply replace the Wks with Wks2
' Wks2.Range("BF"&i) = = Wks.Range("N"&i)
end if
next i
End Sub
Hope this helps!
希望这可以帮助!