[英]VBA loop with InStr for a .txt file
I have problems trying to import info from a .txt file
. 我在尝试从
.txt file
导入信息时遇到问题。 I have this data inside the 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
I want to extract for any BUS (NODO) the information: 我想提取任何总线(NODO)的信息:
Name Vnom ( MW )
ALICURA 500 2 500 251.8
SANTO TOME 500 2 500 101.2
ALMAFUERTE 1.0 3 1 -9.5
I make this code, but only works with the first one: 我编写此代码,但仅适用于第一个代码:
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
How can I do to make a Loop and extract the information from any bus(NODO)? 我该如何做一个循环并从任何总线(NODO)中提取信息?
I've tried the following and I believe it is very close to what you are trying to achieve, used a counter to check for the NODO and get the values two lines below, then get the values of the textline and split them into an array, then looped through the array to place the values into the Sheet: 我已经尝试了以下内容,但我相信它与您要实现的目标非常接近,使用计数器来检查NODO并获取下面两行的值,然后获取文本行的值并将它们拆分为一个数组,然后遍历数组以将值放入Sheet中:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.