2014-01-27 647 views
2

我在用短划线分隔的单元格中有多个项目。我试图通过分割行来规范数据库,以便每行仅包含一个条目。如何在Excel VBA中查找/计算字符串?我知道你可以做整个细胞的值与如何使用Excel VBA计算单元格中特定字符的数量

myVar = Application.WorksheetFunction.COUNTIF(Range("A1:Z100"),"Value") 

我需要搜索一个单元格,并找出有多少连字符。实施例

123-456-789 = 2 
9876-12 = 1 
+0

你只想分割它们吗?那么就没有必要计算我猜测的猜想了。你只需要'分割'功能。 – L42

+0

我需要在单独的列中包含计数。 – parap

+0

啊,我在想2个可能的解决方案。 1是'UDF',第二个是'Array Formula'。等待,为两个生成示例... – L42

回答

0

这里的UDF在串计数单个字符串occurence:

Option Explicit 
Function COUNTTEXT(ref_value As Range, ref_string As String) As Long 

Dim i As Integer, count As Integer 

count = 0 
If Len(ref_string) <> 1 Then COUNTTEXT = CVErr(xlErrValue): Exit Function 
For i = 1 To Len(ref_value.value) 
    If Mid(ref_value, i, 1) = ref_string Then count = count + 1 
Next 

COUNTTEXT = count 

End Function 

下面是使用Array式:

=SUM(IF(ISERROR(SEARCH("-",MID(A1,ROW(INDIRECT("$1:$" & LEN(A1))),1))),0,1)) 

进入使用Ctrl键 + + 输入
希望这会有所帮助。

2

这将算在activecell

Sub test() 
    a = Len(ActiveCell) 
    my_txt = Replace(ActiveCell, "-", "", 1, -1, vbTextCompare) 
    b = Len(my_txt) 
    numb_occur = a - b 
End Sub 
0

连字符的数目。这些代码可能对你有帮助..你也可以使用它作为一个UDF ... :)

Function CountHypens(rng_Src As Range) As Long 

'A VARIANT FOR SPLITTING CELL CONTENTS 
Dim var As Variant 

On Error Resume Next 
var = Split(rng_Src.Value, "-", , vbTextCompare) 

If Err.Number <> 0 Then 
    Debug.Print "This cell does not have any hyphens." 
Else 
    CountHypens = UBound(var) 
    End If 
Err.Clear: On Error GoTo 0 

End Function 
6

使用上面的ron's function提示我创建了这个公式,它工作正常:

=LEN(A1) - LEN(SUBSTITUTE(A1, "-", "")) 
0

我发现这个答案:

Sub xcountCHARtestb() 
    'If countCHAR(RANGE("aq528"), ".") > 0 Then 'YES 
    If countCHAR(Selection, ".") > 0 Then 'YES 
     MsgBox "YES" & Space(10), vbQuestion ', "title" 
    Else 
     MsgBox "NO" & Space(10), vbQuestion ', "title" 
    End If 
End Sub 


Sub xcountCHARtesta() 'YES 
    MsgBox "There are " & countCHAR(Selection, "test") & " repetitions of the character string", vbQuestion 'YES 
End Sub 

Function countCHAR(myString As String, myCHAR As String) As Integer 'as: If countCHAR(Selection, ".") > 1 Then selection OR RANGE("aq528") '"any char string" 
    countCHAR = UBound(split(myString, myCHAR)) 'YES 
End Function 
0

后续行动:DAVEX,通过DAVEX .. :)

我一直在寻找所有试图找到一种方法来测试相同的公式中查找文本字符串。 这个答案似乎适用于这两个公式/不&适合1班轮.. (我仍然很新手在vba,让我知道如果有更好的方式)s)感谢。

If countChar(UCase(Selection.Formula), UCase("offset")) > 0 Then 'YES (thee? answer, works for both formulas/not) 
'If countChar(Selection.Formula, "OFFSET") > 0 Then 'yes 
'If countChar(Cells(ActiveCell.row, Selection.Column).Formula, "OFFSET") > 0 Then 'yes 
'If countChar(Cells(ActiveCell.row, "BG").Formula, "OFFSET") > 0 Then 'yes 
'If countChar(UCase(Selection), UCase("OffSET")) > 0 Then 'yes but not work on formula 
'If Selection.Formula Like "*offset*" Then 'no (for eq) 
MsgBox "YES" & Space(15), vbQuestion 
Else 
MsgBox "NO" & Space(15), vbQuestion 
End If 

注:在地方变量的 “BG” 上面,我使用永久工作单元,以改善柱BG实例的使用,工作单元A3有/节目:BG:BG

=SUBSTITUTE(SUBSTITUTE(CELL("address",$BG3),"$",""),ROW(),"")&":"&SUBSTITUTE(SUBSTITUTE(CELL("address",$BG3),"$",""),ROW(),"") 

你会还需要昏黄的工作单元,顶部/ VBA的面前:

Dim A3 As String 
A3 = RANGE("A3") 

原谅,尝试3次获得所有的代码到1盒。真的建议在工具栏中放置一个代码停止开始图标。

+0

您的代码没有正确格式化/缩进,请修复它。 –

相关问题