2017-09-11 121 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