使用vba

时间:2018-11-12 11:17:15

标签: excel vba vbscript

我有一个excel文件,其中在特定列上使用了过滤器。之后,它返回了我3条可见的行。现在,我想从同一列的可见3行中提取一个单元格值。如何为此编写VBA代码。

注意:我正在使用UFT,vb脚本连接excel应用程序。

Environment.value("Path1")="C:Test\Data1\"
Environment.value("FileName")="ExcelTest.xlsx"
Set obj = CreateObject("Excel.Application")
obj.visible=True  
Set obj1 = obj.Workbooks.Open(Environment("Path1")&Environment("FileName"))
Set obj2=obj1.Worksheets("RESULT")
obj2.Range("L1").Autofilter 12,"abcdef"
obj2.Range("A1").Autofilter 1,Array("Bucket",2,"Material","Flags"),7
rows=obj2.usedrange.columns(1).specialcells(12).count-1

1 个答案:

答案 0 :(得分:0)

如果您只想使用可见的单元格。

当然,您可以在一个示例中对A列进行过滤,以适合您的数据:

    Sub test()


Dim ws As Worksheet
Dim i As Long, LastRow As Long
Dim r As Range, Cell As Range, Range As Range

Set ws = ThisWorkbook.Sheets("Sheet1")
Set r = ws.Range("A1")

 ws.AutoFilterMode = False
    With r
        .AutoFilter Field:=1, Criteria1:="Yourcriteria"
        LastRow = ws.Cells(Rows.Count, 1).End(xlUp).Row
        Set Range = ws.Range(ws.Cells(2, 1), ws.Cells(LastRow, 1))

        For Each Cell In Range.SpecialCells(xlCellTypeVisible)
            'whatever you need to be done
        Next Cell

    End With
    ws.AutoFilterMode = False  
End Sub