使用 IF - REFRESH 时提取 VBA 中的数字
EXtracting number in VBA while using IF - REFRESH
我想提取字符串中的数字并将此数字应用于 IF 函数,但如果源行发生更改,包含 VBA 函数的 If 函数不会刷新。
例子>
源行= 10VBA
Public Function JustNumber(ByVal vValor As String) As String
Application.Volatile True
Dim vQtdeCaract As Long
Dim vControle As Boolean
vQtdeCaract = Len(vValor)
vControle = False
For i = 1 To vQtdeCaract
If IsNumeric(Mid(vValor, i, 1)) Then
If vControle = True And JustNumber <> vbNullString Then
JustNumber = JustNumber + "/"
End If
vControle = False
JustNumber = JustNumber & Mid(vValor, i, 1)
Else
vControle = True
End If
Next
End Function
returns数10
=IF(JustNumber(source row)=10;"True";"False")
Returns“正确”
但是,如果我更改源行,IF 函数不会改变,有一种方法可以在源行更改为 15VBA 后刷新函数,使函数 = "False " ?
一些建议:
- 将您的过程和变量命名为有意义的东西
- 在模块顶部使用
Option Explicit
一些调整:
- 您正在从函数结果中获取一个字符串(您可以更改函数结果或在 Excel 公式中将数字计算为字符串)
- 我简化了提取数字的循环
代码:
Public Function numbersFromString(ByVal evalString As String) As Double
Application.Volatile True
' Get string length
Dim stringLength As Integer
stringLength = Len(evalString)
' Loop through characters in string and extract numbers
Dim resultString As String
Dim counter As Long
For counter = 1 To stringLength
If IsNumeric(Mid(evalString, counter, 1)) Then resultString = resultString & Mid(evalString, counter, 1)
Next counter
' Check if there are any numbers
If Len(resultString) > 0 Then
' Convert to number
numbersFromString = CDbl(resultString)
End If
End Function
我想提取字符串中的数字并将此数字应用于 IF 函数,但如果源行发生更改,包含 VBA 函数的 If 函数不会刷新。
例子> 源行= 10VBA
Public Function JustNumber(ByVal vValor As String) As String
Application.Volatile True
Dim vQtdeCaract As Long
Dim vControle As Boolean
vQtdeCaract = Len(vValor)
vControle = False
For i = 1 To vQtdeCaract
If IsNumeric(Mid(vValor, i, 1)) Then
If vControle = True And JustNumber <> vbNullString Then
JustNumber = JustNumber + "/"
End If
vControle = False
JustNumber = JustNumber & Mid(vValor, i, 1)
Else
vControle = True
End If
Next
End Function
returns数10
=IF(JustNumber(source row)=10;"True";"False")
Returns“正确”
但是,如果我更改源行,IF 函数不会改变,有一种方法可以在源行更改为 15VBA 后刷新函数,使函数 = "False " ?
一些建议:
- 将您的过程和变量命名为有意义的东西
- 在模块顶部使用
Option Explicit
一些调整:
- 您正在从函数结果中获取一个字符串(您可以更改函数结果或在 Excel 公式中将数字计算为字符串)
- 我简化了提取数字的循环
代码:
Public Function numbersFromString(ByVal evalString As String) As Double
Application.Volatile True
' Get string length
Dim stringLength As Integer
stringLength = Len(evalString)
' Loop through characters in string and extract numbers
Dim resultString As String
Dim counter As Long
For counter = 1 To stringLength
If IsNumeric(Mid(evalString, counter, 1)) Then resultString = resultString & Mid(evalString, counter, 1)
Next counter
' Check if there are any numbers
If Len(resultString) > 0 Then
' Convert to number
numbersFromString = CDbl(resultString)
End If
End Function