Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel VBA如何在字符串中查找整数并设置为变量_Excel_Vba - Fatal编程技术网

Excel VBA如何在字符串中查找整数并设置为变量

Excel VBA如何在字符串中查找整数并设置为变量,excel,vba,Excel,Vba,我有一个从“目录”页面复制到D列的列表。不幸的是,每个单元格都包含章节号、章节名称和页码 3.14.4章节名称占位符140 有时页码和最后一个字符之间有空格。其他时候没有 我试过了 Function john(txt As String) As Long Dim x x = Split(Trim(txt), Chr(32)) john = Val(x(UBound(x))) End Function 这确实有效,但我希望以后也能将其应用于章节号 Private Sub FIND_LAST_NU

我有一个从“目录”页面复制到D列的列表。不幸的是,每个单元格都包含章节号、章节名称和页码

3.14.4章节名称占位符140

有时页码和最后一个字符之间有空格。其他时候没有

我试过了

Function john(txt As String) As Long
Dim x
x = Split(Trim(txt), Chr(32))
john = Val(x(UBound(x)))
End Function
这确实有效,但我希望以后也能将其应用于章节号

Private Sub FIND_LAST_NUMBER()

Dim A As String
Dim B As Integer
Dim C As String
Dim D As String

x = 3

Do While ActiveSheet.Cells(x, 4).Value <> ""

A = Range("D" & x).Value
A = Trim(A)
B = Len(A)
For Position = B To 1 Step -1
    C = Mid(A, Position, 1)
    'MsgBox C
    If C <> " " Then
        D = Right(A, B - Position)
        Range("E" & x).Value = C
    GoTo LastLine
  'Exit Sub
End If
Next Position

LastLine:
    x = x + 1

Loop

End Sub 
Private Sub FIND_LAST_NUMBER()
像线一样变暗
作为整数的Dim B
作为字符串的Dim C
将D变暗为字符串
x=3
在ActiveSheet.Cells(x,4.Value)中执行时
A=范围(“D”和x).值
A=修剪(A)
B=Len(A)
对于位置=B到1步骤-1
C=中间(A,位置,1)
'MsgBox C
如果是“C”,则
D=右侧(A、B位置)
范围(“E”&x)。值=C
转到最后一行
“出口接头
如果结束
下一个位置
最后一行:
x=x+1
环
端接头
但我想知道如何从原始单元格中获取所有的数字,而不是页码的最后一位

很明显,我没有得到什么东西


任何提示或窍门都将受到极大的赞赏。

一个,诚然,我能马上想到的不是很好的解决方案是使用
替换
删除所有非数字字符

Dim str As String

str = Replace(str, " ", "") '<- to remove the random spaces
str = LCase(str) '<- making everything lower case

For i = 97 To 122
    str = Replace(str, Chr(i), "")
Next i
Dim str作为字符串

str=Replace(str,“,”)'章节号之后和章节标题之前是否总是有空格?我仍然不太理解您的输入格式,数字之间是否有点?请尝试正则表达式模式
^(\d+)\(\d+)。(\d+).*(\d+)