가정 :
Sub tgr()
Dim oShell As Object
Dim oFSO As Object
Dim arrData(1 To 65000) As String
Dim strFolderPath As String
Dim strFileName As String
Dim strText As String
Dim DataIndex As Long
Dim lAvgLoc As Long
Set oShell = CreateObject("Shell.Application")
On Error Resume Next
strFolderPath = oShell.BrowseForFolder(0, "Select a Folder", 0).Self.Path & Application.PathSeparator
Set oShell = Nothing
On Error GoTo 0
If Len(strFolderPath) = 0 Then Exit Sub 'Pressed cancel
Set oFSO = CreateObject("Scripting.FileSystemObject")
strFileName = Dir(strFolderPath & "*.txt*")
Do While Len(strFileName) > 0
strText = oFSO.OpenTextFile(strFolderPath & strFileName).ReadAll
lAvgLoc = InStr(1, strText, "Daily Avg", vbTextCompare)
If lAvgLoc > 0 Then
strText = Mid(strText, lAvgLoc)
strText = Trim(Mid(Replace(strText, vbCrLf, String(255, " ")), Evaluate("MIN(FIND({1,2,3,4,5,6,7,8,9,0},""" & strText & """&1234567890))"), 240))
DataIndex = DataIndex + 1
arrData(DataIndex) = DateValue(Replace(strFileName, ".txt", vbNullString)) & vbTab & strText
End If
strFileName = Dir
Loop
If DataIndex > 0 Then
With Sheets.Add
.Range("A1:F1").Value = Array("DATE", "AVG1", "AVG2", "AVG3", "AVG4", "AVG5")
With .Range("A2").Resize(DataIndex)
.Value = Application.Transpose(arrData)
.TextToColumns .Cells, xlDelimited, xlTextQualifierDoubleQuote, Tab:=True
.NumberFormat = "mm-dd-yy"
End With
Application.DisplayAlerts = False
.SaveAs strFolderPath & "Daily Averages.csv", xlCSV
Application.DisplayAlerts = True
End With
End If
Set oFSO = Nothing
Erase arrData
End Sub
방법 : 모든 텍스트 파일이 같은 폴더에
텍스트 파일에 있습니다
사용이 엑셀 VBA 코드를 구분하다 매크로 사용 :
- , 매크로는 항상 통합 문서의 사본에 새로운 코드를 실행
- 에서 실행됩니다 통합 문서의 복사본을 만듭니다 단지의 경우 코드가이 코드 특히 사실이다
- 원활하게 실행되지 않습니다 | 복사 된 통합 문서에서 아무것도
- , Alt + F11 키를 눌러 Visual Basic 편집기
- 삽입을 엽니 다 삭제 모듈
- 는
- 원하는 매크로를 두 번 클릭하여 실행 가능한 매크로의 목록을 가져 Visual Basic 편집기
- 엑셀, 보도 ALT + F8을 닫습니다 모듈에 제공된 코드를 복사하여 붙여 넣기 (I라는 이 하나의 tgr)
날짜는 어디에서 유래됩니까? 파일 이름, '마지막으로 수정 한'스탬프? – JScoobyCed
날짜는 파일 이름에서옵니다. 예 : '12 -13-06.txt ' – ptrckqnln
귀하의 질문을 해결할 수 있기 때문에 답변으로 게시하지 않을 다양한 방법입니다. 옵션은 다음과 같습니다 : awk의 상사 인 경우 셸 스크립트 + awk :) 또는 [id | date | avg1 | ... | avg5] 데이터베이스의 파일을로드 (쉘 스크립트 루프를 통해) 한 다음 편안하다고 느끼는 프로그래밍 언어 필요에 따라 형식을 지정합니다. 아마 다른 옵션들 ... – JScoobyCed