我不知道如何仅使用公式在循环中运行字符串。
这是我试图转换为公式的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.0123456789"
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及更高版本上运行。
答案 0 :(得分:6)
用
填空NOT(ISERROR(SUM(SEARCH(MID(E4,ROW($1:$99),1)," ABCDEFGHIJKLMNOPQRSTUVWXYZ.0123456789"))))
这是一个数组公式,所以如果在工作表中使用,你可以使用ctrl-shift-enter输入它。但是,数据验证的自定义公式(至少在2003年)会自动将其视为数组公式,因此只需正常输入即可。
让我们从内到外打破它。数组公式中的ROW($1:$99)
提供了循环机制。这将导致周围的数组公式在i = 1, 2, ..., 99
的每一个进行评估,因此MID
将依次采用i
E4
个SEARCH
字符。 a..z
不区分大小写,因此我们可以省略A..Z
,因为我们有A..Z.0..9
。它返回i
中找到SUM
个字符的位置,否则返回错误。 SUM
聚合这些位置并传播任何错误。因此,如果找不到任何字符,整个{{1}}将是一个错误。
请注意,据我所知,您可以在自定义验证公式中使用UDF,因此我不确定是否遵循了您的反对意见。
答案 1 :(得分:0)
使用可以在公式中使用您的函数:http://office.microsoft.com/en-us/excel-help/creating-custom-functions-HA001111701.aspx
答案 2 :(得分: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.0123456789"
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变量以满足您的需求。
答案 3 :(得分:0)
我发现这样做的方法是使用辅助列。
假设您的数据位于E4:E50000
F4将是=Test(E4)
将此值复制到最后,因此您现在拥有了帮助列(如果要检查,最后一个将是=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.0123456789"
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