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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/25.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
Vba 仅将Word表格单元格中的数字提取到Excel单元格中_Vba_Excel - Fatal编程技术网

Vba 仅将Word表格单元格中的数字提取到Excel单元格中

Vba 仅将Word表格单元格中的数字提取到Excel单元格中,vba,excel,Vba,Excel,我在word文档中有一个表,我只需要从中提取数字。文档中有2个单元格,第一个单元格中有以下字符串: “24小时” 我只需要那个号码的“24”。它不会总是2位数,因为它的持续时间为小时。可能超过100。但通常是“xxx.xxx”格式 我需要从中提取的第二个单元格有点困难。看起来是这样的: “$125.00至$140.00每小时” 我需要提取“125”并将其放在excel中的一个单元格中,然后提取“140”并将其放在另一个单元格中。这些数字将始终介于“$”和“.00”之间,并用“to”一词分隔 持续

我在word文档中有一个表,我只需要从中提取数字。文档中有2个单元格,第一个单元格中有以下字符串:

“24小时”

我只需要那个号码的“24”。它不会总是2位数,因为它的持续时间为小时。可能超过100。但通常是“xxx.xxx”格式

我需要从中提取的第二个单元格有点困难。看起来是这样的:

“$125.00至$140.00每小时”

我需要提取“125”并将其放在excel中的一个单元格中,然后提取“140”并将其放在另一个单元格中。这些数字将始终介于“$”和“.00”之间,并用“to”一词分隔

持续时间需要进入第J列,费率需要分为K和L列

这是我目前的代码:

Sub ImportWordTable()
Dim wdDoc As Object
Dim wdFileName As Variant
Dim TableNo As Integer 'table number in Word
Dim iTable As Integer  'table number index
Dim iRow As Long     'row index in Excel
Dim iCol As Integer  'column index in Excel

wdFileName = Application.GetOpenFilename("Word files (*.docx),*.docx", , _
"Browse for file containing table to be imported")

If wdFileName = False Then Exit Sub '(user cancelled import file browser)

Set wdDoc = GetObject(wdFileName) 'open Word file

Worksheets("Request Detail").Activate 'activates sheet of specific name


With wdDoc
TableNo = wdDoc.tables.Count
If TableNo = 0 Then
MsgBox "This document contains no tables", _
vbExclamation, "Import Word Table"
ElseIf TableNo > 1 Then
TableNo = InputBox("This Word document contains " & TableNo & " tables." & vbCrLf & _
"Enter table number of table to import", "Import Word Table", "1")
End If

For iTable = 1 To TableNo

       Dim lRow As Long
       lRow = Range("A" & Rows.Count).End(xlUp).Offset(1).Row + 1

        With .tables(TableNo)


            Cells(lRow - 1, "A") = WorksheetFunction.Clean(.cell(14, 2).Range.Text) 'polaris id
            Cells(lRow - 1, "B").Value = Date                                       'post current date
            Cells(lRow - 1, "C") = WorksheetFunction.Clean(.cell(16, 2).Range.Text) 'resource manager name
            Cells(lRow - 1, "D") = WorksheetFunction.Clean(.cell(15, 2).Range.Text) 'requestor name
            Cells(lRow - 1, "E") = WorksheetFunction.Clean(.cell(1, 2).Range.Text)  'customer name
            Cells(lRow - 1, "H") = WorksheetFunction.Clean(.cell(7, 2).Range.Text)  'start date
            Cells(lRow - 1, "I") = WorksheetFunction.Clean(.cell(8, 2).Range.Text)  'end date
            Cells(lRow - 1, "J") = WorksheetFunction.Clean(.cell(9, 2).Range.Text)  'duration
            Cells(lRow - 1, "K") = WorksheetFunction.Clean(.cell(12, 2).Range.Text)  'request low rate
            Cells(lRow - 1, "L") = WorksheetFunction.Clean(.cell(12, 2).Range.Text)  'request high rate


            'Cells(lRow - 1, "S") = WorksheetFunction.Clean(.cell(3, 2).Range.Text)  need to post name of negotiatoe

        End With

Next iTable
End With

Set wdDoc = Nothing

End Sub
下面是我所指的表格部分的一个示例:


尝试此自定义项,并根据需要进行修改。如果文本行中的第N个数字不匹配,则返回负数(-1

假设Word单元格中的文本已放入Excel范围(例如C3),存储在D列中的小时数,存储在E列中的速率最小值,存储在F列中的速率最大值,然后输入公式:
D3
=GetNthNumber(C3)

E3
=GetNthNumber(C3,1)

F3
=GetNthNumber(C3,2)

如果文本行中包含时间“天”,则可以执行更多操作

Option Explicit

Function GetNthNumber(oItem As Variant, Optional Nth As Long) As Double
    Dim sText As String, n As Long, i As Long, oTmp As Variant
    n = Nth
    ' Set to First if argument "Nth" is not passed in
    If n <= 0 Then n = 1
    ' Retrieve the text from the input item
    Select Case TypeName(oItem)
        Case "Range":   sText = oItem.Text
        Case "String":  sText = oItem
        Case Else:      sText = CStr(oItem)
    End Select
    i = 0 ' Initialize counter
    ' Loop through all the words in the text
    For Each oTmp In Split(sText, " ")
        ' Process only if the word is a number
        If IsNumeric(oTmp) Then
            i = i + 1
            ' Check if it's the Nth number
            If i = n Then
                sText = oTmp
                Exit For
            End If
        End If
    Next
    ' Return -1 if there isn't an answer
    If Not IsNumeric(sText) Then sText = "-1"
    GetNthNumber = CDbl(sText)
End Function

您可以使用
Mid()
公式。假设您的A1单元“每小时125.00美元到140.00美元”。要提取首个数字
$125.00
,可以使用:
=MID(A1,SEARCH($),A1,1),SEARCH(“To”,A1)-2)
。要获取“$140.00”,
=MID(A1,SEARCH($”,A1,2),SEARCH(“To”,A1)-2)
。这有帮助吗?@BruceWayne这会进入VBA脚本还是Excel单元格本身?很抱歉,耽搁了-你可以使用这两个地方。在VBA中,在计算变量后,将其设置为该变量。或者你可以直接放在工作表中,用VBA引用该单元格,但我建议使用一个变量。老实说,我不知道如何实现它。@Advancin请参阅更新,假设我从你的代码注释中得到了正确的列。我用一些有趣的值对此进行了测试<代码>124-135它完美地把数字拉了出来……这真是太好了。再次感谢!只要数字有一个与其他文本分开的空格,它就可以工作。请注意,如果它是
124-135
(破折号后没有空格),第二个数字将是负数:
-135
您可以使用
Abs(…)
强制它为正数,即
GetNthNumber=Abs(CDbl(sText))
Cells(lRow - 1, "J").Value = GetNthNumber(WorksheetFunction.Clean(.cell(9, 2).Range.Text))  'duration (Time to Book?)
Cells(lRow - 1, "K").Value = GetNthNumber(WorksheetFunction.Clean(.cell(12, 2).Range.Text), 1) 'request low rate
Cells(lRow - 1, "L").Value = GetNthNumber(WorksheetFunction.Clean(.cell(12, 2).Range.Text), 2) 'request high rate