View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Soo Cheon Jheong[_2_] Soo Cheon Jheong[_2_] is offline
external usenet poster
 
Posts: 46
Default Parse (TRIM) File Name into seperate columns

Diana,


(Data)
---------------A---------------------B---------C--------
1 File name
2 ABC-040901-1015-incoming1.dat
3 ABD-040902-1016-incoming2.dat
4 ABE-040903-1017-incoming3.dat
5 .
6 .
7 .
---------------A---------------------B---------C--------


Macro A
- - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Option Explicit
Sub Test_1()

Dim CL As Range
Dim Pos1 As Integer
Dim Pos2 As Integer
Dim R As Long
Dim T As String

Application.ScreenUpdating = False

R = Cells(Rows.Count, 1).End(xlUp).Row
If R = 1 Then GoTo e:

With Range("B2:C" & R)
.ClearContents
.Rows(0).Value = Array("File name", "Code")
End With

For Each CL In Range("A2:A" & R)

Pos1 = InStr(1, CL.Text, "-")
Pos2 = InStrRev(CL.Text, "-", -1)

CL(1, 2).Value = Left(CL.Text, Pos1 - 1)
CL(1, 3).Value = Mid(CL.Text, Pos1 + 1, Pos2 - Pos1 - 1)

Next

e:
Application.ScreenUpdating = True

End Sub
- - - - - - - - - - - - - - - - - - - - - - - - - - - - -


Macro B
- - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Option Explicit
Sub Test_2()

Dim R As Long

Application.ScreenUpdating = False

R = Cells(Rows.Count, 1).End(xlUp).Row
If R = 1 Then GoTo e:

With Range("B2:C" & R)
.ClearContents
.Rows(0).Value = Array("File name", "Code")
.Columns(1).Formula = "=LEFT(A2,FIND(""-"",A2)-1)"
.Columns(2).Formula = "=MID(A2,FIND(""-"",A2)+1," & _
"FIND(""-"",A2,FIND(""-"",A2," & _
"FIND(""-"",A2)+1)+1)-1-FIND(""-"",A2))"
.Value = .Value
End With

e:
Application.ScreenUpdating = True

End Sub
- - - - - - - - - - - - - - - - - - - - - - - - - - - - -


--
Regards,
Soo Cheon Jheong
_ _
^вп^
--