My Problem is about VBA MACRO....
i write some code for importing Multiple text files in to EXCEL worksheet....
but this works only for one column text files(With same structure)....
but if i have many text files with diffferent structure like in first text file there r 5 tab delimited Columns and in 2nd Text file 3 tab delimited columns...
Similarly there 20 to 30 text files in One folder.....
there is code that i m using plz give me actual code that work for multiple column text files...
Private Sub CommandButton1_Click()
Sheet1.Activate
Dim Result As VbMsgBoxResult
Result = MsgBox("Do You Really Want to Import?", vbOKCancel, "HyperSpectral Tool")
If Result = vbOK Then
On Error GoTo cmdcancel_Click_Exit
Sheet1.Columns.Delete
Cells(1, "A").Select
Dim myDir As String, fn As String, ff As Integer, txt As String
Dim delim As String, n As Long, b(), flg As Boolean, x, t As Integer, i As Long
With Application.FileDialog(msoFileDialogFolderPicker)
'setup File Dialog
.AllowMultiSelect = False
.ButtonName = "Select"
.InitialView = msoFileDialogViewList
.Title = "Select Input Folder"
'display file dialog box
If .Show Then
'get selected files
myDir = Application.FileDialog(msoFileDialogFolderPicker).InitialFileName
delim = vbTab '<- delimiter (assuming Tab delimited)
fn = Dir(myDir & "\*.txt")
Else
End If
End With
myDir = Application.FileDialog(msoFileDialogFolderPicker).InitialFileName
delim = vbTab '<- delimiter (assuming Tab delimited)
fn = Dir(myDir & "\*.txt")
Do While fn <> ""
'i = Columns.Count
ReDim b(1 To Rows.Count, 1 To 1)
ff = FreeFile
Open myDir & "\" & fn For Input As #ff
Do While Not EOF(ff)
Line Input #ff, txt
x = Split(txt, delim)
'If Not flg Then
'n = n + 1: b(n, 1) = fn
'End If
If UBound(x) > 0 Then
n = n + 1
b(n, 1) = x(1)
End If
flg = True
Loop
Close #ff
flg = False
t = t + 1
ThisWorkbook.Sheets(1).Cells(1, t).Resize(n).Value = b
n = 0
fn = Dir()
Loop
Range("A1").EntireRow.Font.Bold = True
Else
End If
cmdcancel_Click_Exit:
End Sub
thanks in advance....
Regards...
Renu Kataria
|