如何在Excel VBA中获得具有不同长度的特定行

你好,我有像下面的文本文件,我保存它myfile.txt;

!File starts Some texts here version.number = 12345 Some texts here !File ends 

我使用下面的代码来获得版本号12345,但有时版本号可能是5或更多的数字。 作为一个例子,我怎样才能得到6位123456与我的代码。

 Dim myFile As String Dim text As String Dim textline As String Dim VersionInfo As Integer Sub DetectModelVersion() myFile = "C:\test\myfile.txt" Open myFile For Input As #1 Do Until EOF(1) Line Input #1, textline text = text & textline Loop Close #1 VersionInfo = InStr(text, "version.number") ThisWorkbook.Worksheets("Sheet1").Range("A1").Value = Mid(text, VersionInfo + 17, 6) End Sub 

只是testing了下面的代码,它的工作。

 Option Explicit Dim myFile As String Dim text As String Dim textline As String Sub DetectModelVersion() myFile = "H:\myfile.txt" Open myFile For Input As #1 Do Until EOF(1) Line Input #1, textline If InStr(textline, "version.number") Then text = Trim(Mid(textline, InStr(textline, "=") + 1, 255)) Exit Do End If Loop Close #1 ThisWorkbook.Worksheets("Sheet1").Range("A1").Value = text End Sub