Solved

One space gap before the last comma

Posted on 2007-03-27
3
305 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
  • 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 119

Expert Comment

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

Accepted Solution

by:
Rey Obrero 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

The first two articles in this short series — Using a Criteria Form to Filter Records (http://www.experts-exchange.com/A_6069.html) and Building a Custom Filter (http://www.experts-exchange.com/A_6070.html) — discuss in some detail how a form can be…
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…
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…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.

760 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now