REPLACE 0 with space

User avatar
sal21
PlatinumLounger
Posts: 4357
Joined: 26 Apr 2010, 17:36

REPLACE 0 with space

Post by sal21 »

I have this:
....
dim myVar as string
myVar "00014"
....

i need to replace only the left zero with space

in my case

" 14"

User avatar
HansV
Administrator
Posts: 78500
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: REPLACE 0 with space

Post by HansV »

Code: Select all

    Dim MyLen As Long
    MyLen = Len(MyVar)
    MyVar = CStr(Val(MyVar))
    MyVar = Space(MyLen - Len(MyVar)) & MyVar
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4357
Joined: 26 Apr 2010, 17:36

Re: REPLACE 0 with space

Post by sal21 »

HansV wrote:

Code: Select all

    Dim MyLen As Long
    MyLen = Len(MyVar)
    MyVar = CStr(Val(MyVar))
    MyVar = Space(MyLen - Len(MyVar)) & MyVar
sorry Hans,
but my really prob is to align item correct in combobox,
I have arranged a code in old your reply here:
http://www.eileenslounge.com/viewtopic. ... lit=+align

... my actual code:

Code: Select all

With Me.SOSP
        .Clear
        For I = 1 To Y

            MIASTRINGA = Val(Left(ARR_SOSP(I), 5))
            strLine = Space(5 - Len(MIASTRINGA)) & MIASTRINGA
            Debug.Print strLine & Mid(ARR_SOSP(I), 6, 48)
            .AddItem strLine & Mid(ARR_SOSP(I), 6, 48)
            
            DoEvents
        Next I

    End With

For test i see all correct in immediate window but not in combobox!!!!
You do not have the required permissions to view the files attached to this post.

User avatar
HansV
Administrator
Posts: 78500
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: REPLACE 0 with space

Post by HansV »

You have to set the font for the combo box to a fixed-width font such as Courier New.
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4357
Joined: 26 Apr 2010, 17:36

Re: REPLACE 0 with space

Post by sal21 »

HansV wrote:You have to set the font for the combo box to a fixed-width font such as Courier New.
now work....
But i dont like Courier New, i use in my all project Arial

User avatar
HansV
Administrator
Posts: 78500
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: REPLACE 0 with space

Post by HansV »

Arial, like Times New Roman, is a variable-width font. It will be virtually impossible to align the text correctly if you use a variable-width font.
Instead of Courier New, you could use Consolas, a newer fixed-width font from Microsoft:
S0593.png
You do not have the required permissions to view the files attached to this post.
Best wishes,
Hans

User avatar
Jay Freedman
Microsoft MVP
Posts: 1318
Joined: 24 May 2013, 15:33
Location: Warminster, PA

Re: REPLACE 0 with space

Post by Jay Freedman »

Another fixed-width font that might be suitable is Lucida Sans Typewriter.
Image

BenCasey
4StarLounger
Posts: 495
Joined: 13 Sep 2013, 07:56

Re: REPLACE 0 with space

Post by BenCasey »

Hi,
Using the Replace function can substitute 1 or more characters with 1 or more others.
eg:-

Code: Select all

MyVar = Replace(MyVar,"0"," ") ' replace 0 with single space
As for lining up correctly, see Hans' suggestions.

NB. This function will replace all 0's not just the leading ones.
Regards, Ben

"Science is the belief in the ignorance of the experts."
- Richard Feynman