Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Excel VBA > Excel VBA
|
Excel VBA Discuss using VBA for Excel programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Excel VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 25th, 2007, 08:32 PM
Authorized User
 
Join Date: Mar 2007
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Default How to fix string length

I want to convert an excel file into text file.

In my excel file there is four columns. When i convert to text file i want to fix the string with four digital place, horizontalAlignment = xlLeft and no comma or tab between the four column data. For example if the data is 100,10,10,10 in excel file then I want to show in " 100"," 10"," 10"," 10" in the text file.(spacing fill up the digital place from Xright)

 
Old July 26th, 2007, 08:53 AM
Friend of Wrox
 
Join Date: Feb 2007
Posts: 163
Thanks: 0
Thanked 2 Times in 2 Posts
Default

You would have to create your own text file and write it out that way. Here's a simple example:
-----------------------------------------------------------------------
Private Sub ButtonName_Click()

' Writes out first 4 columns as special formatted comma separated values
  Dim oPageName As Object, sToWrite As String, iRowOn As Long
  Set oPageName = CreateObject("Scripting.FileSystemObject") _
    .CreateTextFile("C:\MyFile.txt", True)
  iRowOn = 1
  Do While ActiveSheet.Cells(iRowOn, 1).value <> "" 'stops at first blank line... or whatever criteria you have
    sToWrite = ""
    With ActiveSheet
      sToWrite = PreFormat(.Cells(iRowOn,1).Value) & "," & PreFormat(.Cells(iRowOn,2).Value) & "," _
        & PreFormat(.Cells(iRowOn,3).Value) & "," & PreFormat(.Cells(iRowOn, 4).Value) 'Could loop this if > 3 or 4
    End With
    oPageName.WriteLine(sToWrite)
    iRowOn = iRowOn + 1
  loop
  oPageName.Close

End Sub

Private Function PreFormat(sPassed As Variant) As String

' Makes sure at least 4 characters, pads with leading spaces
  PreFormat = "" & Trim(sPassed)
  Do While Len(PreFormat) < 4
    PreFormat = " " & PreFormat
  Loop
  PreFormat = """" & PreFormat & """"

End Function
-----------------------------------------------------------------------

Hope this helps.
 
Old July 26th, 2007, 08:55 AM
Friend of Wrox
 
Join Date: Feb 2007
Posts: 163
Thanks: 0
Thanked 2 Times in 2 Posts
Default

Sorry, forgot the last line of code for the function... edited it to make it correct. ^^:






Similar Threads
Thread Thread Starter Forum Replies Last Post
string-length problem alapati.sasi XSLT 3 July 4th, 2007 10:08 AM
How to loop a command for a string length? LordJaffa Excel VBA 5 June 5th, 2007 10:20 PM
Chap 3, "Length of String" richajos BOOK: Beginning Visual Basic 2005 ISBN: 978-0-7645-7401-6 1 February 25th, 2006 08:38 PM
Syntax help with string-length and XPath EstherMStrom XSLT 1 February 9th, 2005 08:02 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.