Excel VBA 宏脚本:从工作表 2 中的工作表 1 中查找值并找到复制值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19446987/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 23:53:18  来源:igfitidea点击:

Excel VBA macro Script : Find value from Sheet 1 in Sheet 2 and Copy Value found

excelvbaexcel-vba

提问by adp

I am facing the following challenge : I have an Excel Sheet with values on column A (ex. ,, C Klasse,A Klasse,Golf,Astra" ).

我面临以下挑战:我有一个 Excel 表,A 列上有值(例如,C Klasse,A Klasse,Golf,Astra")。

On the Second Sheet I have 2 columns A & B containing the following Information : Column A ,,Mercedes,Opel,VW etc." and on B column I have the following ,,C Klasse,A Klasse,Golf,Astra" (the same information as on Sheet 1 column A).

在第二张表上,我有 2 列 A 和 B,其中包含以下信息:A 列、梅赛德斯、欧宝、大众等”,而在 B 列上,我有以下内容 ,,C Klasse,A Klasse,Golf,Astra”(与表 1 列 A 上的信息相同)。

Goal: Find the value from Sheet 1 ,Column A in Sheet 2 Column B -> copy value from Sheet 2 column A -> Paste into Sheet 1 column M.

目标:从工作表 1 中找到值,工作表 2 列 B 中的 A 列 -> 从工作表 2 A 列复制值 -> 粘贴到工作表 1 列 M。

Example: A2 = C Klasse -> Find ,,C Klasse'' in Sheet 2 - > Found ,,C Klasse'' in B42 -> Copy Content from A42 ,,Mercedes'' - > Paste M2.

例如:A2 = C Klasse -> Find ,,C Klasse'' in Sheet 2 -> Found ,,C Klasse'' in B42 -> Copy Content from A42 ,,Mercedes'' -> Paste M2。

This is the code that I came up with ,I just started this so please bear with me :

这是我想出的代码,我刚开始这样做,所以请耐心等待:

Range("A2").Select
Selection.Copy
Sheets("Form2").Select
Cells.Find(What:=Range("A2"), After:=ActiveCell, _
    LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, _
    SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False).Activate
Range("A193").Select
Selection.Copy
ActiveWindow.ScrollWorkbookTabs Position:=xlFirst
Sheets("Form1").Select
Range("M2").Select
ActiveSheet.Paste

enter image description here

在此处输入图片说明

回答by adp

So this Problem was solved by using Excel Formula VLOOKUP not VBA.

所以这个问题是通过使用 Excel 公式 VLOOKUP 而不是 VBA 来解决的。

The formula looks like this : =VLOOKUP(A2,Ressort!$A$1:$B$232,2,FALSE)

公式如下所示: =VLOOKUP(A2,Ressort!$A$1:$B$232,2,FALSE)

Many thanks to @Siddharth Rout - he is the one who opened my eyes! :)

非常感谢@Siddharth Rout - 他让我大开眼界!:)