Excel VBA以匹配和排列行

Excel VBA以匹配和排列行,excel,excel-2007,vba,Excel,Excel 2007,Vba,我有一个Excel文档,其中A列到J列。我有K列到N列以及相关数据,但没有对齐 我需要将F列中的值与K列中的值进行匹配,以便它们对齐。当我移动K时,我必须一起移动L,M,N 我无法将A列排序到J列-它们必须保持在原位 之前的示例: 示例如下: 最简单的方法可能是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 D

我有一个Excel文档,其中A列到J列。我有K列到N列以及相关数据,但没有对齐

我需要将F列中的值与K列中的值进行匹配,以便它们对齐。当我移动K时,我必须一起移动L,M,N

我无法将A列排序到J列-它们必须保持在原位

之前的示例:

示例如下:


最简单的方法可能是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 a.F=b.k "

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进行排序,并将它们像在X-Z中那样移动得更远

在K-M列中添加一个VLOOKUP函数,以根据F列从X-Z中提取数据

为了使它更美观,请使用INDEX函数拉出该行,并且仅当找到时,才使用MATCH函数将数据从X-Z复制到K-L。否则返回一个空字符串

A-J列包含数据,F列包含查找值

列X-Z和参考表,其中X包含loopup匹配值

在F、XYZ表中添加一列N,其中值=MATCHValue,FALSE 这将产生一个N/a或行号

K列,如果不是N中的IsNavValue,则为INDEXX表格,值为N

列L,如果不是N中的IsNavValue,索引表,N中的值

M列,如果不是N中的ISNAValue,则为INDEXZ表中的值,N中的值


除非你想在VBA中实现,否则这是可行的。

大家好,这很好,我想我会尝试ADO,我必须通过代码来实现,因为在用K-N填充值之前,我有一些东西。Remou只需问一个问题,我永远不知道行数,所以我不能使用Sheet2$A1:J5和Sheet2$K1:N5。它超过5行,它会时不时地改变。如何指定尽可能多的记录,而不是像recordCount这样的5条记录。提前感谢。您可以使用列引用[Sheet2$a:J]或命名范围&从ANamedRange a Hi Remou,我尝试了您的代码,但在rs上出现错误。Open strSQL,cn,3,3调试器没有为一个或多个必需参数提供任何值。代码是相同的,我做的唯一更改是Sheet2$A1:J5,K-N更改为Sheet1$A1:J10。谢谢你的专栏有标题吗?如果是这样,您应该在这里使用列标题:a.F=b.k,其中F=记录集1键和k=记录集2键的列名称,如果不是,您必须更改连接字符串以显示HDR=No,并对列名使用F1、F2、F3等,这将给出a.F6=b.F11Hi Remou,I有a到J的标题,但不是K到N。所以我更改了代码,得到了相同的错误。下面就是我正在使用的:strCon=Provider=Microsoft.Jet.OLEDB.4.0;数据源=&strFile&;扩展属性=Excel 8.0;HDR=否;IMEX=1;设置cn=CreateObjectADODB.Connection Set rs=CreateObjectADODB.Recordset cn.Open strCon strSQL=SELECT*.&FROM[Sheet1$A:J]A.&LEFT JOIN[Sheet1$K:N]b.&ON A.F6=b.F11 rs.打开strSQL,cn,3,3工作表sheet3.Cells2,1.copyfrom记录集rs
A     B     C     D     E     F        G     H     I     J     K       L     M     N

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  
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 a.F=b.k "

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