2015-05-25 34 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]任何字符的細胞