2013-02-01 55 views
2

我不知道如何使用公式在循環中運行字符串。基於現有的VBA函數對許多單元格應用數據驗證

這是我試圖轉換成公式的vba代碼。我想用這個公式來進行各種單元格的數據驗證。

公式:

Function Test(pValue) As Boolean 
    If Len(pValue) < 2 Or Len(pValue) > 99 Then 
     AlphaNumeric = False 
     Exit Function 
    End If 
    LPos = 1 
    LValid_Values = " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ." 
    While LPos <= Len(pValue) 
     LChar = Mid(pValue, LPos, 1) 
     If InStr(LValid_Values, LChar) = 0 Then 
      Test = False 
      Exit Function 
     End If 
     LPos = LPos + 1 
    Wend 
    Test = True 
End Function 

公式的簡單的一部分,我寫道:

=IF(AND(LEN(E4)>1,LEN(E4)<100,____Formula_Here____),TRUE,FALSE) 

我假設,如果我選擇E4:E50000並點擊DataValidation,選擇Custom從然後輸入上面的公式,然後它將自動應用於E4,E5,E6 .. E50000。如果我錯了,請糾正我。

此外,我希望這在Excel 2003和更高版本上運行。

回答

6

填空與

NOT(ISERROR(SUM(SEARCH(MID(E4,ROW($1:$99),1)," ABCDEFGHIJKLMNOPQRSTUVWXYZ.")))) 

這是一個數組公式,所以你會用它輸入CTRL移進。但是,數據驗證的自定義公式(至少在2003年)會自動將其視爲數組公式,因此只需正常輸入。

讓我們從裏到外分解它。 ROW($1:$99)在數組公式中提供了循環機制。這將導致周圍的陣列公式在i = 1, 2, ..., 99的每一個處被評估,因此MID將依次取i的第012個字符E4SEARCH不區分大小寫,所以我們可以省略a..z,因爲我們有A..Z。它返回A..Z.0..9中發現i個字符的位置,否則返回錯誤。 SUM聚合這些位置並會傳播任何錯誤。所以,如果找不到任何字符,整個SUM將是一個錯誤。

請注意,據我所知,您可以在自定義驗證公式中使用UDF,所以我不確定我是否遵循您的反對意見。

+0

使用ROW函數的好方法!我使用VLOOKUP數組公式中的COLUMN函數將一行VLOOKUP變成一個VLOOKUP公式。這個技巧可以幫助我減少大量工作簿的大小,並且我可以使用它來執行從列A到XFD的大範圍VLOOKUP。 – deusxmach1na

0

我當時的想法是用一堆嵌套的替代功能,以減少有效的條目爲「」,做數據驗證,以確保這是「」 。

這裏是嵌套替換。

=SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(B1," ",""),"a",""),"b",""),"c",""),"d",""),"e",""),"f",""),"g",""),"h",""),"i",""),"j",""),"k",""),"l",""),"m",""),"n",""),"o",""),"p",""),"q",""),"r",""),"s",""),"t",""),"u",""),"v",""),"w",""),"x",""),"y",""),"z",""),"A",""),"B",""),"C",""),"D",""),"E",""),"F",""),"G",""),"H",""),"I",""),"J",""),"K",""),"L",""),"M",""),"N",""),"O",""),"P",""),"Q",""),"R",""),"S",""),"T",""),"U",""),"V",""),"W",""),"X",""),"Y",""),"Z",""),".",""),"0",""),"1",""),"2",""),"3",""),"4",""),"5",""),"6",""),"7",""),"8",""),"9","") 

事實證明,沒有做你多好,因爲我不是複製粘貼到數據驗證,但一切都沒有失去我的VBA寫了這個。

Sub jf() 
Dim lValid_values As String 
Dim rowcount As Long 
lValid_values = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ." 
Dim form As String 
Dim desired_column As String 
Dim desired_rowMin As Long 
Dim desired_rowMax As Long 
Dim InitialCell As String 
Dim temp As String 
desired_column = "E" 
desired_rowMin = 4 
desired_rowMax = 50000 
InitialCell = desired_column & desired_rowMin 
form = "substitute(||cell||," & """" & " " & """" & "," & """" & """" & ")" 

Do While lValid_values <> "" 
form = "substitute(" & form & "," & """" & Left(lValid_values, 1) & """" & "," & """" & """" & ")" 
lValid_values = Right(lValid_values, Len(lValid_values) - 1) 
Loop 
form = "=AND(LEN(||cell||)>1,LEN(||cell||)<100," & form & "=" & """" & """" & ")" 

For rowcount = desired_rowMin To desired_rowMax 
temp = Replace(form, "||cell||", desired_column & rowcount) 

With Range(desired_column & rowcount).Validation 
    .Delete 
    .Add Type:=xlValidateCustom, AlertStyle:=xlValidAlertStop, Operator:= _ 
    xlBetween, Formula1:=temp 
    .IgnoreBlank = True 
    .InCellDropdown = True 
    .InputTitle = "" 
    .ErrorTitle = "" 
    .InputMessage = "" 
    .ErrorMessage = "" 
    .ShowInput = True 
    .ShowError = True 
End With 
Next rowcount 
End Sub 

我開始運行這個上升到50000,但它很慢,所以我停止了它在4000左右,它工作,所以希望這不是你所要做不止一次東西。您必須更改desired_column,desired_rowmin和desired_rowmax變量以滿足您的需求。如果使用工作表中的

0

我發現這樣做的方式是使用助手列。

假設你有你的數據在E4:E50000

F4將​​
複製此值下降到年底,所以你現在有你的助手列(如果你想檢查,最後一個是=Test(E50000)

然後,數據驗證自定義公式將爲=F4(當您在範圍E4:E50000中選擇時輸入此值,它將複製它並調整引用以指向它旁邊的相應單元格)。

你的代碼也有錯誤 - 你有AlphaNumeric = False,應該是Test = False。設置選項顯式將有助於查找這些錯誤。


你的日常我在我的測試中使用電子表格的略微修改後的版本:

Option Explicit 

Function Test(pValue) As Boolean 
Dim lPos As Long 
Dim lValid_Values As String 
Dim LChar As String 

If Len(pValue) < 2 Or Len(pValue) > 99 Then 
    Test = False 
    Exit Function 
End If 
lPos = 1 
lValid_Values = " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ." 
While lPos <= Len(pValue) 
    LChar = Mid(pValue, lPos, 1) 
    If InStr(lValid_Values, LChar) = 0 Then 
     Test = False 
     Exit Function 
    End If 
    lPos = lPos + 1 
Wend 
Test = True 
End Function