[英]how to read data from an excel worksheet dynamically using VBA 2003
[英]How to import specific data from a Notepad to Excel Worksheet using VBA
沿着这些路线的东西应该工作。 我没有你的文本文件来测试,但你应该能够从中解决它......
Sub ImportText()
Dim LineString As String
Dim sSourceFile As String
Dim r As Long
Dim fLen As Long
Dim fn As Integer
Dim CutterPA, CutterBR, CutterRad As Double
'Text file and path
sSourceFile = "C:\filtest.txt"
'sSourceFile doesn't exist
If Len(Dir(sSourceFile)) = 0 Then Exit Sub
'Gets a free file number from the operating system
fn = FreeFile
'Opens the file for input
Open sSourceFile For Input As #fn
fLen = LOF(fn)
r = 2
While Not EOF(fn)
Line Input #fn, LineString
If InStr(LineString, "(Cutter PA)") > 0 Then
CutterPA = Val(Split(LineString, "=")(1))
End If
If InStr(LineString, "(Cutter BR)") > 0 Then
CutterBR = Val(Split(LineString, "=")(1))
End If
If InStr(LineString, "(Cutter Radius)") > 0 Then
CutterRad = Val(Split(LineString, "=")(1))
End If
'Write values if new item
If InStr(LineString, "(***********") > 0 Then
With ActiveSheet
.Cells(r, 1).Value = CutterPA
.Cells(r, 2).Value = CutterBR
.Cells(r, 3).Value = CutterRad
End With
r = r + 1
End If
Wend
'Closes the text file
Close #fn
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.