简体   繁体   English

VBA修剪留下领先的空白区域

[英]VBA Trim leaving leading white space

I'm trying to compare strings in a macro and the data isn't always entered consistently. 我正在尝试比较宏中的字符串,并且数据并不总是一致地输入。 The difference comes down to the amount of leading white space (ie " test" vs. "test" vs. " test") 差异归结为领先的空白量(即“测试”与“测试”与“测试”的比较)

For my macro the three strings in the example should be equivalent. 对于我的宏,示例中的三个字符串应该是等效的。 However I can't use Replace, as any spaces in the middle of the string (ex. "test one two three") should be retained. 但是我不能使用Replace,因为字符串中间的任何空格(例如“test one two three”)都应该保留。 I had thought that was what Trim was supposed to do (as well as removing all trailing spaces). 我曾经认为这是Trim应该做的(以及删除所有尾随空格)。 But when I use Trim on the strings, I don't see a difference, and I'm definitely left with white space at the front of the string. 但是当我在琴弦上使用Trim时,我看不出有什么不同,而且我肯定在琴弦前面留下了白色空间。

So A) What does Trim really do in VBA? 那么A)Trim在VBA中做了什么? B) Is there a built in function for what I'm trying to do, or will I just need to write a function? B)我正在尝试做什么内置功能,或者我只需要编写一个函数?

Thanks! 谢谢!

So as Gary's Student aluded to, the character wasn't 32. It was in fact 160. Now me being the simple man I am, white space is white space. 所以,正如加里的学生所说,这个角色不是32岁。实际上是160岁。现在我是一个简单的人,白色空间就是白色空间。 So in line with that view I created the following function that will remove ALL Unicode characters that don't actual display to the human eye (ie non-special character, non-alphanumeric). 因此,根据该视图,我创建了以下函数,该函数将删除未实际显示给人眼的所有Unicode字符(即非特殊字符,非字母数字)。 That function is below: 该功能如下:

Function TrueTrim(v As String) As String
Dim out As String
Dim bad As String
bad = "||127||129||141||143||144||160||173||" 'Characters that don't output something
       'the human eye can see based on http://www.gtwiki.org/mwiki/?title=VB_Chr_Values

out = v

'Chop off the first character so long as it's white space
If v <> "" Then
    Do While AscW(Left(out, 1)) < 33 Or InStr(1, bad, "||" & AscW(Left(out, 1)) & "||") <> 0 'Left(out, 1) = " " Or Left(out, 1) = Chr(9) Or Left(out, 1) = Chr(160)
        out = Right(out, Len(out) - 1)
    Loop

    'Chop off the last character so long as it's white space
    Do While AscW(Right(out, 1)) < 33 Or InStr(1, bad, "||" & AscW(Right(out, 1)) & "||") <> 0 'Right(out, 1) = " " Or Right(out, 1) = Chr(9) Or Right(out, 1) = Chr(160)
        out = Left(out, Len(out) - 1)
    Loop
End If 'else out = "" and there's no processing to be done

'Capture result for return
TrueTrim = out
End Function

TRIM() will remove all leading spaces TRIM()将删除所有前导空格

Sub demo()
    Dim s As String
    s = "   test   "
    s2 = Trim(s)
    msg = ""
    For i = 1 To Len(s2)
        msg = msg & i & vbTab & Mid(s2, i, 1) & vbCrLf
    Next i
    MsgBox msg
End Sub

It is possible your data has characters that are not visible, but are not spaces either. 您的数据可能包含不可见的字符,但也不是空格。

Without seeing your code it is hard to know, but you could also use the Application.WorksheetFunction.Clean() method in conjunction with the Trim() method which removes non-printable characters. 在没有看到代码的情况下很难知道,但您也可以将Application.WorksheetFunction.Clean()方法与Trim()方法结合使用,该方法可以删除不可打印的字符。

MSDN Reference page for WorksheetFunction.Clean() WorksheetFunction.Clean()的MSDN参考页

Why don't you try using the Instr function instead? 为什么不尝试使用Instr功能呢? Something like this 像这样的东西

Function Comp2Strings(str1 As String, str2 As String) As Boolean
    If InStr(str1, str2) <> 0 Or InStr(str2, str1) <> 0 Then
        Comp2Strings = True
    Else
        Comp2Strings = False
    End If
End Function

Basically you are checking if string1 contains string2 or string2 contains string1. 基本上你正在检查string1是否包含string2或string2是否包含string1。 This will always work, and you dont have to trim the data. 这将始终有效,您无需修剪数据。

VBA's Trim function is limited to dealing with spaces. VBA的Trim功能仅限于处理空间。 It will remove spaces at the start and end of your string. 它将删除字符串开头和结尾的空格。

In order to deal with things like newlines and tabs, I've always imported the Microsoft VBScript RegEx library and used it to replace whitespace characters. 为了处理换行符和标签之类的东西,我总是导入Microsoft VBScript RegEx库并用它来替换空格字符。

In your VBA window, go to Tools, References, the find Microsoft VBScript Regular Expressions 5.5. 在VBA窗口中,转到“工具”,“引用”,“查找Microsoft VBScript正则表达式5.5”。 Check it and hit OK. 检查并点击确定。

Then you can create a fairly simple function to trim all white space, not just spaces. 然后你可以创建一个相当简单的函数来修剪所有的空白区域,而不仅仅是空格。

Private Function TrimEx(stringToClean As String)
    Dim re As New RegExp
    ' Matches any whitespace at start of string
    re.Pattern = "^\s*"
    stringToClean = re.Replace(stringToClean, "")
    ' Matches any whitespace at end of string
    re.Pattern = "\s*$"
    stringToClean = re.Replace(stringToClean, "")
    TrimEx = stringToClean
End Function

Non-printables divide different lines of a Web page. 非printables划分网页的不同行。 I replaced them with X, Y and Z respectively. 我分别用X,Y和Z替换它们。

Debug.Print Trim(Mid("X test ", 2)) ' first place counts as 2 in VBA Debug.Print Trim(Mid(“X test”,2))'第一个位置在VBA中计为2

Debug.Print Trim(Mid("XY test ", 3)) ' second place counts as 3 in VBA Debug.Print Trim(Mid(“XY test”,3))'第二位在VBA中计为3

Debug.Print Trim(Mid("XYZ test ", 2)) ' more rounds needed :) Debug.Print Trim(Mid(“XYZ test”,2))'需要更多轮:)

Programmers prefer large text as may neatly be chopped with built in tools (inSTR, Mid, Left, and others). 程序员更喜欢大文本,因为可以用内置工具(inSTR,Mid,Left和其他)整齐地切割。 Use of text from several children (ie taking .textContent versus .innerText) may result several non-printables to cope with, yet DOM and REGEX are not for beginners. 使用来自几个孩子的文本(即使用.textContent与.innerText)可能会导致几个非printables应对,但DOM和REGEX不适合初学者。 Addressing sub-elements for inner text precisely (child elements one-by-one !) may help evading non-printable characters. 精确地处理内部文本的子元素(子元素一个一个!)可以帮助规避不可打印的字符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM