Excel VBA匹配并排列行

我有列A到J的Excel文档。我有列K到N与相关的数据,但不alignment。

我需要将列F中的值与列K中的值匹配,以便排列在一起。 当我移K时,我必须将L,M,N一起移动。

我不能排列列A到J – 他们必须保持在位。

之前的示例:

ABCDEFGHIJKLMN data data data data data record1 data data data data record3 data data data data data data data data record2 data data data data record1 data data data data data data data data record3 data data data data data data data data data record4 data data data data 

之后的示例:

 ABCDEFGHIJKLMN data data data data data record1 data data data data record1 data data data data data data data data record2 data data data data data data data data data record3 data data data data record3 data data data data data data data data record4 data data data data 

最简单的方法可能是ADO。

 Dim cn As Object Dim rs As Object Dim strFile As String Dim strCon As String Dim strSQL As String Dim s As String Dim i As Integer, j As Integer ''This is not the best way to refer to the workbook ''you want, but it is very convenient for notes ''It is probably best to use the name of the workbook. strFile = ActiveWorkbook.FullName ''Note that if HDR=No, F1,F2 etc are used for column names, ''if HDR=Yes, the names in the first row of the range ''can be used. ''This is the Jet 4 connection string, you can get more ''here : http://www.connectionstrings.com/excel strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strFile _ & ";Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1"";" ''Late binding, so no reference is needed Set cn = CreateObject("ADODB.Connection") Set rs = CreateObject("ADODB.Recordset") cn.Open strCon strSQL = "SELECT * " _ & "FROM [Sheet2$A1:J5] a " _ & "LEFT JOIN [Sheet2$K1:N5] b " _ & "ON aF=bk " rs.Open strSQL, cn, 3, 3 ''Pick a suitable empty worksheet for the results Worksheets("Sheet3").Cells(2, 1).CopyFromRecordset rs ''Tidy up rs.Close Set rs = Nothing cn.Close Set cn = Nothing 

对列K – M进行sorting并将其移动得更远(如XZ)

在列KM中添加一个VLOOKUP()函数,根据F列从XZ中提取数据。

使用INDEX()函数,只要find将XZ中的数据用MATCH()函数复制到KL,就可以把它拉出来。 否则返回一个空string。

列AJ与数据,列F包含查找值

包含循环匹配值的包含X的参考表的列XZ

=MATCH((Value in F),(XYZ Table),FALSE)添加一列N,产生一个#N/A或一个行号

列K =IF( NOT( ISNA(Value in N) ), INDEX((X Table), (Value in N) ), "")

列L =IF( NOT( ISNA(Value in N) ), INDEX((Y Table), (Value in N) ), "")

列M =IF( NOT( ISNA(Value in N) ), INDEX((Z Table), (Value in N) ), "")

除非你想在VBA中做到这一点,