答案1
在这种情况下,仅仅更改格式是不够的,您还需要明确将单元格中的数值转换为文本字符串。例如,像这样:
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
答案2
使用最简单的代码:
Sub SetCellFormat()
For Each oCell In Worksheets("Sheet1").Range("E:E")
oCell.Value = "'" & Format(oCell.Value, "0")
oCell.NumberFormat = "@"
Next oCell
End Sub
或者将两条线合并成一条长线
oCell.Value = "'" & Format(oCell.Value, "0")
oCell.NumberFormat = "@"
用于格式化 E 列单元格的最短 vba 代码:
Sub SetCellFormat()
For Each oCell In Worksheets("Sheet1").Range("E:E")
oCell.Value = Format("'" & Format(oCell.Value, "0"), "@")
Next oCell
End Sub