簡體   English   中英

Excel VBA:索引匹配多個條件

[英]Excel VBA: Index-Match Multiple Criteria

我試圖產生一個VBA函數,該函數將隱藏單元格D9:CC9不等於“ A6”且單元格D8:CC8不等於“ 12”的所有列。 根據以下腳本,系統會不斷返回錯誤。 我是VBA的新手,希望有人可以提供幫助。

謝謝!

Dim MyCell As Range
       Set MyCell = Range("D9:CC9,D8:CC8")
             For Each cell In MyCell
                   If cell.Value <> WorksheetFunction.Index(Range("D9:CC9"),WorksheetFunction.Match(Range("A6")&"12",Range("D9:CC9")&Range("D8:CC8"), 0))
                        cell.EntireColumn.Hidden = True

                   End If

             Next cell

使用VBA進行的大多數操作與手動執行的操作完全不同。 如果要使用VBA,則應進行一些有關使用變量和對象的研究。 該頁面應該很有趣。

變量和常量Excel對象具有語句范圍屬性(Excel)

我對您的代碼進行了一些更改,請查看其中的注釋,並參考上面提到的頁面。

Sub Rng_HideColumns()
Dim rTrg As Range, rCol As Range
Dim sCllVal As String
    Rem Set rTrg = ThisWorkbook.Sheets("Sht(0)").Range("D9:CC9,D8:CC8")
    Rem Refers to the worksheet you want to work with instead or using the active worksheet
    With ThisWorkbook.Sheets("Sht(0)")

        Rem Get value to match
        sCllVal = .Range("A6").Value2 & 12

        Rem Set Range to Search
        Set rTrg = .Range("D8:CC9")
        For Each rCol In rTrg.Columns
            With rCol
                If .Cells(2).Value2 & .Cells(1).Value2 = sCllVal Then
                    .EntireColumn.Hidden = 1
                Else
                    .EntireColumn.Hidden = 0
    End If: End With: Next: End With
End Sub

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM