【问题标题】:VBA loop with InStr for a .txt file带有 InStr 的 VBA 循环用于 .txt 文件
【发布时间】:2018-05-11 13:56:08
【问题描述】:

我在尝试从 .txt file 导入信息时遇到问题。我在文件中有这些数据:

EMPRESA : TRANSENER S.A.                
---- NODO --------------------         INYECCION
Descripción                     VNom       (MW)
ALICURA     500        2         500      251.8
---- LINEAS ------------------         PARTIC  CHPOTik  REMUNERACION
Descripción                     VNom      (%)    ($/H)  MENSUAL  ($)
ACABRAL-R.OESTE 500.0            500     0.15     5.24       3775.62
ALMAFUE.-ACABRAL 500.0           500     0.15     1.85       1335.12
------------------------------
---- NODO --------------------         INYECCION
Descripción                     VNom       (MW)
SANTO TOME  500  2               500      101.2
---- LINEAS ------------------         PARTIC  CHPOTik  REMUNERACION
Descripción                     VNom      (%)    ($/H)  MENSUAL  ($)
ALMAFUE.-EMBALSE-500-1           500     0.15     0.25        179.54
CHACO500-RESISTEN 500.0          500     0.10     0.17        124.02
------------------------------
---- NODO --------------------         INYECCION
Descripción                     VNom       (MW)
ALMAFUERTE    1.0     3            1       -9.5
---- LINEAS ------------------         PARTIC  CHPOTik  REMUNERACION
Descripción                     VNom      (%)    ($/H)  MENSUAL  ($)
ALMAFUE.-EMBALSE-500-1           500     0.15     0.25        179.54
CHACO500-RESISTEN 500.0          500     0.10     0.17        124.02

我想为任何 BUS (NODO) 提取信息:

Name                             Vnom    ( MW ) 
ALICURA     500        2         500      251.8 
SANTO TOME  500  2               500      101.2
ALMAFUERTE    1.0     3            1       -9.5

我编写了这段代码,但只适用于第一个:

Private Sub BarrasIny_Click()
    Dim myFile As String
    Dim text As String
    Dim quote As String
    Dim textline As String

    myFile = "C:\Users\tvelis\Desktop\CFCT_por_barra2.txt"

    Open myFile For Input As #1
        Do Until EOF(1)
            Line Input #1, textline
            text = text & textline
        Loop
    Close #1

    quote = InStr(text, "---- NODO")
    Range("A1").Value = "Barra"
    Range("B1").Value = "Tension"
    Range("c1").Value = "MW"

    Range("A2").Value = Mid(text, quote + 103, 30)
    Range("B2").Value = Mid(text, quote + 136, 3)
    Range("C2").Value = Mid(text, quote + 145, 5)
End Sub

我该如何做一个循环并从任何总线(NODO)中提取信息?

【问题讨论】:

    标签: excel import vba


    【解决方案1】:

    我尝试了以下方法,我相信它非常接近您想要实现的目标,使用计数器检查 NODO 并获取下面两行的值,然后获取文本行的值并将它们拆分放入一个数组,然后循环遍历该数组以将值放入工作表中:

    Private Sub BarrasIny_Click()
        Dim myFile As String
        Dim text As String
        Dim quote As String
        Dim textline As String
        Dim ws As Worksheet: Set ws = Sheets("Sheet1")
        'declare and set your worksheet, amend as required
        Dim arr(0) As Variant
        myFile = "C:\Users\tvelis\Desktop\CFCT_por_barra2.txt"
        counter = 0
    
        Open myFile For Input As #1
            Do Until EOF(1)
                Line Input #1, textline
                counter = counter + 1
                If InStr(textline, "--- NODO") > 0 Then counter = 0
    
                If counter = 2 Then
                    LastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row + 1
                    b = 2
                    arr(0) = Split(textline, " ")
                    For c = LBound(arr(0)) To UBound(arr(0))
                        If arr(0)(c) <> "" Then
                            If Not IsNumeric(arr(0)(c)) Then
                                ws.Cells(LastRow, 1).Value = Trim(ws.Cells(LastRow, 1).Value & " " & arr(0)(c))
                                y = LastRow
                            Else
                                ws.Cells(y, b).Value = arr(0)(c)
                                b = b + 1
                            End If
                            x = x + 1
                        End If
                    Next c
                End If
            Loop
        Close #1
    
        ws.Range("A1").Value = "Barra"
        ws.Range("B1").Value = "Tension"
        ws.Range("C1").Value = "MW"
    End Sub
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-12-18
      • 2023-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-20
      • 1970-01-01
      • 2016-07-20
      相关资源
      最近更新 更多