Test for space

30percent

Board Regular
Joined
May 5, 2011
Messages
123
Office Version
  1. 2016
Platform
  1. Windows
Hi,

I have the following line of VBA code.
char_first_3 = Mid(ThisWorkbook.Worksheets("test").Range("A" & j + 1), var_1 - 3, 1)

the value of variable char_first_3 is " ".

However when I test for it in the following lines of code:

ElseIf char_first_3 = " " Then
Debug.Print "char_first_3 is a space"

The test char_first_3 = " " came out as false. What am I missing?
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Either your code branches before the ElseIf char_first_3 = " " Then statement, or else despite your assumption, char_first_3 is not a space character. Add this debug code to see.
VBA Code:
    char_first_3 = Mid(ThisWorkbook.Worksheets("test").Range("A" & j + 1), var_1 - 3, 1)
    'For debug
    Dim Msg As String
    Msg = "Char = '" & char_first_3 & "'" & vbCr
    Msg = Msg & "Ascii Char Code = " & Asc(char_first_3) & vbCr
    Msg = Msg & "Char Length = " & Len(char_first_3)
    MsgBox Msg
    ' end debug
 
Upvote 0
Either your code branches before the ElseIf char_first_3 = " " Then statement, or else despite your assumption, char_first_3 is not a space character. Add this debug code to see.
VBA Code:
    char_first_3 = Mid(ThisWorkbook.Worksheets("test").Range("A" & j + 1), var_1 - 3, 1)
    'For debug
    Dim Msg As String
    Msg = "Char = '" & char_first_3 & "'" & vbCr
    Msg = Msg & "Ascii Char Code = " & Asc(char_first_3) & vbCr
    Msg = Msg & "Char Length = " & Len(char_first_3)
    MsgBox Msg
    ' end debug
Here are the result:
Char = ' '
Ascii Char Code = 160
Char Length = 1

So how do I test for Ascii Char Code 160?

thanks
 
Upvote 0
Ascii 160 is a 'non-breaking space' character and it is NOT a regular space character. One way is just to replace it with a normal space.

VBA Code:
     char_first_3 = Mid(ThisWorkbook.Worksheets("test").Range("A" & j + 1), var_1 - 3, 1)
     char_first_3 = Replace(char_first_3, Chr(160), " ")
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,637
Latest member
Ezio2866

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top