Sub convertNumbersToText()
Dim oCell As Range, nonEmptyCells As Range
With Worksheets("Sheet1")
Set nonEmptyCells = Application.Intersect(.UsedRange, _
.Range("E:E")).SpecialCells(xlCellTypeConstants)
For Each oCell In nonEmptyCells.Cells
oCell.Value2 = "'" & Format(oCell.Value, "0")
Next oCell
.Range("E:E").NumberFormat = "@"
End With
End Sub
Sub SetCellFormat()
For Each oCell In Worksheets("Sheet1").Range("E:E")
oCell.Value = "'" & Format(oCell.Value, "0")
oCell.NumberFormat = "@"
Next oCell
End Sub
在这种情况下,仅更改格式是不够的,您还需要将单元格中的数值显式转换为文本字符串。例如,像这样:
用最简单的代码:
或者将两条线与一条长线组合起来
格式化 E 列单元格的最短 vba 代码: