仅从Word表格单元格中提取数字到Excel单元格中

我在Word文档中有一个表格,我只需要从中提取数字。 文档中有两个单元格,第一个单元格中有以下string:

“24.00(小时)”

我只需要那个数字“24”。 由于持续时间不一定总是2位数。 它可能超过100.它通常以“xxx.xxx”格式。

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

“每小时$ 125.00到$ 140.00”

我需要提取“125”,并将其放置在Excel中的单元格,然后提取“140”,并将其放置在另一个单元格中。 这些数字总是在由“to”分隔的“$”和“.00”之间。

持续时间需要进入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 

这里是我所指的表格部分的一个例子:

表零件

试试这个UDF并进行修改以适应您的需求。 如果在一行文本中没有第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 

UPDATE
对于你感兴趣的东西,首先在我的代码中粘贴一个新的Module或者你现有的代码的底部,然后在With .tables(TableNo)块中改变几行到下面:

 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