Sub rbbtnText2Num(control As IRibbonControl)
Call MRange.Text2Num
End Sub
Sub rbbtnNum2Text(control As IRibbonControl)
Call MRange.Num2Text
End Sub
函数实现:
Sub Text2Num()
Dim selectRng As Range
Dim arr As Variant
'确保选中的是单元格
If TypeName(Selection) = "Range" Then
Set selectRng = Selection
'设置单元格为常规格式
selectRng.NumberFormatLocal = "G/通用格式"
'读取单元格数据,Excel会根据数据的特点自动转换格式
arr = selectRng.Value
selectRng.Value = arr
End If
Set selectRng = Nothing
End Sub
Sub Num2Text()
Dim selectRng As Range
Dim arr() As Variant
Dim i As Long, j As Long
'确保选中的是单元格
If TypeName(Selection) = "Range" Then
Set selectRng = Selection
'设置单元格为文本格式
selectRng.NumberFormatLocal = "@"
If selectRng.Cells.Count > 1 Then
arr = selectRng.Value
For i = 1 To UBound(arr)
For j = 1 To UBound(arr, 2)
'将数据转换为文本
arr(i, j) = VBA.CStr(arr(i, j))
selectRng.Value = arr
selectRng.Value = VBA.CStr(selectRng.Value)
End If
End If
Set selectRng = Nothing
End Sub