Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

One space gap before the last comma

Posted on 2007-03-27
3
Medium Priority
?
316 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 66

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 2000 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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Access custom database properties are useful for storing miscellaneous bits of information in a format that persists through database closing and reopening.  This article shows how to create and use them.
The Windows Phone Theme Colours is a tight, powerful, and well balanced palette. This tiny Access application makes it a snap to select and pick a value. And it doubles as an intro to implementing WithEvents, one of Access' hidden gems.
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

704 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