我有2个Excel文件,其中包含姓名(同一单元格中的姓氏和名字),在第二个文件中,我想要将Excel File1的数据写入某人的同名,并复制其作业名称写在同一行。我会设置一个结构,以便你们可以理解为一个例子。让你们知道,我只是在Excel File2中有一些员工。在Excel File1中,大约有1200个Employees,在Excel File2中只有大约150个。我需要一个VBA技巧。产品:>希望你喜欢有趣的内容。
答案 0 :(得分:0)
只需使用INDEX和MATCH excel功能,即可在没有VBA的情况下完成。在您的情况下,您需要将Excel File2 B3单元格放入以下公式,并复制到下面的单元格。
=INDEX([Excel File1.xlsx]Sheet1!$A$3:$B$8,MATCH($A3,[Excel File1.xlsx]Sheet1!$A$3:$A$8,0),2)
上面的公式是基于文件的名称是" Excel File1.xlsx",数据在" Sheet1"中,并且您的数据从第3行到8.所以相应调整。 INDEX-MATCH组合非常有用,所以我建议你阅读更多关于它的内容,以及做一些练习。
答案 1 :(得分:0)
我有一个古老的模板,可以将RAW表中的数据复制回原始文件。 如果你正在寻找一种方法来循环浏览2个打开文件,这将有所帮助。托管“File1”上的代码并在那里运行,将最后一部分修改为所需的范围。
Dim BookCounter As Integer
Dim wb, FirstWB, SecondWB As Workbook
Set FirstWB = ActiveWorkbook 'set the initial variables
BookCounter = 0
'*********** Looping to check if more than 2 files (FIRST + SECOND) are open ********
For Each wb In Workbooks
BookCounter = BookCounter + 1
Next
If BookCounter > 2 Then
MsgBox "Please Close all unnecessary files!" & vbNewLine & vbNewLine & "Make sure you keep only 2 files open while running this Macro.", vbOKOnly + vbInformation, "Too Many Files Open"
Exit Sub
Else
'nothing
End If
'*********** Looping to set the RAW workbook into your SecondWB variable **************
For Each wb In Workbooks
If wb.FullName <> FirstWB.FullName Then
wb.Activate
Set SecondWB = ActiveWorkbook
End If
Next
'------------- now that you've captured both files, you can move from
'--------------------- one to another with <variable>.activate cmnd, selecting the sheet + range, and copying whatever you need.
Dim FirstSht, SecondSht As Worksheet
FirstWB.Activate
Sheets(1).Select
Set FirstSht = ActiveSheet
SecondWB.Activate
Sheets(1).Select
Set SecondSht = ActiveSheet
'--------------------------- cycling through the "files" (now captured as your var.workbooks) needs to be done with <>.activate cmnd
FirstWB.Activate
FirstSht.Select
FirstSht.Range("A1").Copy
SecondWB.Activate
SecondSht.Select
SecondSht.Range("A100").PasteSpecial Paste:=xlPasteAll