Excel VBA:在两张工作表中给定列中的每个单元格进行循环vlookup

时间:2018-08-19 03:43:15

标签: vba excel-vba

基本上,我想浏览特定列中的单元格,看它是否适合其他两个可能的工作表中的属性(在该数据库的列中用“ x”表示)。

如果它包含/具有该属性(vlookup的结果为“ x”),则将原始单元格的值粘贴到另一个单元格中。

我想为每一行的两个属性执行此操作。因此,由于必须在2个工作表中搜索2个可能的属性,因此循环内有4个If / Then语句。

我为这两个属性添加了“计数器”,以便在列表中找到具有属性的项目时,确保以后再找到其他项目时,将其插入其下一行而不是覆盖它

我的代码运行时没有出现错误,但是没有产生任何结果。抱歉,我很抱歉!

Sub ASSESS()

Dim counterF As Integer
Dim counterS As Integer
Dim cell As Range
Dim Ret As String

'These are counters to start logging the desired cells in rows 6 and 10, respectively, depending on if it fit attribute F or S

counterF = 6
counterS = 10

For Each cell In Worksheets("Sheet1").Range("C14:C64")

    Ret = CStr(Application.VLookup(cell, Worksheets("Activities").Range("B2:U1000"), 13, 0))

    If Ret = "x" Then
        Worksheets("Sheet1").Cells(counterS, 3) = cell
        counterS = counterS + 1
        Ret = ""
    Else
    End If

    Ret = CStr(Application.VLookup(cell, Worksheets("Song List").Range("B2:U1000"), 13, 0))

    If Ret = "x" Then
        Worksheets("Sheet1").Cells(counterS, 3) = cell
        counterS = counterS + 1
        Ret = ""
    Else
    End If

    Ret = CStr(Application.VLookup(cell, Worksheets("Activities").Range("B2:U1000"), 12, 0))

    If Ret = "x" Then
        Worksheets("Sheet1").Cells(counterF, 3) = cell
        counterF = counterF + 1
        Ret = ""
    Else
    End If

    Ret = CStr(Application.VLookup(cell, Worksheets("Song List").Range("B2:U1000"), 12, 0))

    If Ret = "x" Then
        Worksheets("Sheet1").Cells(counterF, 3) = cell
        counterF = counterF + 1
        Ret = ""
    Else
    End If

Next

End Sub

0 个答案:

没有答案