如何使用vba删除引号?

时间:2015-05-25 09:51:46

标签: excel vba excel-vba

我有:

nuid="!,@,a-z"

但我不想要双引号。我想要nuid =!,@,a-z

建议我删除开始和结束引号的方法

这是我的代码:

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

从我的gui,我将输入将存储在变量nuid中的特殊字符。我只想要特殊字符而不是它周围的引号

3 个答案:

答案 0 :(得分:8)

你也可以尝试:

nuid = Replace(nuid, Chr(34), vbNullString)

但如果引号不是第一个字符或最后一个字符,则可能会出现问题,例如:"!,@,"a-z"

在这种情况下,您可以尝试:

nuid = Mid(nuid, 2, Len(nuid) - 1)这将删除第一个和最后一个字符

编辑: 在我看来,您看到的引号表示变量字符串的类型。

enter image description here

Edit2 - 观看窗口

enter image description here

结果:

enter image description here

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

答案 1 :(得分:0)

基本上使用"

转义""

下面应该有帮助

nuid = replace (nuid, """", "")

Code Output

答案 2 :(得分:0)

其他变种

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

输出

enter image description here

但是如果你需要找到,例如包含任何小写[a-z]的char的单元格,那么应该使用另一个aproach