Read specific text value from text file and input it on relevant cell in spreadsheet?

时间:2015-09-14 15:54:01

标签: excel vba

I am using the following VBA code to check if a value from column F on the active row on sheet1 exists anywhere in column b on sheet2. Then where the value is found I am getting my code to read a text file, the directory to which is collated by using the active row values in

Range("G" & ActiveCell.Row).Value, Range("E" & ActiveCell.Row).Value and Range("F" & ActiveCell.Row).Value

I am then pointing the code to my specific text line 'hours:' in my text file. And then inserting the text file value one row down and one column to the right of where the original value was found.

Like So:

Sheet 2:

Column B                   Column C
Value Found       
Supplier Registration      50
External Input          

And my text file looks like this:

----------------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--------------------------

So whilst my code works to some degree and gets the value from the line 'hours' and puts it in the cell across from Supplier registration, I have to find a way that allows me to also get the value from the other component 'external input' and put the number of hours in the cell next to 'external input'.

Here is my code:

'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

Please can someone show me how to do this?

1 个答案:

答案 0 :(得分:0)

if语句中有else条件。因此,您的代码只会执行其中一个,而不是两个。如果您尝试将“小时”值放在“供应商注册”和“外部输入”旁边,则更改以下内容: 如果cell.Offset(1,0).Value =“供应商注册”则     cell.Offset(1,1).Value = hours'&lt; ~~默认属性是.Value 其他     如果cell.Offset(2,0).Value =“Exernal Input”那么         cell.Offset(2,1).Value = hours'&lt; ~~默认属性是.Value     万一 结束如果

对此: 如果cell.Offset(1,0).Value =“供应商注册”则     cell.Offset(1,1).Value = hours'&lt; ~~默认属性是.Value

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