다음 내용이 포함 된 텍스트 파일 :
userId=10101
givenname=Mark
orclmaidenname=Visser
[email protected]
userID=10102
givenname=Jan
orclmaidenname=Klaassen
userID=10104
givenname=Jessica
orclmaidenname=Host
[email protected]
등등.
이 txt 파일을 텍스트 파일의 행 머리글이 열 머리글이되고 Excel 시트의 행이 텍스트 파일의 해당 값으로 채워지는 Excel 워크 시트로 어떻게 변환합니까?
txt 파일에 1000 개가 넘는 항목이 있지만 모든 항목에 동일한 행 머리글이있는 것은 아닙니다 (예 : 일부 항목에 "메일"이 없음).
이것이 제가 지금까지 가지고있는 것입니다. "uid", "givenname", "orclmaidenname"을 올바르게 가져옵니다. 그러나 "메일"을 추가하면 예기치 않은 결과가 발생합니다. 모든 항목에 "mail"이라는 행 머리글이 포함되어 있지 않기 때문이라고 생각합니다.
Sub Frank()
Application.ScreenUpdating = False
' Name current file
MyMacroFile = ActiveWorkbook.Name
' Prompt for file
MyFile = Application.GetOpenFilename("All Files,*.*")
If MyFile = False Then
Exit Sub
End If
' Open file
Workbooks.OpenText Filename:=MyFile, Origin:=xlWindows, StartRow:=1, _
DataType:=xlFixedWidth, FieldInfo:=Array(0, 2)
' Name text file
MyTextFile = ActiveWorkbook.Name
' Find cell with "uid", "givenname", "orclmaidenname"
Do
Windows(MyTextFile).Activate
' Exit loop if can't find any matches
On Error GoTo Err_Fix
Cells.Find(What:="uid=", After:=ActiveCell, LookIn _
:=xlValues, LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:= _
xlNext, MatchCase:=False).Activate
Cells.Find(What:="givenname=", After:=ActiveCell, LookIn _
:=xlValues, LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:= _
xlNext, MatchCase:=False).Activate
Cells.Find(What:="orclmaidenname=", After:=ActiveCell, LookIn _
:=xlValues, LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:= _
xlNext, MatchCase:=False).Activate
On Error GoTo 0
' Exit loop if starting search over form top
If ActiveCell.Row <= MyRow Then
Exit Do
End If
' Get specific characters of selected rows
MyValue = Right(ActiveCell.Value, 6)
MyValue2 = Right(ActiveCell.Value, Len(ActiveCell.Value) - 10)
MyValue3 = Right(ActiveCell.Value, Len(ActiveCell.Value) - 15)
MyRow = ActiveCell.Row
' Paste value in spreadsheet with macro in columns A, B and C
Windows(MyMacroFile).Activate
Range("A65536").End(xlUp).Offset(1, 0) = MyValue
Range("B65536").End(xlUp).Offset(1, 0) = MyValue2
Range("C65536").End(xlUp).Offset(1, 0) = MyValue3
Loop
Err_Fix:
Windows(MyTextFile).Activate
ActiveWorkbook.Close
Application.ScreenUpdating = True
Exit Sub
End Sub
통찰력이 있습니까?
죄송합니다. 이것은 귀하의 코드와 일치하지 않지만 VBA를 사용하여 텍스트 파일을 읽지 않았으므로 여기서 스스로 가르치고있었습니다. 이 코드는 test.txt
제공된 텍스트에서 복사 한 파일을 읽습니다 . 처음 세 개의 필드가 채워져있는 한 오류가 발생해서는 안되지만 누락 된 mail
필드는 문제가되지 않습니다. 이 코드 =
는 첫 번째 행 뒤의 모든 행에 대해 다음 항목을 취하고 빈 행 뒤의 새 행으로 이동하는 방식으로 작동합니다.
Option Explicit
Sub test()
Dim fname As String
Dim txt As String
Dim rowID As Long
Dim colID As Long
Dim pos As Integer
fname = "H:\test.txt"
Open fname For Input As #1
rowID = 1
colID = 1
Do Until EOF(1)
Line Input #1, txt
If rowID = 1 And txt <> "" Then
pos = InStr(1, txt, "=")
Cells(rowID, colID).Value = Left(txt, pos - 1) 'Populate the field names
Cells(rowID + 1, colID).Value = Right(txt, Len(txt) - pos) 'Populate the second row (first row of data)
colID = colID + 1
ElseIf txt = "" Then
If rowID = 1 Then rowID = rowID + 1 'Increase the rowID by 1 since we have already populated the first row of data.
colID = 1 'Set the colID back to 1 for the new row
rowID = rowID + 1 'Move to a new row
Else
pos = InStr(1, txt, "=")
Cells(rowID, colID).Value = Right(txt, Len(txt) - pos) 'Print out everything after the "=" to the cell
colID = colID + 1 'Move to new column
End If
Loop
Close #1
End Sub
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다