返回多行

时间:2019-07-19 14:26:56

标签: vba ms-access

我有大量文本,正尝试将其部分内容提取到表单中的字段中。我正在使用以下代码:

If InStr(1, abody(j), "Extent of Work:", 1) Then
    strExtWork = Mid(abody(j), InStr(abody(j), "Extent of Work:") + 16)
End If

这在返回一行时效果很好,但是有时我尝试提取的工作范围是多行,而且我有点碰壁...有什么想法吗?

以下是文本示例:

SEQUENCE NUMBER     CDC = RIC 

Transmit:  Date: 

*** R O U T I N E         *** Request No.: 000000000

Operators Notified: 


Start Date/Time:    01/01/18   At 00:15  Expiration Date: 01/01/18 

Location Information: 
County:     Municipality: 
Subdivision/Community:  
Street:               0 FAKE ST
Nearest Intersection: FAKE ST
Other Intersection:    
Lat/Lon: 
Type of Work: REPAIR  
Block:                Lot:                Depth: 2FT 

Extent of Work:  BEGINS 53FT W OF C/L OF INTERSECTION AND EXTENDS 785FT
  W.  MARK A 3FT RADIUS OF POLE NUMBERS 000/000, 000/000

Remarks:  
  Working For Contact:  NO ONE

Working For: NO ONE
Address:     123 FAKE ST
City:        SPRINGFIELD
Phone:       555-555-5555   Ext:  

Excavator Information: 
Caller:      NO ONE
Phone:       555-555-5555   Ext:  

Excavator:   NO ONE

Address:     123 FAKE ST
City:        SPRINGFIELD
Phone:       555-555-5555   Ext:          Fax:  
Cellular:     
Email:       EMAIL@EMAIL.COM

End Request 

我现在注意到,如果工作范围的范围只有一行,它将不会导入。它只有在有多行时才导入,有什么想法吗?这是我正在使用的代码:

Dim rowOffset As Long

If InStr(1, abody(j), "Extent of Work:", 1) Then
    strExtWork = Mid(abody(j), InStr(abody(j), "Extent of Work:") + 16)
    rowOffset = 1 ' reinit
    While Left(abody(j + rowOffset), 8) <> "Remarks:" ' check if this is Remarks:
        strExtWork = strExtWork & abody(j + rowOffset)
        rowOffset = rowOffset + 1
        Me.ExtentofWork = Trim(strExtWork)
    Wend
End If

2 个答案:

答案 0 :(得分:0)

听起来您需要找到一个与split命令一起使用的通用文本拆分器值,使用该值将文本解析为数组,然后循环数组以构建文本。

这里是一个例子:

VBA Split String Loop

答案 1 :(得分:0)

如果Extent of Work:之后始终有空行:

Dim rowOffset as long

If InStr(1, abody(j), "Extent of Work:", 1) Then
    strExtWork = Mid(abody(j), InStr(abody(j), "Extent of Work:") + 16)

    rowOffset = 1 ' reinit 
    While trim(abody(j + rowOffset)) <> ""  ' check if this is empty separator line
        strExtWork = strExtWork & abody(j + rowOffset)
        rowOffset = rowOffset + 1
    Wend

如果Remarks:之后仍然有Extent of Work,请将循环条件更改为:

While left(abody(j + rowOffset),8) <> "Remarks:"  ' check if this is Remarks: