나는 접근 할 때 vba로 작업하는 것이 처음이므로이 코드를 작동시키는 데 문제가 있습니다. 그것이하는 일은 선택된 텍스트 파일을 가져와 원본 파일을 목록 상자로 읽는 것입니다. 그런 다음 두 번째 버튼을 누르면 텍스트 파일을 파이프로 구분 된 파일에서 탭으로 구분 된 파일로 변환 한 다음 변경된 파일을 새 목록 상자에 표시합니다. 파이프로 구분 된 파일을 탭으로 구분 된 파일로 변환하고 목록 상자에 결과를 표시하는 방법 VBA
Option Compare Database
Option Explicit
Function GetFilenameFromPath(ByVal strPath As String) As String
' Returns the rightmost characters of a string upto but not including the rightmost '\'
' e.g. 'c:\winnt\win.ini' returns 'win.ini'
If Right$(strPath, 1) <> "\" And Len(strPath) > 0 Then
GetFilenameFromPath = GetFilenameFromPath(Left$(strPath, Len(strPath) - 1)) + Right$(strPath, 1)
End If
End Function
Private Sub Command0_Click()
Dim fdlg As Office.FileDialog
Dim pipe_file As Variant
Dim FileName As String
Dim fn As Integer
Dim varFile As Variant
Dim FilePath As String
Me.OrigFile.RowSource = ""
Me.ConvertFile.RowSource = ""
Me.FileName = ""
Me.FilePath = ""
FileName = ""
Set fdlg = Application.FileDialog(msoFileDialogFilePicker)
With fdlg
.AllowMultiSelect = False
.Title = "Select pipe delimited file"
.Filters.Clear
.Filters.Add "Text Files", "*.txt"
If .Show = True Then
For Each varFile In .SelectedItems
FileName = GetFilenameFromPath(varFile)
FilePath = varFile
Next varFile
Me.FileName = FileName
Me.FilePath = FilePath
fn = FreeFile
Open FileName For Input As #fn
Do While Not EOF(fn)
Line Input #fn, pipe_file
Me.OrigFile.AddItem pipe_file
Loop
Close #fn
Else
MsgBox "You clicked Cancel in the file dialog box."
End If
End With
End Sub
Private Sub Convert_File_Click()
'ByVal OutputFile As String)'
On Error GoTo error1
Dim pipe_file As Variant
Dim ThisString As String
Dim NewString As String
Dim A As Integer
Dim InputFile As String
InputFile = Me.FilePath
Open InputFile For Input As #1
Const FileName = "c:\outputfile.txt"
Dim my_filenumber As Integer
my_filenumber = FreeFile
Open FileName For Output As #2
'Open OutputFile For Output As #2'
While Not EOF(1)
NewString = ""
Line Input #1, ThisString
For A = 1 To Len(ThisString)
If Mid(ThisString, A, 1) = "|" Then
NewString = NewString & Chr$(9)
Else
NewString = NewString & Mid(ThisString, A, 1)
End If
Next
Print #2, ThisString
Wend
Do While Not EOF(2)
Line Input #2, pipe_file
Me.ConvertFile.AddItem pipe_file
Loop
Close #2
Close #1
Exit Sub
error1:
Close #1
Close #2
End Sub
이
내가 지금까지 지금 내 문제는 두 번째 버튼 또는 Convert_File_Click() convertfile 내가 업데이트하려고 및 파일 경로있어 목록 상자입니다에 TEXTFILE의 파일 경로를 개최 텍스트 상자입니다 관련되는 것을입니다 가 선택됩니다. 도움을 주시면 감사하겠습니다.