2017-09-11 122 views
0

我有一张评估表。在表中,我必须寻找S,T和U三列。将公式转换成vba

如果列S包含无效并且列T和U都填满了,那么我需要筛选整行。

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

列Z被视为辅助列,根据条件标记为真或假。如果列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 
+1

只要将.Value = .Value移动到自动筛选器上并检查。 – Sixthsense

+0

@Sixthsense有没有其他方法?我试图与它合作 – Jenny

回答

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代替当前值。即没有用处,或者我错过了什么?

+0

我会尝试这个并回复你。你能否给我提供这个变化背后的原因,这将是有帮助的 – Jenny

+0

通过在列1中的范围中应用自动填充器,所有内容都被过滤了, – Jenny

+1

嗯,不在我的例子中:'1'是以相对方式解释的,看'z'列。至少在我的Excel工作表中。当我尝试使用大于1的数字时,出现'Run tim error 1004:Range class failed失败的AutoFilter方法。所以我*只能使用'.AutoFilter 1,True'。 – cars10m

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 
+0

我不明白,你的代码在我的机器上工作得很好。但它不适用于我的朋友笔记本电脑。我们都使用相同的EXcel版本2013.可能是什么原因?你能帮我弄清楚这个问题吗? – Jenny