我希望我的宏能够读取每个文本文件中的某些行(保存在服务器文件夹中),但到目前为止,我只能让宏从第一个文本文件中返回正确的值...
我认为这是因为我并不真正理解'打开xxx输入为#1'命令......这是宏:
Public CurrCell As Range
Public noLines As Integer
Sub NextCell()
Dim myFile As String
noLines = InputBox("Enter the number of TRs to add")
Range("A1").Activate
For Each CurrCell In Range(Cells(2, 1), Cells(noLines + 1, 1))
myFile = Application.GetOpenFilename()
ActiveCell.Offset(1, 0).Select
ActiveCell.FormulaR1C1 = myFile
Next CurrCell
End Sub
Sub GrabData()
Dim myFileName As String
Dim text As String
Dim textline As String
Dim Incidental As Integer
Dim TotalAccom As Integer
Dim Incidental_value As String
Dim TotalAccom_value As String
Dim i As Integer
For i = 1 To noLines
myFileName = Cells(i + 1, 1)
Open myFileName For Input As #1
Do Until EOF(1)
Line Input #1, textline
text = text & textline
Loop
Close #1
Incidental = InStr(text, "INCIDENTAL ALLOWANCE")
Cells(i + 1, 2).Value = Mid(text, Incidental + 22, 5)
Next i
End Sub
第一个子项是要求用户输入并选择他们想要读取的文本文件数,第二个子项假设为每个文本文件带回正确的值。
提前致谢!!!
答案 0 :(得分:0)
你可能想试试这个:
Public noLines As Long ' Use Public if this is to be accessed by another Module
Sub NextCell()
Dim i As Long, oRng As Range
noLines = CLng(InputBox("Enter the number of TRs to add"))
' First store all the filenames, store them below A1
Set oRng = Range("A1")
For i = 1 To noLines
oRng.Offset(i, 0).Value = Application.GetOpenFilename()
Next
Set oRng = Nothing
' Then invoke the sub "GrabData"
GrabData
End Sub
Sub GrabData()
Const sMarker = "INCIDENTAL ALLOWANCE"
Dim i As Long, oRng As Range
'Dim myFileName As String
Dim text As String
Dim textline As String
Dim Incidental As Long
'Dim TotalAccom As Integer
'Dim Incidental_value As String
'Dim TotalAccom_value As String
Set oRng = Range("A1")
For i = 1 To noLines
text = "" ' Reset the text to blank!
' Now go through the list of filenames stored below A1
Open oRng.Offset(i, 0).Value For Input As #1
Do Until EOF(1)
Line Input #1, textline
text = text & textline
Loop
Close #1
' Get location of text after sMarker
Incidental = InStr(text, sMarker) + Len(sMarker) + 2
' Store 5 characters of text after sMarker to column C
oRng.Offset(i, 2).Value = Mid(text, Incidental, 5)
Next i
Set oRng = Nothing
End Sub