D
DK
Hello!
I am extracting data from a text file which has been created by running
directory tree command.
The problem is that :
1. Macro is debugging if the directory path has a hyphen like in abc -
xyz. I understand that excel is taking it up as a negative/subtract
sign. Can someone help me in fixing this?
2. When I import text file, if directory path has spaces in it, those
get stripped out. For example ICE COLD goes into two columns and later
when I concatenate them, the space is purged. I need to avoid that. The
directory path has to be the same as it is in the text file. Can
someone please point out what am I doing wrong?
Thanks!
Here is the format of the text file:
Directory of P:\Ice Cold
some files in this directory
Directory of P:\Ice Cold\00108 koa spirit\letter
file listing for the above directory
Directory of P:\Ice Cold\123456 abc - xyz\correspondence
file listing for the above directory
This is the macro I use to import text file and then strip data:
Public Sub DoTheImport()
Dim FName As Variant
FName = Application.GetOpenFilename _
(filefilter:="Text Files(*.txt),*.txt,All Files (*.*),*.*")
If FName = False Then
MsgBox "You didn't select a file"
Exit Sub
End If
ImportTextFile CStr(FName)
Workbooks.OpenText Filename:= _
FName, Origin:=xlWindows, StartRow:=1, DataType:=xlFixedWidth,
FieldInfo:= _
Array(Array(0, 1), Array(10, 1), Array(21, 1), Array(29, 1),
Array(39, 1))
End Sub
Public Sub ImportTextFile(FName As String)
Dim RowNdx As Integer
Dim ColNdx As Integer
Dim WholeLine As String
Dim Pos As Integer
Dim NextPos As Integer
Dim SaveColNdx As Integer
Application.ScreenUpdating = False
'On Error GoTo EndMacro:
SaveColNdx = ActiveCell.Column
RowNdx = ActiveCell.row
Open FName For Input Access Read As #1
EndMacro:
On Error GoTo 0
Application.ScreenUpdating = True
Close #1
End Sub
*******************************************************************************************************
'Autofilter column A with Directory and concatenate the directory path
which is split in multiple columns
Range("A5").Select
Selection.AutoFilter Field:=1, Criteria1:="=Directory"
Selection.CurrentRegion.Select
Selection.Offset(2, 0).Resize(Selection.Rows.Count - 1).Select
Selection.SpecialCells(xlVisible).Select
Range("F3").Select
ActiveCell.FormulaR1C1 = "=CONCATENATE(RC[-5]:RC[-1])"
Range("F4").Select
Columns("F:F").ColumnWidth = 14.71
Range("F3").Select
ActiveCell.FormulaR1C1 =
"=CONCATENATE(RC[-5],RC[-4],RC[-3],RC[-2],RC[-1])"
Application.CutCopyMode = False
Selection.Copy
Range("F4").Select
Range(Selection, Selection.End(xlDown)).Select
ActiveSheet.Paste
'After this I apply a paste special to get rid of the formula.
'the last step is to copy the respective directory in the rows in the
next column to the file name.
'Puts the directory path with file names in Coloumn G
Dim itotalrows, i, j
Selection.SpecialCells(xlCellTypeLastCell).Select
itotalrows = Mid(ActiveCell.Cells.AddressLocal,
InStrRev(ActiveCell.Cells.AddressLocal, "$") + 1)
'MsgBox itotalrows
For i = 1 To itotalrows
If (Range("f" & i) <> "") Then
j = i
i = i + 1
Do While (Range("E" & i) <> "")
Range("g" & i).Value = Range("f" & j).Value
'& "\" & Range("e" & i).Value
i = i + 1
Loop
i = i - 1
End If
Next i
I am extracting data from a text file which has been created by running
directory tree command.
The problem is that :
1. Macro is debugging if the directory path has a hyphen like in abc -
xyz. I understand that excel is taking it up as a negative/subtract
sign. Can someone help me in fixing this?
2. When I import text file, if directory path has spaces in it, those
get stripped out. For example ICE COLD goes into two columns and later
when I concatenate them, the space is purged. I need to avoid that. The
directory path has to be the same as it is in the text file. Can
someone please point out what am I doing wrong?
Thanks!
Here is the format of the text file:
Directory of P:\Ice Cold
some files in this directory
Directory of P:\Ice Cold\00108 koa spirit\letter
file listing for the above directory
Directory of P:\Ice Cold\123456 abc - xyz\correspondence
file listing for the above directory
This is the macro I use to import text file and then strip data:
Public Sub DoTheImport()
Dim FName As Variant
FName = Application.GetOpenFilename _
(filefilter:="Text Files(*.txt),*.txt,All Files (*.*),*.*")
If FName = False Then
MsgBox "You didn't select a file"
Exit Sub
End If
ImportTextFile CStr(FName)
Workbooks.OpenText Filename:= _
FName, Origin:=xlWindows, StartRow:=1, DataType:=xlFixedWidth,
FieldInfo:= _
Array(Array(0, 1), Array(10, 1), Array(21, 1), Array(29, 1),
Array(39, 1))
End Sub
Public Sub ImportTextFile(FName As String)
Dim RowNdx As Integer
Dim ColNdx As Integer
Dim WholeLine As String
Dim Pos As Integer
Dim NextPos As Integer
Dim SaveColNdx As Integer
Application.ScreenUpdating = False
'On Error GoTo EndMacro:
SaveColNdx = ActiveCell.Column
RowNdx = ActiveCell.row
Open FName For Input Access Read As #1
EndMacro:
On Error GoTo 0
Application.ScreenUpdating = True
Close #1
End Sub
*******************************************************************************************************
'Autofilter column A with Directory and concatenate the directory path
which is split in multiple columns
Range("A5").Select
Selection.AutoFilter Field:=1, Criteria1:="=Directory"
Selection.CurrentRegion.Select
Selection.Offset(2, 0).Resize(Selection.Rows.Count - 1).Select
Selection.SpecialCells(xlVisible).Select
Range("F3").Select
ActiveCell.FormulaR1C1 = "=CONCATENATE(RC[-5]:RC[-1])"
Range("F4").Select
Columns("F:F").ColumnWidth = 14.71
Range("F3").Select
ActiveCell.FormulaR1C1 =
"=CONCATENATE(RC[-5],RC[-4],RC[-3],RC[-2],RC[-1])"
Application.CutCopyMode = False
Selection.Copy
Range("F4").Select
Range(Selection, Selection.End(xlDown)).Select
ActiveSheet.Paste
'After this I apply a paste special to get rid of the formula.
'the last step is to copy the respective directory in the rows in the
next column to the file name.
'Puts the directory path with file names in Coloumn G
Dim itotalrows, i, j
Selection.SpecialCells(xlCellTypeLastCell).Select
itotalrows = Mid(ActiveCell.Cells.AddressLocal,
InStrRev(ActiveCell.Cells.AddressLocal, "$") + 1)
'MsgBox itotalrows
For i = 1 To itotalrows
If (Range("f" & i) <> "") Then
j = i
i = i + 1
Do While (Range("E" & i) <> "")
Range("g" & i).Value = Range("f" & j).Value
'& "\" & Range("e" & i).Value
i = i + 1
Loop
i = i - 1
End If
Next i