简体   繁体   English

EXCEL VBA:遍历列和复制

[英]EXCEL VBA: looping through columns and copying

In excel I have four columns. 在Excel中,我有四列。 There are numbers in the first column, the second column is blank, the third also contains numbers and the fourth contains text. 第一列有数字,第二列为空白,第三列也包含数字,第四列包含文本。

I want to check each value in the first column and check if it exists in the third column. 我想检查第一列中的每个值,并检查它是否存在于第三列中。 If it does the value in the fourth column next to the corresponding third column should be copied up to the second column next to the corresponding first column. 如果是这样,应将对应的第三列旁边的第四列中的值复制到对应的第一列旁边的第二列中。

I am getting the error compile error. 我收到错误编译错误。 Next without For. 下一个没有For。 Here is my code so far: 到目前为止,这是我的代码:

Sub Compare()

    Dim colA As Integer, colB As Integer

    colA = Columns("A:A").Rows.Count
    colB = Columns("C:C").Rows.Count


        For I = 2 To colA 'loop through column A

            For j = 2 To colB 'loop through column C

                ' If a match is found:
                If Worksheets("Sheet1").Cells(I, 1) = Workshee("Sheet1").Cells(j, 3) Then
                    ' Copy 
                    Worksheets("Sheet1").Cells(j, 4) = Worksheets("Sheet1").Cells(I, 2)
                    'Exit For

            Next j

        Next I

End Sub

As already pointed out in the comments above you could also accomplish this with a VLookUp or a combination of INDEX/MATCH . 正如上面的注释中已经指出的那样,您还可以使用VLookUpINDEX/MATCH的组合来完成此操作。 Yet, if you wish to stick with VBA then you should adjust your code a bit. 但是,如果您希望使用VBA,则应该稍微调整一下代码。

Option Explicit

Sub Compare()

Dim ws As Worksheet
Dim i As Long, j As Long
Dim colA As Long, colC As Long

Set ws = ThisWorkbook.Worksheets("Sheet1")
colA = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
colC = ws.Cells(ws.Rows.Count, "C").End(xlUp).Row

'loop through column A
For i = 2 To colA
    'loop through column C
    For j = 2 To colC
        ' If a match is found:
        If ws.Cells(i, 1).Value2 = ws.Cells(j, 3).Value2 Then
            ' Copy column B to Column D as written in your code above
            ws.Cells(j, 4).Value2 = ws.Cells(i, 2).Value2
            ' or copy column D to Column B as written in the question / post
            ws.Cells(i, 2).Value2 = ws.Cells(j, 4).Value2
            'Exit For
        End If
    Next j
Next i

ws.Range("D2:D" & colC).FormulaR1C1 = "=INDEX(R2C2:R" & colA & "C2,MATCH(RC[-1],R2C1:R" & colA & "C1,0))"

End Sub

The above code will do both: 上面的代码将同时执行以下操作:

  1. the VBA way and VBA方式和
  2. write the INDEX/MATCH formulas for you. 为您编写INDEX/MATCH公式。

Just delete the code segment you don't want. 只需删除不需要的代码段即可。

If you insist on using your code, then use this fixed version. 如果您坚持使用代码,请使用此固定版本。 It should work fine though it's untested. 尽管未经测试,它应该可以正常工作。

Sub Compare()
Dim LastRowA As Long, LastRowB As Long, i As Long, j As Long

With Worksheets("Sheet1")
    LastRowA = .Range("A" & Rows.Count).End(xlUp).Row
    LastRowC = .Range("C" & Rows.Count).End(xlUp).Row

    For i = 2 To LastRowA

        For j = 2 To LastRowC

                If .Cells(i, 1) = .Cells(j, 3) Then .Cells(i, 2) = .Cells(j, 4): Exit For

        Next j

    Next i

End With

End Sub

Let me know in the comment section if there's any error. 如果有任何错误,请在评论部分告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM