如果列在 Excel 中包含特定的 text/value,如何复制行?

How to copy rows if column contains specific text/value in 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

这是同一文件中的 achievable。如何跨 excel/libreoffice 中的多个文件完成此操作?

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

图中的公式为:

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

通过单击并拖动单元格 C1 lower-right 角的正方形,$ 符号可以更轻松地向下填充公式。

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

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

希望大家多多指教,我是新人,第一次回答!