繁体   English   中英

如何使用 VBA 将记事本中的特定数据导入 Excel 工作表

[英]How to import specific data from a Notepad to Excel Worksheet using VBA

首先,我将展示我的记事本的外观。请参阅附图。 然后我会解释我在找什么。

在此处输入图片说明

从此我想将“Cutter PA Value”、Cutter BR Value 和 Cutter Radius Value 导入到 excel 工作表的单独列中。我的 Excel 工作表的列名称为 Cutter PA、Cutter Radius、Cutter BR。我希望记事本值为在每个对应的列中输入。请指导我如何解决这个问题。

沿着这些路线的东西应该工作。 我没有你的文本文件来测试,但你应该能够从中解决它......

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM