Meliodus123
Meliodus123

Reputation: 57

Stripping Hyphens from Numbers in Excel VBA

I am new to Excel VBA and I need help removing hyphens from an Excel document. I usually use python, and could just use .strip, I was wondering if there is an equivalent in Excel. The cell I will be stripping the hyphens from is static, so the cell it self could be hardcoded I think.

I am trying to go from

233-79-01 to 2337901.

I tried these following lines of code

  Sub striphyphen()

  A5 = replace(A4,'-','')

  End Sub

I am getting a compiling error, and am not sure why.

Any help would be appreciated

Upvotes: 1

Views: 1659

Answers (2)

Michał Turczyn
Michał Turczyn

Reputation: 37460

The problem is that you are wrongly referencing cells. The way you do it is the way to reference variables (just like in any other language).

To reference cell you can use one of the following (A4 cell for example):

Cells(4, 1) ' Cells(row, column)
Range("A4")
[A4]

Upvotes: 2

TourEiffel
TourEiffel

Reputation: 4434

You can use replace as said @BigBen and @JvdV

Sub Test()
Dim myString as String
Dim newString as String

myString = "233-79-01"

newString = replace(myString, "-", "")

MsgBox newString

End Sub

Note That you can also set mystring = Cells(4,1).Value or mystring = Range("A4").Value

Upvotes: 2

Related Questions