Excel 从文本文件中读取特定文本值,并将其输入电子表格中的相关单元格?

Excel 从文本文件中读取特定文本值,并将其输入电子表格中的相关单元格?,excel,vba,Excel,Vba,我正在使用以下VBA代码检查表1中活动行的F列中的值是否存在于表2的b列中。然后,在找到值的地方,我让我的代码读取一个文本文件,通过使用中的活动行值对该目录进行整理 Range("G" & ActiveCell.Row).Value, Range("E" & ActiveCell.Row).Value and Range("F" & ActiveCell.Row).Value 然后,我将代码指向我的文本文件中的特定文本行“hours:”。然后在找到原始值的位置向下一行和

我正在使用以下VBA代码检查表1中活动行的F列中的值是否存在于表2的b列中。然后,在找到值的地方,我让我的代码读取一个文本文件,通过使用中的活动行值对该目录进行整理

Range("G" & ActiveCell.Row).Value, Range("E" & ActiveCell.Row).Value and Range("F" & ActiveCell.Row).Value
然后,我将代码指向我的文本文件中的特定文本行“hours:”。然后在找到原始值的位置向下一行和向右一列插入文本文件值

像这样:

第2页:

Column B                   Column C
Value Found       
Supplier Registration      50
External Input          
我的文本文件如下所示:

----------------DELGEATE TIME & RESOURCE STATS-----------------
Component: Supplier Registration
Completed By: Mark O'Brien
Hours: 50 hours
-------------------------END OF STATS--------------------------
----------------DELGEATE TIME & RESOURCE STATS-----------------
Component: Exernal Input
Completed By: Mark O'Brien
Hours: 10 hours
-------------------------END OF STATS--------------------------
因此,虽然我的代码在某种程度上起作用,从“小时”行中获取值,并将其放在供应商注册的单元格中,但我必须找到一种方法,允许我也从其他组件“外部输入”中获取值,并将小时数放在“外部输入”旁边的单元格中

这是我的密码:

'Sync Time / Resour Alloc
If Not Intersect(Target, Range("AE" & ActiveCell.Row)) Is Nothing And Range("AE" & ActiveCell.Row).Value = "Sync" Then
  m1 = Month(Range("C" & ActiveCell.Row).Value)
M = MonthName(m1, True)
Y = Year(Range("C" & ActiveCell.Row).Value)
Dim myFile As String, text As String, textline As String, posLat As Integer, posLong As Integer
myFile = "\\MI-FILESERVE1\Shared Folders\Shared_Business_Dev\Tenders\" & Range("G" & ActiveCell.Row).Value & "\" & Range("E" & ActiveCell.Row).Value & "\" & Range("F" & ActiveCell.Row).Value & " - " & M & " - " & Y & "\log.txt"
Open myFile For Input As #1
Do Until EOF(1)
    Line Input #1, textline
     text = text & textline
Loop
Close #1
posLat = InStr(text, "Hours")
hours = Mid(text, posLat + 7, 2)




    Dim rw As Long, cell As Range
rw = ActiveCell.Row
    With Worksheets("Sheet2").Columns("B:B")
        Set cell = .Find(What:=Sheets("Sheet1").Range("F" & rw).Value, LookIn:=xlFormulas, _
                        LookAt:=xlWhole, MatchCase:=False, SearchFormat:=False)
        If Not cell Is Nothing Then

            If cell.Offset(1, 0).Value = "Supplier Registration" Then
            cell.Offset(1, 1).Value = hours  '<~~the default property is the .Value
            Else
            If cell.Offset(2, 0).Value = "Exernal Input" Then
            cell.Offset(2, 1).Value = hours  '<~~the default property is the .Value
            End If
            End If


        Else
            MsgBox "Oooops there was an Error linking Tender reference to Time Allocation Chart."
        End If
    End With
    End If

有人能告诉我怎么做吗?

if语句中有一个else条件。因此,您的代码只能执行其中一项,而不能同时执行两项。如果您试图完成将工时值放在供应商注册和外部输入旁边,请更改以下内容: 如果cell.Offset1,0.Value=供应商注册,则 cell.Offset1,1.值=小时'
If cell.Offset(2, 0).Value = "Exernal Input" Then
    cell.Offset(2, 1).Value = hours  '<~~the default property is the .Value
End If