简体   繁体   English

从 Excel VBA 中的数字中去除连字符

[英]Stripping Hyphens from Numbers in Excel VBA

I am new to Excel VBA and I need help removing hyphens from an Excel document.我是 Excel VBA 的新手,我需要帮助从 Excel 文档中删除连字符。 I usually use python, and could just use.strip, I was wondering if there is an equivalent in Excel.我通常使用 python,可以只使用.strip,我想知道 Excel 中是否有等效项。 The cell I will be stripping the hyphens from is static, so the cell it self could be hardcoded I think.我将从中删除连字符的单元格是 static,所以我认为它自己的单元格可以是硬编码的。

I am trying to go from我正在尝试从 go

233-79-01 to 2337901. 233-79-01 至 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任何帮助,将不胜感激

You can use replace as said @BigBen and @JvdV您可以像@BigBen@JvdV所说的那样使用replace

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请注意,您还可以设置mystring = Cells(4,1).Valuemystring = Range("A4").Value

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):要引用单元格,您可以使用以下之一(例如 A4 单元格):

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM