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

时间:2016-01-25 16:28:06

标签: excel libreoffice-calc

我有两个文件,比如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中的多个文件中完成此操作?

2 个答案:

答案 0 :(得分:2)

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

enter image description here

图片中的公式为:

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

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

答案 1 :(得分:1)

假设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

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