简体   繁体   English

Vlookup使用VBA for Excel过滤数据

[英]Vlookup against filtered data with VBA for Excel

Sub FilteredTest()

    Dim LastRow1 As Long
    Dim LastRow2 As Long
    Dim myI As Long
    Dim myLookupvalue As Long
    Dim myTableArray As Range

    LastRow1 = Worksheets(2).Cells(Cells.Rows.Count, "A").End(xlUp).Row
    LastRow2 = Worksheets(8).Cells(Cells.Rows.Count, "A").End(xlUp).Row

    Set myTableArray = Worksheets(2).Range("A2:A" & LastRow1)

    myI = 3

    Do Until myI > LastRow2

        myLookupvalue = Worksheets(8).Range("E" & myI)
        On Error Resume Next
        Worksheets(8).Range("H" & myI).Value = WorksheetFunction.VLookup(myLookupvalue, myTableArray, 1, False)
        ' Error 1004 is when the VLOOKUP can't find a corresponding value
        If Err = 1004 Then
            Worksheets(8).Range("H" & myI).Value = "Remove"
        End If
        myI = myI + 1

    Loop
End Sub

I need some help getting this code to only Vlookup against visible values in another sheet. 我需要一些帮助,以使此代码仅针对另一个工作表中的可见值Vlookup。

Basically what I want to do, is have column H# in the main sheet lookup values in column A in another data-sheet and give a regular Iferror w/ Vlookup output in column E# at the main sheet. 基本上,我想做的是在另一张数据表的A列的主表查找值中包含H#列,并在主表的E#列中提供常规的Iferror w / Vlookup输出。

I've tried several placements of the SpecialCells(xlCellTypeVisible) -function both outside the loop and inside the loop, but nothing I do seems to work. 我已经尝试了在循环外和循环内对SpecialCells(xlCellTypeVisible) -函数进行多次放置,但是我似乎无济于事。 All I get are Errors in the code or errors on the Vlookup. 我得到的只是代码中的错误或Vlookup上的错误。 I've tried searching on this site and googling like a madman. 我尝试在此网站上搜索并像疯子一样谷歌搜索。 Threw in the towel at this point and decided to start a thread on my own. 在这一点上扔毛巾,并决定自己开始线程。 Hope someone can help me integrate the function to my code and/or help me understand this function better. 希望有人可以帮助我将该功能集成到我的代码中和/或帮助我更好地理解此功能。

You might add this User Defined Function and swap out WorksheetFunction.VLookup for this new 'helper' function. 您可以添加此用户定义的函数,并将WorksheetFunction.VLookup换出此新的“帮助”函数。

Option Explicit

Function VisLookup(lu As Variant, rng As Range, col As Long, _
                   Optional bin As Boolean = False) As Variant

    Dim i As Long

    Set rng = Intersect(rng, rng.Parent.UsedRange)
    VisLookup = CVErr(xlErrNA)
    If col > rng.Columns.Count Then Exit Function

    If bin Then

        For i = 1 To rng.Rows.Count
            If rng.Cells(i + 1, "A").Value2 > lu And Not rng.Rows(i).Hidden Then
                VisLookup = rng.Cells(i, col).Value
                Exit For
            End If
        Next i

    Else

        For i = 1 To rng.Rows.Count
            If lu = rng.Cells(i, "A").Value2 And Not rng.Rows(i).Hidden Then
                VisLookup = rng.Cells(i, col).Value
                Exit For
            End If
        Next i

    End If

End Function

Implemented as, 实施为

Worksheets(8).Range("H" & myI).Value = VisLookup(myLookupvalue, myTableArray, 1, False)

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

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