2015-05-25 35 views
2

我:如何使用vba删除引号?

nuid="!,@,a-z" 

但我不希望双引号。 !我想nuid =,@,AZ

推荐我的方法来去除开始和结束的报价

这里是我的代码:

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 

从我的鬼,我会输入特殊字符,这将将存储在变量nuid.I只想要特殊字符,而不是引号周围

+1

搜索[VBA和替换](https://www.google.se/#q=VBA+replace)。 –

回答

0

基本上是一个"与逃避""

下面应该帮助

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

Code Output

+0

不起作用:/ nuid的值仍然是“!,@,a-z” – Sagi

+1

你能分享一段代码吗? –

+0

编辑问题以添加您的代码示例。 –

6

你也可以试试:

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 - 与子4鹭:

Sub Highlight4Sagi(SpecChar As String) 

Dim Column As Integer 
SpecChar = "[email protected]#" 

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 
+0

我尝试用“nuid = Mid(nuid,2,Len(nuid) - 1”替换“nuid = Replace(nuid,”“”“,”)“,但仍然是nuid =”!,@,az “ – Sagi

+0

@sagi:你可以这样运行这个子类:'highlight(”!,@,az“)'?在我看来,你看到的引号表示一个变量'string'的类型,请看我的回答 – Dawid

+0

@sagi:你可以添加你有问题的数据,并且这个程序不能按预期工作吗? – Dawid

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]任何字符的细胞