![](/img/trans.png)
[英]How can I export from Excel file to a csv file, where the file has leading zeros, in text format?
[英]How to remove leading zeros from alphanumeric text in excel
我在 Excel 中的单元格列之一如下所示:
00071331
000062KV
00008M01
00009R22
001N5350
12345678
00123456
我想删除所有前导零。 输出应如下所示:
71331
62KV
8M01
9R22
1N5350
12345678
123456
我曾尝试在 excel 中使用 Flash 填充,但它没有选择模式。
因为您标记了VBA ,请尝试:
Sub NoZero()
Dim r As Range, v As String
For Each r In Range("A:A")
v = r.Text
If v = "" Then Exit Sub
While Left(v, 1) = "0"
v = Mid(v, 2, Len(v))
Wend
r.Value = v
Next r
End Sub
这将执行“就地”转换,而无需“帮助列”。
我认为在你的情况下你可以使用:
=MID(A1,FIND(LEFT(SUBSTITUTE(A1,"0",""),1),A1),8)
或数组公式,如:
=MID(A1,MATCH(TRUE,MID(A1,ROW($1:$8),1)<>"0",0),8)
或者,如果您不想将其作为数组公式输入:
=MID(A1,MATCH(TRUE,INDEX(MID(A1,ROW($1:$8),1)<>"0",),0),8)
与@Gary 的学生相同,但有细微的变化:不使用 A 列,而是使用当前选择; 在 0000 形式的字符串中,并非所有的零都被删除,而只是前导的零等。
Sub NoZeroAnywhere()
Dim r As Range, v As String
Rem Work with all selected cells
If TypeName(Selection) <> "Range" Then Exit Sub
For Each r In Selection.Cells
v = r.Text
If v <> "" Then ' Don't break loop on empty cell
Rem Condition Len(v)>1 prevent remove all zeros, 0000 will be 0, not empty string
While Len(v) > 1 And Left(v, 1) = "0"
v = Right(v, Len(v) - 1)
Wend
r.Value = v
End If
Next r
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.