如何使用VBA将特定数据从记事本导入Excel工作表
首先,我将展示我的记事本的外观。请看附件中的图片。然后我会解释我在找什么 从这里,我想将“刀具PA值”、刀具BR值和刀具半径值导入excel工作表的单独列中。我的excel工作表的列名为刀具PA、刀具半径、刀具BR。我想将记事本值输入到每个相应的列中。如何使用VBA将特定数据从记事本导入Excel工作表,excel,vba,Excel,Vba,首先,我将展示我的记事本的外观。请看附件中的图片。然后我会解释我在找什么 从这里,我想将“刀具PA值”、刀具BR值和刀具半径值导入excel工作表的单独列中。我的excel工作表的列名为刀具PA、刀具半径、刀具BR。我想将记事本值输入到每个相应的列中。 请指导我如何解决这个问题。这方面的问题应该可以解决。我还没有测试您的文本文件,但您应该可以从中找到答案 Sub ImportText() Dim LineString As String Dim sSourceFile As String D
请指导我如何解决这个问题。这方面的问题应该可以解决。我还没有测试您的文本文件,但您应该可以从中找到答案
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
你能再解释一下吗?谢谢你的回复,你能给我分享一下你的邮件id吗?这样我就可以和你分享我的文本文件了。虽然我会尝试你发送的代码,很抱歉回复太晚,但我在度假,嗨@Aeneas,我试图在这里上传文本文件,但它不起作用。我在运行代码时没有遇到什么错误。我很抱歉n行接一行,并在该if循环的第一行中出错。如果InStr(0,LineString,“(Cutter PA)”)>0,则CutterPA=Val(拆分(LineString,“=”)(1))结束如果我得到的错误是“无效的过程调用或参数”,我从InStr函数中删除了可选参数。请重试