나는이 대답을 좋은 질문으로 생각하고 있습니다. 나는 이것이 당신이 원하는 것을 할 것이라고 생각합니다! 십진수와 천 단위 구분 기호로도 작동합니다.
I합니다 (NumericalValue
기능뿐만 아니라 다른 방법으로 생성 될 수 인정 첫 번째와 마지막 숫자를 찾아 문자열의 mid
참여 않습니다.
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rng As Range
Dim rngTarget As Range
Dim varValue As Variant
Set rngTarget = Range("a1:a100")
If Not Intersect(Target, rngTarget) Is Nothing Then
For Each rng In rngTarget
'only check cells that contain an H and a P
If InStr(1, rng, "H") > 0 And InStr(1, rng, "P") > 0 Then
'find the numerical value if any (Empty if not found)
varValue = NumericalValue(rng.Value2)
If Not IsEmpty(varValue) Then
MsgBox "hurray the value of cell " & rng.AddressLocal & " is " & varValue
End If
End If
Next
End If
End Sub
'return the first numerical value found in the cell
Private Function NumericalValue(ByVal strValue As String) As Variant
Dim intChar As Integer
Dim booNumberFound As Boolean
Dim intDecimal As Integer
booNumberFound = False
NumericalValue = Val(strValue)
For intChar = 1 To Len(strValue) Step 1
'if a number found then grow the total numerical value with it
If IsNumeric(Mid(strValue, intChar, 1)) Then
NumericalValue = NumericalValue * IIf(intDecimal = 0, 10, 1) + _
Val(Mid(strValue, intChar, 1)) * 10^-intDecimal
If intDecimal > 0 Then
intDecimal = intDecimal + 1
End If
booNumberFound = True
'if the decimal separator is found then set the decimal switch
ElseIf intDecimal = 0 And booNumberFound = True And Mid(strValue, intChar, 1) = Application.DecimalSeparator Then
intDecimal = 1
'skip the thousand separator to find more numbers
ElseIf booNumberFound = True And Mid(strValue, intChar, 1) = Application.ThousandsSeparator Then
ElseIf booNumberFound = True Then
Exit For
End If
Next intChar
End Function
을하지만, 스크립트는 문자 "H"와 "P"를 확인하고, 이러한 무엇인가는 H와 P가 전에 reaquired 있습니까 숫자 값을 확인하고 싶습니까? –
무엇이'prNg'입니까? 및'rngEachValue'? 거기에'rng'을 사용 하시겠습니까? –
예 이러한 경우 rng (범위)를 의미합니다. "H"& "P"와 관련하여 - 셀에 이러한 문자가 포함되어 있지 않으면 어떤 조치도 취하고 싶지 않습니다. – user1624926