Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 33
Default Counting the length of a string and adding spaces...

I'm currently constructing the string using the following code: -

textstring = (Cells(ActiveCell.Row, make).Value) & " - " &
(Cells(ActiveCell.Row, model).Value) & " - " & (Cells(ActiveCell.Row,
expirary).Value) & " - " & "£" & (Cells(ActiveCell.Row,
commision).Value) & Chr(10)

This produces something like the following, eg.

Honda - Civic 1.5 5dr - 10/02/2001 - £674
Honda - Civic Sport 1.4i 5dr Hatch - 24/06/2001 - £400
Renault - Clio 16V 3dr Hatch - 24/06/2001 - £622

But I would like the information to appear as if it was in columns.
More like this: -

Honda - Civic 1.5 5dr - 10/02/2001 - £674
Honda - Civic Sport 1.4i 5dr Hatch - 24/06/2001 - £400
Renault - Clio 16V 3dr Hatch - 24/06/2001 - £622

So I need to figure out how to add spaces between each variable as it
is put into the string, depending upon the size of the variable. I
assume there are functions that let you count the length of a string,
but i'm not sure how I would go about changing the spacing between the
variables upon the result of this.

Any ideas? or Code? it would be greatly appreciated!

Thankyou

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10,593
Default Counting the length of a string and adding spaces...

With ActiveCell
sMake = Cells(.Row, make).Value
sModel = Cells(.Row, model).Value
sExpiry = Cells(.Row, expirary).Value
sCommission = Cells(.Row, commision).Value
textstring = sMake & Space(20 - Len(sMake)) & " - " & _
sModel & Space(30 - Len(sModel)) & " - " & _
sExpiry & Space(30 - Len(sExpiry)) & " - " & _
"£" & sCommission & Space(30 - Len(sCommission)) &
Chr(10)
End With



--
HTH

Bob Phillips

(replace somewhere in email address with gmail if mailing direct)

wrote in message
oups.com...
I'm currently constructing the string using the following code: -

textstring = (Cells(ActiveCell.Row, make).Value) & " - " &
(Cells(ActiveCell.Row, model).Value) & " - " & (Cells(ActiveCell.Row,
expirary).Value) & " - " & "£" & (Cells(ActiveCell.Row,
commision).Value) & Chr(10)

This produces something like the following, eg.

Honda - Civic 1.5 5dr - 10/02/2001 - £674
Honda - Civic Sport 1.4i 5dr Hatch - 24/06/2001 - £400
Renault - Clio 16V 3dr Hatch - 24/06/2001 - £622

But I would like the information to appear as if it was in columns.
More like this: -

Honda - Civic 1.5 5dr - 10/02/2001 - £674
Honda - Civic Sport 1.4i 5dr Hatch - 24/06/2001 - £400
Renault - Clio 16V 3dr Hatch - 24/06/2001 - £622

So I need to figure out how to add spaces between each variable as it
is put into the string, depending upon the size of the variable. I
assume there are functions that let you count the length of a string,
but i'm not sure how I would go about changing the spacing between the
variables upon the result of this.

Any ideas? or Code? it would be greatly appreciated!

Thankyou


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,391
Default Counting the length of a string and adding spaces...

Are you using a fixed-width font for display ?
If not, there's no easy way to get the visual length of the parts.
i.e. the 2 string "WWW" and "iii" are both 3 chars, but considerable
different in their displayed length.

NickHK

wrote in message
oups.com...
I'm currently constructing the string using the following code: -

textstring = (Cells(ActiveCell.Row, make).Value) & " - " &
(Cells(ActiveCell.Row, model).Value) & " - " & (Cells(ActiveCell.Row,
expirary).Value) & " - " & "£" & (Cells(ActiveCell.Row,
commision).Value) & Chr(10)

This produces something like the following, eg.

Honda - Civic 1.5 5dr - 10/02/2001 - £674
Honda - Civic Sport 1.4i 5dr Hatch - 24/06/2001 - £400
Renault - Clio 16V 3dr Hatch - 24/06/2001 - £622

But I would like the information to appear as if it was in columns.
More like this: -

Honda - Civic 1.5 5dr - 10/02/2001 - £674
Honda - Civic Sport 1.4i 5dr Hatch - 24/06/2001 - £400
Renault - Clio 16V 3dr Hatch - 24/06/2001 - £622

So I need to figure out how to add spaces between each variable as it
is put into the string, depending upon the size of the variable. I
assume there are functions that let you count the length of a string,
but i'm not sure how I would go about changing the spacing between the
variables upon the result of this.

Any ideas? or Code? it would be greatly appreciated!

Thankyou


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 33
Default Counting the length of a string and adding spaces...

Thank you Bob that looks ideal, but there is a slight problem at the
moment - when I run the code i get an error that mentions an 'invalid
procedure call or argument'. When I click debug the following code is
highlighted: -

textstring = sMake & Space(20 - Len(sMake)) & " - " & _
sModel & Space(30 - Len(sModel)) & " - " & _
sExpiry & Space(30 - Len(sExpiry)) & " - " & _
"£" & sCommission & Space(30 - Len(sCommission)) &
Chr(10)


Thanks,

Gary

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 33
Default Counting the length of a string and adding spaces...

On the fixed width - how do i use a fixed width font?

Thanks,

Gary



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,391
Default Counting the length of a string and adding spaces...

Set the font of whatever you are using to display this string to something
like "Courier New".

NickHK

wrote in message
oups.com...
On the fixed width - how do i use a fixed width font?

Thanks,

Gary



  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10,593
Default Counting the length of a string and adding spaces...

See if this helps

With ActiveCell
sMake = Cells(.Row, make).Value
sModel = Cells(.Row, model).Value
sExpiry = Cells(.Row, expirary).Value
sCommission = Cells(.Row, commision).Value
textstring = sMake & Space(20 - Len(sMake)) & " - " & _
sModel & Space(30 - Len(sModel)) & " - " & _
sExpiry & Space(30 - Len(sExpiry)) & " - " & _
"£" & sCommission & Space(30 - Len(sCommission)) & _
Chr(10)
End With



--
HTH

Bob Phillips

(replace somewhere in email address with gmail if mailing direct)

wrote in message
ups.com...
Thank you Bob that looks ideal, but there is a slight problem at the
moment - when I run the code i get an error that mentions an 'invalid
procedure call or argument'. When I click debug the following code is
highlighted: -

textstring = sMake & Space(20 - Len(sMake)) & " - " & _
sModel & Space(30 - Len(sModel)) & " - " & _
sExpiry & Space(30 - Len(sExpiry)) & " - " & _
"£" & sCommission & Space(30 - Len(sCommission)) &
Chr(10)


Thanks,

Gary


  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 33
Default Counting the length of a string and adding spaces...

Hi Bob,

That is giving the same error, i've checked and I think that code is
identical to the first code you posted.

Thanks again,

Gary.

Bob Phillips wrote:
See if this helps

With ActiveCell
sMake = Cells(.Row, make).Value
sModel = Cells(.Row, model).Value
sExpiry = Cells(.Row, expirary).Value
sCommission = Cells(.Row, commision).Value
textstring = sMake & Space(20 - Len(sMake)) & " - " & _
sModel & Space(30 - Len(sModel)) & " - " & _
sExpiry & Space(30 - Len(sExpiry)) & " - " & _
"£" & sCommission & Space(30 - Len(sCommission)) & _
Chr(10)
End With



--
HTH

Bob Phillips

(replace somewhere in email address with gmail if mailing direct)

wrote in message
ups.com...
Thank you Bob that looks ideal, but there is a slight problem at the
moment - when I run the code i get an error that mentions an 'invalid
procedure call or argument'. When I click debug the following code is
highlighted: -

textstring = sMake & Space(20 - Len(sMake)) & " - " & _
sModel & Space(30 - Len(sModel)) & " - " & _
sExpiry & Space(30 - Len(sExpiry)) & " - " & _
"£" & sCommission & Space(30 - Len(sCommission)) &
Chr(10)


Thanks,

Gary


  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10,593
Default Counting the length of a string and adding spaces...

It is, but I thought it might be word-wrap, so I split a line.

--
HTH

Bob Phillips

(replace somewhere in email address with gmail if mailing direct)

wrote in message
ups.com...
Hi Bob,

That is giving the same error, i've checked and I think that code is
identical to the first code you posted.

Thanks again,

Gary.

Bob Phillips wrote:
See if this helps

With ActiveCell
sMake = Cells(.Row, make).Value
sModel = Cells(.Row, model).Value
sExpiry = Cells(.Row, expirary).Value
sCommission = Cells(.Row, commision).Value
textstring = sMake & Space(20 - Len(sMake)) & " - " & _
sModel & Space(30 - Len(sModel)) & " - " & _
sExpiry & Space(30 - Len(sExpiry)) & " - " & _
"£" & sCommission & Space(30 - Len(sCommission)) & _
Chr(10)
End With



--
HTH

Bob Phillips

(replace somewhere in email address with gmail if mailing direct)

wrote in message
ups.com...
Thank you Bob that looks ideal, but there is a slight problem at the
moment - when I run the code i get an error that mentions an 'invalid
procedure call or argument'. When I click debug the following code is
highlighted: -

textstring = sMake & Space(20 - Len(sMake)) & " - " & _
sModel & Space(30 - Len(sModel)) & " - " & _
sExpiry & Space(30 - Len(sExpiry)) & " - " & _
"£" & sCommission & Space(30 - Len(sCommission)) &
Chr(10)


Thanks,

Gary



  #10   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 33
Default Counting the length of a string and adding spaces...

Dear Bob I have noticed that it is only generating the error on certain
rows, if i run it on a row it will work fine, but then if i run it on
some other row it generates the problem.
I've included the full code for the macro i'm using here, which the
code you gave me was a part of. Could you please take a look and see
where you think it might be going wrong?

.....

Sub SelectEveryThird()
Dim ilastcolumn As Long
Dim textstring As String

Application.ScreenUpdating = False

ilastcolumn = (Cells(ActiveCell.Row,
Columns.Count).End(xlToLeft).Column)

' 42 is the number of columns between repeated fields.
' 6 is the numberical reference for the first column that contains make
data.

model = 7
expirary = 38
commision = 42

For make = 6 To ilastcolumn Step 42

With ActiveCell
sMake = Cells(.Row, make).Value
sModel = Cells(.Row, model).Value
sExpiry = Cells(.Row, expirary).Value
sCommission = Cells(.Row, commision).Value
textstring = sMake & Space(20 - Len(sMake)) & " - " & _
sModel & Space(30 - Len(sModel)) & " - " & _
sExpiry & Space(30 - Len(sExpiry)) & " - " & _
"£" & sCommission & Space(30 - Len(sCommission)) &
_
Chr(10)
End With


If model + 42 = ilastcolumn Then
model = ilastcolumn
Else
model = model + 42
End If

If expirary + 42 = ilastcolumn Then
k = expirary
Else
expirary = expirary + 42
End If

If commision + 42 = ilastcolumn Then
commision = ilastcolumn
Else
commision = commision + 42
End If

Next make

Cells(ActiveCell.Row, "A").Value = textstring
End Sub



  #11   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 33
Default Counting the length of a string and adding spaces...

The only thing to note is that the textstring= in the code I use starts
textstring= textring & .. because i'm compiling a list of order data.

  #12   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 33
Default Counting the length of a string and adding spaces...

I have figured out the problem, the 20, and 30 for the spaces were not
long enough so were generating negative spacing for high value strings.
Having fixed this i have one final hurdle.

The above code is not creating a list of data, instead it is creating
only one line of data into Cell A.

I had though that by having textstring = textstring & ...

that the textstring would increase by the size of the newest textstring
at each interation but for some reason i'm only getting one line of
data, regardless of the number of columns?

Many Thanks,

Gary.

  #13   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 33
Default Counting the length of a string and adding spaces...

Sorted. I had misspelled the variable.
It's working like a gem now.

Thanks to both of you!

All the best,

Gary

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Data Validation, Need text any length no spaces BB Excel Discussion (Misc queries) 5 August 8th 07 12:46 PM
number as string and adding spaces to it A.New Excel Programming 2 January 26th 05 03:22 AM
Counting string length The Wonder Thing[_3_] Excel Programming 4 August 3rd 04 09:23 PM
counting spaces in a string xnman Excel Programming 7 May 9th 04 03:06 PM
counting spaces in a string xnman Excel Programming 4 December 16th 03 01:36 AM


All times are GMT +1. The time now is 02:41 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"