Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove quotes using vba?

Tags:

excel

vba

I have:

nuid="!,@,a-z"

But I do not want the double quotes. I want nuid=!,@,a-z

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

like image 439
Sagi Avatar asked Apr 26 '26 06:04

Sagi


2 Answers

Also you can try:

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

But you can have problem if quotes not the first nor the last character, for example: "!,@,"a-z".

In that case you can try:

nuid = Mid(nuid, 2, Len(nuid) - 1) This will cut the first and last character

Edit: It seems to me that the quotes that you see indicates the type of a variable string.

enter image description here

Edit2 - watch window

enter image description here

Results:

enter image description here

Edit3 - with 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
like image 127
Dawid Avatar answered Apr 28 '26 20:04

Dawid


Proper Function:

Sub Test()
 Debug.Print RemoveOuterQuotes(Cells(2, 1).Value)
End Sub

Public Function RemoveOuterQuotes(ByVal Str As String) As String
 If Left(Str, 1) = """" Then
  Str = Right(Str, Len(Str) - 1)
 End If
 If Right(Str, 1) = """" Then
  Str = Left(Str, Len(Str) - 1)
 End If
 'Debug.Print Str
 'Stop
 RemoveOuterQuotes = Str
End Function
like image 40
FreeSoftwareServers Avatar answered Apr 28 '26 21:04

FreeSoftwareServers



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!