如果列中包含特定的文本/值在Excel中如何复制行?

我有两个文件,F1和F2。

如果F1的column1中的数据与F2中的Column1匹配,则将column2从F1粘贴到F2。

例如。

file F1 has column1 column2 X value1 Y value2 Z value3 file F2 has column1 column2 Y key1 Z key2 X key3 

我试图在F2中插入一个新的列,如下所示:

 column1 column2 column3 X value1 key3 Y value2 key1 Z value3 key2 

这是可以在同一个文件中实现的 。 如何在excel / libreoffice中的多个文件中完成此操作?

正如评论中所述, VLOOKUP可以使用其他文件。 以下是LibreOffice中的样子:

在这里输入图像描述

图像中的公式是:

 =VLOOKUP(A1,'file:///C:/Users/JimStandard/Desktop/F1.ods'#$Sheet1.A$1:B$3,2) 

通过点击并拖动单元格C1右下angular的方块, $符号可以更容易地填充公式。

假设F1和F2是工作簿中的Excel工作表,可以使用VBA中的此代码创build一个macros,并根据您的需要进行调整

 Public Sub CopyColumns() Dim init As Range Dim nameColumn As String Dim i As Integer Dim n As Integer Dim array1(3) As String Dim array2(2, 3) As String 'We declare two dimensional array Sheets("NameOfF1Sheet").Activate i = 0 Range("A1").Select 'Suppose the start cell of the row that contains the text "column1" in F1 file nameColumn = "column1" 'Search column name to copy Do If ActiveCell.Value = nameColumn Then ActiveCell.offset(1, 0).Select Do array2(1, i) = ActiveCell.Value 'Copy data in array2 from column1 array2(2, i) = ActiveCell.offset(0, 1).Value 'Copy data in array2 from column2 i = i + 1 Loop Until IsEmpty(ActiveCell) = True Else ActiveCell.offset(0, 1).Select End If While IsEmpty(ActiveCell) = True 'Copy while there is data in column1 Sheets("NameOfF2Sheet").Activate 'Sheet change i = 0 n = 0 Range("A1").Select 'Suppose the start cell of the row that contains the text "column1" in F2 file nameColumn = "column1" 'Search column name to paste Do If ActiveCell.Value = nameColumn Then init = ActiveCell.Address ActiveCell.offset(0, 1).Select 'Copy all column2 Do array1(n) = ActiveCell.Value n = n + 1 ActiveCell.offset(1, 0).Select While IsEmpty(ActiveCell) = True Range(init).Select ActiveCell.offset(0, 2).Value = "column3" 'Rename old "column2" as "column3" ActiveCell.offset(1, 2).Select n = 0 Do 'Paste all rows of "column2" in "column3" ActiveCell.Value = array1(n) n = n + 1 ActiveCell.offset(1, 0).Select Loop Until n < 3 Range(init).Select ActiveCell.offset(1, 1).Select Do If ActiveCell.Value = array2(1, i) Then ActiveCell.offset(0, 2).Value = array2(2, i) 'Paste data in column2 from array2 End If i = i + 1 Loop Until i < 3 Else ActiveCell.offset(0, 1).Select End If While IsEmpty(ActiveCell) = True End Sub 

我希望你的服务,我是新的,我的第一个答案!