【发布时间】:2016-01-11 21:51:01
【问题描述】:
我在 word 文档中有一个表格,我只需要从中提取数字。文档中有 2 个单元格,第一个单元格中包含以下字符串:
“24.00(小时)”
我只需要其中的数字“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
这是我所指的表格部分的示例:
【问题讨论】:
-
您可以使用
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 引用该单元格,但我建议使用变量。