PC Review


Reply
Thread Tools Rate Thread

character.delete doesn't work

 
 
svai
Guest
Posts: n/a
 
      11th Aug 2008
Hi!

I have created a loop in vb which deletes unnecessary spaces like this:

For i = 1 To langd - 1

If c.Characters(i, 1).Text = " " Then
While c.Characters(i + 1, 1).Text = " "
c.Characters(i + 1, 1).Delete
Wend
End If

Next

This is nested inside a "For each c in range..." and has never failed until
applied on on a cell containing this text:

Trädgårdsg 31, Trädgårdsg 26, Trädgårdsg 23, Trädgårdsg 21, Trådgårdsg 30,
Tingstug 16, Sörgärdsg 22, Sörgärdsg 20, Rådmansg 1, Nyg 23, Nikanderg 9,
Nikanderg 6, Nikanderg 11, Järnvägsg 4, Hospitalsg 15, Gyllenhjelmsg Fh 18,
Brinkska V 2 FH, Brinkska V 2 A

c.characters.count returns 257 and the loop gets stuck on character 218, the
space after Gyllenhjelmsg where there are two spaces. The line
"c.Characters(i + 1, 1).Delete" is then excecuted, but it seems like it
doesn't work since the while-statement is then evaluated as true, resulting
in an eternal loop. What is the problem? And does anyone have a nice solution?

Thanks
Stefan
 
Reply With Quote
 
 
 
 
Peter T
Guest
Posts: n/a
 
      11th Aug 2008
Cell.Characters Delete/Insert fails with 256+ characters.

But what are you trying to do, in your example what text do you want to end
up with.

Regards,
Peter T


"svai" <(E-Mail Removed)> wrote in message
news:F7A98BDF-AA21-4235-9EA6-(E-Mail Removed)...
> Hi!
>
> I have created a loop in vb which deletes unnecessary spaces like this:
>
> For i = 1 To langd - 1
>
> If c.Characters(i, 1).Text = " " Then
> While c.Characters(i + 1, 1).Text = " "
> c.Characters(i + 1, 1).Delete
> Wend
> End If
>
> Next
>
> This is nested inside a "For each c in range..." and has never failed
> until
> applied on on a cell containing this text:
>
> Trädgårdsg 31, Trädgårdsg 26, Trädgårdsg 23, Trädgårdsg 21, Trådgårdsg 30,
> Tingstug 16, Sörgärdsg 22, Sörgärdsg 20, Rådmansg 1, Nyg 23, Nikanderg 9,
> Nikanderg 6, Nikanderg 11, Järnvägsg 4, Hospitalsg 15, Gyllenhjelmsg Fh
> 18,
> Brinkska V 2 FH, Brinkska V 2 A
>
> c.characters.count returns 257 and the loop gets stuck on character 218,
> the
> space after Gyllenhjelmsg where there are two spaces. The line
> "c.Characters(i + 1, 1).Delete" is then excecuted, but it seems like it
> doesn't work since the while-statement is then evaluated as true,
> resulting
> in an eternal loop. What is the problem? And does anyone have a nice
> solution?
>
> Thanks
> Stefan



 
Reply With Quote
 
svai
Guest
Posts: n/a
 
      11th Aug 2008
Ok, that explains why... Thanks.

In my example, if I extract the part where it fails, I want to make
"Gyllenhjelmsg Fh 18" (with two space characters between "Gyllenhjelmsg" and
"Fh") into "Gyllenhjelmsg Fh 18" (with just one space character), just to
shorten the length of the text. In this example the difference is small, but
sometimes I have "Gyllenhjelmsg Fh 18" and want to
keep just one space character.

Best regards
Stefan

"Peter T" wrote:

> Cell.Characters Delete/Insert fails with 256+ characters.
>
> But what are you trying to do, in your example what text do you want to end
> up with.
>
> Regards,
> Peter T
>
>
> "svai" <(E-Mail Removed)> wrote in message
> news:F7A98BDF-AA21-4235-9EA6-(E-Mail Removed)...
> > Hi!
> >
> > I have created a loop in vb which deletes unnecessary spaces like this:
> >
> > For i = 1 To langd - 1
> >
> > If c.Characters(i, 1).Text = " " Then
> > While c.Characters(i + 1, 1).Text = " "
> > c.Characters(i + 1, 1).Delete
> > Wend
> > End If
> >
> > Next
> >
> > This is nested inside a "For each c in range..." and has never failed
> > until
> > applied on on a cell containing this text:
> >
> > Trädgårdsg 31, Trädgårdsg 26, Trädgårdsg 23, Trädgårdsg 21, Trådgårdsg 30,
> > Tingstug 16, Sörgärdsg 22, Sörgärdsg 20, Rådmansg 1, Nyg 23, Nikanderg 9,
> > Nikanderg 6, Nikanderg 11, Järnvägsg 4, Hospitalsg 15, Gyllenhjelmsg Fh
> > 18,
> > Brinkska V 2 FH, Brinkska V 2 A
> >
> > c.characters.count returns 257 and the loop gets stuck on character 218,
> > the
> > space after Gyllenhjelmsg where there are two spaces. The line
> > "c.Characters(i + 1, 1).Delete" is then excecuted, but it seems like it
> > doesn't work since the while-statement is then evaluated as true,
> > resulting
> > in an eternal loop. What is the problem? And does anyone have a nice
> > solution?
> >
> > Thanks
> > Stefan

>
>
>

 
Reply With Quote
 
Peter T
Guest
Posts: n/a
 
      11th Aug 2008
If I follow, any two or more spaces should only be a single space. Following
will not be affected by that 256 limit, and very considerably faster

Dim pos As Long
Dim sText As String
Dim c As Range

Set c = ActiveCell ' or in a loop

If InStr(1, c.Value, " ") Then
sText = cValue
Do
pos = InStr(sText, " ")
If pos Then sText = Replace(sText, " ", " ") ' not xl97
Loop Until pos = 0
c.Value = sText
End If

End Sub

Might be worth checking first (in the loop) that c refers to a non-formula
text cell

Regards,
Peter T


"svai" <(E-Mail Removed)> wrote in message
news:4F845FAE-17D4-47DD-B4B1-(E-Mail Removed)...
> Ok, that explains why... Thanks.
>
> In my example, if I extract the part where it fails, I want to make
> "Gyllenhjelmsg Fh 18" (with two space characters between "Gyllenhjelmsg"
> and
> "Fh") into "Gyllenhjelmsg Fh 18" (with just one space character), just to
> shorten the length of the text. In this example the difference is small,
> but
> sometimes I have "Gyllenhjelmsg Fh 18" and want to
> keep just one space character.
>
> Best regards
> Stefan
>
> "Peter T" wrote:
>
>> Cell.Characters Delete/Insert fails with 256+ characters.
>>
>> But what are you trying to do, in your example what text do you want to
>> end
>> up with.
>>
>> Regards,
>> Peter T
>>
>>
>> "svai" <(E-Mail Removed)> wrote in message
>> news:F7A98BDF-AA21-4235-9EA6-(E-Mail Removed)...
>> > Hi!
>> >
>> > I have created a loop in vb which deletes unnecessary spaces like this:
>> >
>> > For i = 1 To langd - 1
>> >
>> > If c.Characters(i, 1).Text = " " Then
>> > While c.Characters(i + 1, 1).Text = " "
>> > c.Characters(i + 1, 1).Delete
>> > Wend
>> > End If
>> >
>> > Next
>> >
>> > This is nested inside a "For each c in range..." and has never failed
>> > until
>> > applied on on a cell containing this text:
>> >
>> > Trädgårdsg 31, Trädgårdsg 26, Trädgårdsg 23, Trädgårdsg 21, Trådgårdsg
>> > 30,
>> > Tingstug 16, Sörgärdsg 22, Sörgärdsg 20, Rådmansg 1, Nyg 23, Nikanderg
>> > 9,
>> > Nikanderg 6, Nikanderg 11, Järnvägsg 4, Hospitalsg 15, Gyllenhjelmsg
>> > Fh
>> > 18,
>> > Brinkska V 2 FH, Brinkska V 2 A
>> >
>> > c.characters.count returns 257 and the loop gets stuck on character
>> > 218,
>> > the
>> > space after Gyllenhjelmsg where there are two spaces. The line
>> > "c.Characters(i + 1, 1).Delete" is then excecuted, but it seems like it
>> > doesn't work since the while-statement is then evaluated as true,
>> > resulting
>> > in an eternal loop. What is the problem? And does anyone have a nice
>> > solution?
>> >
>> > Thanks
>> > Stefan

>>
>>
>>



 
Reply With Quote
 
Dave Peterson
Guest
Posts: n/a
 
      11th Aug 2008
c.value = application.trim(c.value)

will remove leading/trailing/and those extra internal spaces.

svai wrote:
>
> Hi!
>
> I have created a loop in vb which deletes unnecessary spaces like this:
>
> For i = 1 To langd - 1
>
> If c.Characters(i, 1).Text = " " Then
> While c.Characters(i + 1, 1).Text = " "
> c.Characters(i + 1, 1).Delete
> Wend
> End If
>
> Next
>
> This is nested inside a "For each c in range..." and has never failed until
> applied on on a cell containing this text:
>
> Trädgårdsg 31, Trädgårdsg 26, Trädgårdsg 23, Trädgårdsg 21, Trådgårdsg 30,
> Tingstug 16, Sörgärdsg 22, Sörgärdsg 20, Rådmansg 1, Nyg 23, Nikanderg 9,
> Nikanderg 6, Nikanderg 11, Järnvägsg 4, Hospitalsg 15, Gyllenhjelmsg Fh 18,
> Brinkska V 2 FH, Brinkska V 2 A
>
> c.characters.count returns 257 and the loop gets stuck on character 218, the
> space after Gyllenhjelmsg where there are two spaces. The line
> "c.Characters(i + 1, 1).Delete" is then excecuted, but it seems like it
> doesn't work since the while-statement is then evaluated as true, resulting
> in an eternal loop. What is the problem? And does anyone have a nice solution?
>
> Thanks
> Stefan


--

Dave Peterson
 
Reply With Quote
 
svai
Guest
Posts: n/a
 
      11th Aug 2008
Nice! Works like a charm, thanks!

"Peter T" wrote:

> If I follow, any two or more spaces should only be a single space. Following
> will not be affected by that 256 limit, and very considerably faster
>
> Dim pos As Long
> Dim sText As String
> Dim c As Range
>
> Set c = ActiveCell ' or in a loop
>
> If InStr(1, c.Value, " ") Then
> sText = cValue
> Do
> pos = InStr(sText, " ")
> If pos Then sText = Replace(sText, " ", " ") ' not xl97
> Loop Until pos = 0
> c.Value = sText
> End If
>
> End Sub
>
> Might be worth checking first (in the loop) that c refers to a non-formula
> text cell
>
> Regards,
> Peter T
>
>
> "svai" <(E-Mail Removed)> wrote in message
> news:4F845FAE-17D4-47DD-B4B1-(E-Mail Removed)...
> > Ok, that explains why... Thanks.
> >
> > In my example, if I extract the part where it fails, I want to make
> > "Gyllenhjelmsg Fh 18" (with two space characters between "Gyllenhjelmsg"
> > and
> > "Fh") into "Gyllenhjelmsg Fh 18" (with just one space character), just to
> > shorten the length of the text. In this example the difference is small,
> > but
> > sometimes I have "Gyllenhjelmsg Fh 18" and want to
> > keep just one space character.
> >
> > Best regards
> > Stefan
> >
> > "Peter T" wrote:
> >
> >> Cell.Characters Delete/Insert fails with 256+ characters.
> >>
> >> But what are you trying to do, in your example what text do you want to
> >> end
> >> up with.
> >>
> >> Regards,
> >> Peter T
> >>
> >>
> >> "svai" <(E-Mail Removed)> wrote in message
> >> news:F7A98BDF-AA21-4235-9EA6-(E-Mail Removed)...
> >> > Hi!
> >> >
> >> > I have created a loop in vb which deletes unnecessary spaces like this:
> >> >
> >> > For i = 1 To langd - 1
> >> >
> >> > If c.Characters(i, 1).Text = " " Then
> >> > While c.Characters(i + 1, 1).Text = " "
> >> > c.Characters(i + 1, 1).Delete
> >> > Wend
> >> > End If
> >> >
> >> > Next
> >> >
> >> > This is nested inside a "For each c in range..." and has never failed
> >> > until
> >> > applied on on a cell containing this text:
> >> >
> >> > Trädgårdsg 31, Trädgårdsg 26, Trädgårdsg 23, Trädgårdsg 21, Trådgårdsg
> >> > 30,
> >> > Tingstug 16, Sörgärdsg 22, Sörgärdsg 20, Rådmansg 1, Nyg 23, Nikanderg
> >> > 9,
> >> > Nikanderg 6, Nikanderg 11, Järnvägsg 4, Hospitalsg 15, Gyllenhjelmsg
> >> > Fh
> >> > 18,
> >> > Brinkska V 2 FH, Brinkska V 2 A
> >> >
> >> > c.characters.count returns 257 and the loop gets stuck on character
> >> > 218,
> >> > the
> >> > space after Gyllenhjelmsg where there are two spaces. The line
> >> > "c.Characters(i + 1, 1).Delete" is then excecuted, but it seems like it
> >> > doesn't work since the while-statement is then evaluated as true,
> >> > resulting
> >> > in an eternal loop. What is the problem? And does anyone have a nice
> >> > solution?
> >> >
> >> > Thanks
> >> > Stefan
> >>
> >>
> >>

>
>
>

 
Reply With Quote
 
Peter T
Guest
Posts: n/a
 
      11th Aug 2008
Go with Dave's Trim, not sure why I didn't suggest that myself!

Regards,
Peter T

"Peter T" <peter_t@discussions> wrote in message
news:%23jIPWo6%(E-Mail Removed)...
> If I follow, any two or more spaces should only be a single space.
> Following will not be affected by that 256 limit, and very considerably
> faster
>
> Dim pos As Long
> Dim sText As String
> Dim c As Range
>
> Set c = ActiveCell ' or in a loop
>
> If InStr(1, c.Value, " ") Then
> sText = cValue
> Do
> pos = InStr(sText, " ")
> If pos Then sText = Replace(sText, " ", " ") ' not xl97
> Loop Until pos = 0
> c.Value = sText
> End If
>
> End Sub
>
> Might be worth checking first (in the loop) that c refers to a non-formula
> text cell
>
> Regards,
> Peter T
>
>
> "svai" <(E-Mail Removed)> wrote in message
> news:4F845FAE-17D4-47DD-B4B1-(E-Mail Removed)...
>> Ok, that explains why... Thanks.
>>
>> In my example, if I extract the part where it fails, I want to make
>> "Gyllenhjelmsg Fh 18" (with two space characters between "Gyllenhjelmsg"
>> and
>> "Fh") into "Gyllenhjelmsg Fh 18" (with just one space character), just to
>> shorten the length of the text. In this example the difference is small,
>> but
>> sometimes I have "Gyllenhjelmsg Fh 18" and want to
>> keep just one space character.
>>
>> Best regards
>> Stefan
>>
>> "Peter T" wrote:
>>
>>> Cell.Characters Delete/Insert fails with 256+ characters.
>>>
>>> But what are you trying to do, in your example what text do you want to
>>> end
>>> up with.
>>>
>>> Regards,
>>> Peter T
>>>
>>>
>>> "svai" <(E-Mail Removed)> wrote in message
>>> news:F7A98BDF-AA21-4235-9EA6-(E-Mail Removed)...
>>> > Hi!
>>> >
>>> > I have created a loop in vb which deletes unnecessary spaces like
>>> > this:
>>> >
>>> > For i = 1 To langd - 1
>>> >
>>> > If c.Characters(i, 1).Text = " " Then
>>> > While c.Characters(i + 1, 1).Text = " "
>>> > c.Characters(i + 1, 1).Delete
>>> > Wend
>>> > End If
>>> >
>>> > Next
>>> >
>>> > This is nested inside a "For each c in range..." and has never failed
>>> > until
>>> > applied on on a cell containing this text:
>>> >
>>> > Trädgårdsg 31, Trädgårdsg 26, Trädgårdsg 23, Trädgårdsg 21, Trådgårdsg
>>> > 30,
>>> > Tingstug 16, Sörgärdsg 22, Sörgärdsg 20, Rådmansg 1, Nyg 23, Nikanderg
>>> > 9,
>>> > Nikanderg 6, Nikanderg 11, Järnvägsg 4, Hospitalsg 15, Gyllenhjelmsg
>>> > Fh
>>> > 18,
>>> > Brinkska V 2 FH, Brinkska V 2 A
>>> >
>>> > c.characters.count returns 257 and the loop gets stuck on character
>>> > 218,
>>> > the
>>> > space after Gyllenhjelmsg where there are two spaces. The line
>>> > "c.Characters(i + 1, 1).Delete" is then excecuted, but it seems like
>>> > it
>>> > doesn't work since the while-statement is then evaluated as true,
>>> > resulting
>>> > in an eternal loop. What is the problem? And does anyone have a nice
>>> > solution?
>>> >
>>> > Thanks
>>> > Stefan
>>>
>>>
>>>

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
International Character Set doesn't work in Outlook 2003 DickSF Microsoft Outlook Discussion 1 23rd Jan 2009 02:31 AM
workaround for problem hyperlinks in Excel (doesn't work if pathcontains # character) mad.scientist.jr@gmail.com Microsoft Excel Worksheet Functions 0 21st Dec 2007 10:04 PM
IF statements with * character doesn't work canadiangal@gmail.com Microsoft Excel Worksheet Functions 5 8th Nov 2006 03:47 PM
How to delete too large outgoing mail - delete key doesn't work =?Utf-8?B?S2F0aGllQg==?= Microsoft Outlook Discussion 1 10th Oct 2005 04:42 PM
Ctrl-Delete (Delete word) occasionally doesn't work =?Utf-8?B?U2FuZHkgSm9obnNvbg==?= Microsoft Word Document Management 1 16th Jun 2005 07:25 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 05:52 PM.