Excel 如何计算单元格中的数值
我有像细胞一样的价值观Excel 如何计算单元格中的数值,excel,vba,Excel,Vba,我有像细胞一样的价值观 1,2,4,45,64,Jan. Ans: 5 我只想数一数牢房里的数字。 有可能吗?以下是我的答案: Sub countingNumbers() Dim c As Range Dim L As Long Dim s As String Dim tmp Dim i Dim y Set c = Range("A1") 'Imagine in A1 is this: "RDFY2372784GDTD2GV3G3G3V3
1,2,4,45,64,Jan. Ans: 5
我只想数一数牢房里的数字。
有可能吗?以下是我的答案:
Sub countingNumbers()
Dim c As Range
Dim L As Long
Dim s As String
Dim tmp
Dim i
Dim y
Set c = Range("A1") 'Imagine in A1 is this: "RDFY2372784GDTD2GV3G3G3V3"
L = Len(c.Value) 'to store the len of the string
s = c.Value 'to store the string
y = 0 'the index of the numbers
For i = 1 To L
tmp = Left(Right(s, i), 1) 'to take just one letter at the time
If Asc(tmp) < 57 And Asc(tmp) > 48 Then
'if is a number ASC() returns the ASCII code of that number
'and increase y one by one || that is number by number
y = y + 1
End If
Next i
Range("B2").Value = y 'store the counting in B2
End Sub
Part of the ASCII Table:
+------------+-----------+
| ASCII Code | Character |
+------------+-----------+
| 48 | 0 |
| 49 | 1 |
| 50 | 2 |
| 51 | 3 |
| 52 | 4 |
| 53 | 5 |
| 54 | 6 |
| 55 | 7 |
| 56 | 8 |
| 57 | 9 |
| 64 | @ |
| 65 | A |
| 66 | B |
| 67 | C |
| 68 | D |
| 69 | E |
| 70 | F |
| 71 | G |
| 72 | H |
| 73 | I |
| 74 | J |
+------------+-----------+
Sub countingNumbers()
调光范围
我和你一样长
像线一样变暗
暗tmp
昏暗的我
暗淡的
设置c=范围(“A1”)'想象A1中是这样的:“RDFY2372784GDTD2GV3G3V3”
L=Len(c.Value)“”来存储字符串的Len
s=c.值“以存储字符串
y=0'数字的索引
对于i=1到L
tmp=左(右)一次只取一个字母
如果Asc(tmp)<57且Asc(tmp)>48,则
'if是一个数字ASC()返回该数字的ASCII码
'然后一个接一个地增加y | | |这是一个接一个的数字
y=y+1
如果结束
接下来我
范围(“B2”)。值=y'将计数存储在B2中
端接头
ASCII表的一部分:
+------------+-----------+
|ASCII码|字符|
+------------+-----------+
| 48 | 0 |
| 49 | 1 |
| 50 | 2 |
| 51 | 3 |
| 52 | 4 |
| 53 | 5 |
| 54 | 6 |
| 55 | 7 |
| 56 | 8 |
| 57 | 9 |
| 64 | @ |
|65 | A|
|66 | B|
|67摄氏度|
|68 | D|
|69 | E|
|70华氏度|
|71 | G|
|72小时|
|73 | I|
|74 | J|
+------------+-----------+
在这里,您可以检查表您还可以在自定义项中使用
拆分
功能:
Function CountNumbers(R As Range) As Integer
Dim values, value As Variant
Dim result As Integer
values = Split(R.Text, ",")
result = 0
For Each value In values
If IsNumeric(value) Then
result = result + 1
End If
Next value
CountNumbers = result
End Function
从这里,您可以在另一个Excel单元格中调用它,如下所示:
=CountNumbers(A1)
虽然应该有所帮助,但您的
Jan
可能是一个带有MMM
格式的数字。。。你拥有什么和需要什么还不完全清楚。。。。你可以指定你有一点问题吗?或者发布一个数据的示例屏幕截图…这将计算单元格中的数字字符。。。至少1到8,cus 0和9不会被您的代码计数。一个简单的IsNumeric(Mid(c.Value,i,1)
将更容易…而这也可以在没有VBA的情况下通过如下数组公式轻松实现:=SUM(IF(ROW(INDIRECT)(&LEN(1))),ISNUMBER(Mid(A1,ROW(INDIRECT)(&LEN(A1))