Solved

One space gap before the last comma

Posted on 2007-03-27
3
313 Views
Last Modified: 2008-02-01
Hi: Can any one please tell me in VBA code as this process works but there is a one space gap before comma in the last column. My question is how to i remove this one space gap before the last comma

SAMPLE DATA
AC0216425,3/10/2007,166.50 ,83.25
JM0338775,3/10/2007,100.00 ,50.00
JM0464931,3/9/2007,200.00 ,100.00
JM0337460,3/10/2007,93.75 ,46.87
JM0509209,3/10/2007,236.03 ,118.01
AZ0501422,3/10/2007,250.00 ,125.00
AC0522680,3/10/2007,201.80 ,100.90
JJ0402934,3/7/2007,200.00 ,100.00
JJ0501329,3/10/2007,356.50 ,178.25
JM0524012,3/10/2007,60.00 ,30.00
JM0431817,3/9/2007,100.00 ,50.00
JM0441390,3/10/2007,249.50 ,124.75
Y0406978,3/9/2007,105.00 ,52.50
JM0531129,3/8/2007,125.00 ,62.50
AC0338563,3/13/2007,50.00 ,25.00
RK0502896,3/9/2007,158.00 ,79.00
JJ0402244,3/9/2007,75.00 ,37.50

Private Sub excelfileprocess()

Dim xlApp As Excel.Application
Dim xlWB As Excel.Workbook
Dim xlWS As Excel.Worksheet
Dim strTempXL As String
Dim strTempTXT As String
Dim WShell As Object
Dim FileName As String
Dim FolderAddress As String
Dim strDestination As String
strDestination = OutputFileText.Text
FileName = getFileNam(OutputFileText.Text)
FolderAddress = getDir(OutputFileText.Text)
strTempTXT = FolderAddress & "TEMP_TXT" & Format$(Now, "yyyymmdd-hhmmss") & ".txt"
strTempXL = FolderAddress & "TEMP_XL" & Format$(Now, "yyyymmdd-hhmmss") & ".xls"
FileCopy InputFileText.Text, strTempXL
Set xlApp = New Excel.Application
Set xlWB = xlApp.Workbooks.Open(strTempXL)
Set xlWS = xlApp.Worksheets(1)
    xlWS.Range("A:A,B:B,D:D,E:E,G:G,H:H,I:I,K:K,L:L,M:M,N:N,O:O,P:P,R:R,S:S,T:T,U:U,V:V,W:W").Delete Shift:=xlToLeft
    xlWS.Columns("A:A").Insert Shift:=xlToRight
    xlWS.Columns("E:E").Cut
    xlWS.Range("A1").Select
    ActiveSheet.Paste
    xlWS.Columns("B:B").Insert Shift:=xlToRight
    xlWS.Columns("E:E").Cut
    xlWS.Range("B1").Select
    ActiveSheet.Paste
    xlWS.Columns("C:D").NumberFormat = "#,##0.00_);(#,##0.00)"
    Debug.Print OutputFileText.Text
    xlWB.SaveAs strTempTXT, xlCSV
xlWB.Close SaveChanges:=True
Set xlWS = Nothing
Set xlWB = Nothing
xlApp.Quit
Set xlApp = Nothing
Dim fso As New FileSystemObject
Dim tsRead As TextStream
Dim tsWrite As TextStream
Dim inLine As String
Set tsRead = fso.OpenTextFile(strTempTXT, ForReading)
Set tsWrite = fso.OpenTextFile(strDestination, ForWriting)
If Not tsRead.AtEndOfStream Then
  tsRead.SkipLine
  While Not tsRead.AtEndOfStream
     inLine = tsRead.ReadLine
     tsWrite.WriteLine inLine
  Wend
End If
tsRead.Close
tsWrite.Close
Kill strTempXL
Kill strTempTXT
MsgBox "Done"
0
Comment
Question by:mustish1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 65

Expert Comment

by:Jim Horn
ID: 18800338
>My question is how to i remove this one space gap before the last comma
Couple of ways...

Trim(YourString) removes leading and trailing spaces
Val(YourString) returns the numerical value starting with the left-most character

Then, you can use CCur( the above), CLng( the above), etc. to convert it to a currency, long integer, etc.
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 18800339
replace("AC0216425,3/10/2007,166.50 ,83.25", space(1),"")
0
 
LVL 120

Accepted Solution

by:
Rey Obrero (Capricorn1) earned 500 total points
ID: 18800396
is the data coming from the textfile  strTempTXT ?

Set tsRead = fso.OpenTextFile(strTempTXT, ForReading)
Set tsWrite = fso.OpenTextFile(strDestination, ForWriting)
If Not tsRead.AtEndOfStream Then
  tsRead.SkipLine
  While Not tsRead.AtEndOfStream
     inLine = Replace(tsRead.ReadLine,space(1),"")   '<<< use this line
     tsWrite.WriteLine inLine
  Wend
End If
0

Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question