将公式转换为vba

时间:2017-09-11 07:43:21

标签: excel vba excel-vba

我有一张评估表。在表格中,我必须查找三列S,T和U.

如果列S包含无效且如果列T和U都填充,则我需要过滤整行。

为此,我尝试了下面的代码。代码对我来说很好。但对于我的其他同事来说,过滤器根据条件不起作用。

列Z被视为标记为true的辅助列,或者根据条件为false。如果列T和U都填充且S无效,则标记为false,其余为真

我在所有模块上使用了显式选项。

有谁能建议,我怎么能克服这个?任何人都可以帮助我编码,而不使用公式

Sub fc()
Dim totalrows As Long
Dim sformula

totalrows = Range("A5").End(xlDown).Row
  With Sheets("Evaluation").Range("Z5:Z" & totalrows)
  ' if the S5 is not equal to invalid and any one of the column T and U are filled, then activate autofilter
    sformula = "=AND(S5<>""Invalid"",OR(ISBLANK(T5),ISBLANK(U5)))"
     .Formula = sformula
    .AutoFilter 26, True
    .Value = .Value
  End With
End Sub

2 个答案:

答案 0 :(得分:2)

我刚试了一下,根据我的经验,以下内容应该有效:

Sub fc()
Dim totalrows As Long, sformula AS String
sformula = "=AND(S5<>""Invalid"",OR(ISBLANK(T5),ISBLANK(U5)))"

totalrows = Range("A5").End(xlDown).Row
  With Sheets("Evaluation").Range("Z5:Z" & totalrows)
    .Formula = sformula
    .AutoFilter 1, True ' take the first column of the current range
  End With
End Sub

在您的版本中,您使用了 工作的.AutoFilter 26, True,是否已将其应用于范围[a1:z<no of rows>],但是因为您正在处理范围[z1:z<no of rows>]您只有 范围内的一列。

我也不知道你要用.value = .value来实现什么。据我所知,这只会用当前值i替换当前值。即什么都没用,或者我错过了什么?

答案 1 :(得分:1)

Sub fc()
Dim totalrows As Long, sformula As String

totalrows = Range("A5").End(xlDown).Row
sformula = "=AND(S5<>""Invalid"",OR(ISBLANK(T5),ISBLANK(U5)))"

Application.ScreenUpdating = False

With Sheets("Evaluation").Range("Z5:Z" & totalrows)
     .Formula = sformula
     .Calculate
     DoEvents
     .Value = .Value
    .AutoFilter 26, True
End With

Application.ScreenUpdating = True

End Sub