vba 如何使用vba删除引号?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/30435548/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to remove quotes using vba?
提问by Sagi
I have:
我有:
nuid="!,@,a-z"
But I do not want the double quotes. I want nuid=!,@,a-z
但我不想要双引号。我想要 nuid=!,@,az
Suggest me ways to remove the start and end quotes
建议我删除开始和结束引号的方法
Here is my code:
这是我的代码:
sub highlight(nuid as string)
dim sh3 as worksheet
Set sh3 = Thisworkbook.Worksheets("Sheet1")
sh3.Select
Cells.Find("User ID").Select
ActiveCell.Offset(1, 0).Select
nuid = Replace(nuid, """", "")
Set rn = sh3.UsedRange
  k = rn.Rows.Count + rn.Row - 1
  For x = 1 To k
 If ActiveCell.Value Like nuid Then
 Selection.Interior.Color = vbYellow
 Else
 Selection.Interior.ColorIndex = xlNone
End If
ActiveCell.Offset(1, 0).Select 'moves activecell down one row.
Next
end sub
From my gui, i will enter special characters which will be stored in the variable nuid.I want only the special characters and not the quotes around it
从我的 gui 中,我将输入将存储在变量 nuid 中的特殊字符。我只想要特殊字符而不是它周围的引号
回答by Dawid
Also you can try:
您也可以尝试:
nuid = Replace(nuid, Chr(34), vbNullString)
nuid = Replace(nuid, Chr(34), vbNullString)
But you can have problem if quotes not the first nor the last character, for example: "!,@,"a-z".
但是,如果引号不是第一个或最后一个字符,则可能会出现问题,例如:"!,@,"a-z".
In that case you can try:
在这种情况下,您可以尝试:
nuid = Mid(nuid, 2, Len(nuid) - 1)This will cut the first and last character
nuid = Mid(nuid, 2, Len(nuid) - 1)这将剪切第一个和最后一个字符
Edit: It seems to me that the quotes that you see indicates the type of a variable string.
编辑:在我看来,您看到的引号表示变量字符串的类型。


Edit2 - watch window
Edit2 - 观察窗口


Results:
结果:


Edit3 - with sub 4 Sagi:
Edit3 - 与 sub 4 Sagi:
Sub Highlight4Sagi(SpecChar As String)
Dim Column As Integer
SpecChar = "!@#"
ThisWorkbook.Worksheets(1).Select
Column = Cells.Find("User ID").Column
LastRow = Cells.Find("*", searchorder:=xlByRows, searchdirection:=xlPrevious).Row
For i = 2 To LastRow 'loop each row in column "User ID"
    For j = 1 To Len(SpecChar) 'loop every specchar: ! and @ and # and find him in each cells
        If InStr(1, Cells(i, Column), Mid(SpecChar, j, 1)) > 0 Then
        Cells(i, Column).Interior.ColorIndex = 6
        Exit For
        Else
        Cells(i, Column).Interior.ColorIndex = 0
        End If
    Next j
Next i
End Sub
回答by S.Krishna
Basically escape a "with ""
基本上逃避"与""
Below should help
下面应该有帮助
nuid = replace (nuid, """", "")




回答by Vasily
additional variant
附加变体
Sub highlight(nuid As String)
    Dim sh3 As Worksheet, Cl&, Lrow&, x&, oCell As Range
    Set sh3 = ThisWorkbook.Worksheets("Sheet1")
    Cl = sh3.Cells.Find("User ID").Column
    Frow = sh3.Cells.Find("User ID").Row + 1
    Lrow = Cells.Find("*", , , , xlByRows, xlPrevious).Row
    For Each oCell In sh3.Range(Cells(Frow, Cl), Cells(Lrow, Cl))
        If oCell.Value <> "" Then
            For x = 1 To Len(nuid)
                If oCell.Value Like "*" & Mid(nuid, x, 1) & "*" Then
                    oCell.Interior.Color = vbYellow
                    Exit For
                Else
                    oCell.Interior.Color = xlNone
                End If
            Next x
        End If
    Next oCell
End Sub
output
输出


but if you need to find, for instance the cells which contain any char in low case  [a-z]then another aproach should be used
但是如果你需要找到,例如在小写中包含任何字符的单元格,  [a-z]那么应该使用另一种方法

