背景提取字符串
我要在Excel中創建VBA代碼打開一個文本文件,並把它的一些字符串開頭的特定部分「:60F」成特定的細胞Excel工作表。
這部分代碼已經工作。
問題
然而,I'm與任務的第二部分中掙扎。我必須在Excel文檔的另一個單元格中寫入以「:61:」開頭的特定字符串的一部分。
問題是,在以「61:」開頭的文本文件中有很多字符串,但我需要在以「60F:」開頭的字符串之後出現的字符串。
如果你能幫助我,我將非常感激。非常感謝您提前!
親切的問候
這裏是我寫到目前爲止代碼:
代碼
Function extract_opening_balance(ByVal filename As String, subfield_61 As String) As String
Dim i As Integer
Dim pos1 As Integer
strPath = ActiveWorkbook.Path & "\Data of Reporting Month\"
filename = strPath & "MT940_T2_" & main_menu.cbo_Year & Left(main_menu.cbo_Month, 2) & Format(day(CDate(Right(main_menu.lst_Date.List(i), 10))), "00") & ".txt"
Open filename For Input As #1
For i = 3 To 13
Do Until EOF(1)
Line Input #1, textline
If InStr(textline, ":60F:") > 0 Then
If InStr(textline, "EUR") = 13 And InStr(textline, 0) <> 16 Then
'For j = 1 To String(":60F:").Count
pos1 = InStr(subfield_61, "//") + 30
'time_str = Mid(subfield_61, pos1, 6)
'time_str = Mid(time_str, 1, 2) & ":" & Mid(time_str, 3, 2) & ":" & Mid(time_str, 5, 2)
Sheets("op_balance").Range("B" & i).Value = Mid(textline, 6, 1)
Sheets("op_balance").Range("C" & i).Value = Mid(textline, 16, 20)
Sheets("op_balance").Range("D" & i).Value = subfield_61
Sheets("op_balance").Range("E" & i).FormulaR1C1 = "=IF(RC[-3]=""D"",(-1)*RC[-2],RC[-2])"
'Next j
End If
End If
Loop
Next i
End Function
文本文件有多大?你能提供一個簡短的例子嗎? – Comintern
[Seek](https://msdn.microsoft.com/en-us/library/7af2feyt(v = vs.90).aspx)並且您應[Find](https://msdn.microsoft.com/zh-cn/ -us/library/office/ff839746.aspx) – abraxascarab
[InStr函數](https://msdn.microsoft.com/zh-cn/library/8460tsh1.aspx)允許您開始搜索的位置。記錄':60F'的位置(也許長),並將其用作開始尋找':61:'的點。 – Jeeped