在VBA中使用列名作为范围

时间:2017-01-11 01:06:15

标签: vba excel-vba excel

我有以下代码,使用col范围作为值搜索工作表中的列。而不是那样,我需要使用col名称(在该表中)作为搜索条件并执行相同的功能。我该如何实现呢?

Sub WBR()
Dim Count1Criteria As Variant
Dim Count3Criteria As Variant
Dim test As Variant
Dim wf As WorksheetFunction
Set wf = Application.WorksheetFunction

Filter1InSummary = Array(Array("AE4", "Latency", "O:O", "Pass"), _
                     Array("AE51", "TT", "G:G", "Yes"), _
                     Array("AE52", "TT", "G:G", "No"), _
                     Array("AE61", "Reactive", "R:R", "Item"))

Filter3InSummary = Array(Array("AE43", "TT", "I:I", "<>Duplicate TT", _
                                         "G:G", "<>Not Tested", _
                                         "U:U", "Item"))
For Each test In Filter3InSummary
With Worksheets(test(1))
    Range(test(0)) = wf.CountIfs(.Range(test(2)), test(3), _
                                 .Range(test(4)), test(5), _
                                 .Range(test(6)), test(7))
End With
Next

1 个答案:

答案 0 :(得分:0)

假设列名称位于工作表的第一行,请使用列名替换数组中的2,4,6个元素。

Filter3InSummary = Array(Array("AE43", "TT", "CName1", "<>Duplicate TT", _
                                         "CName2", "<>Not Tested", _
                                         "CName3", "Item"))

然后用以下代码替换公式中的.Range(test(2))

.rows(1).Find(test(2),lookat:xlWhole).EntireColumn

并为4和6做同样的事。

请注意列名必须出现在指定行中,否则会失败。