'假设A1~D1分别为米、分米、厘米、毫米,需要换算的单元格位置分别是A2、B2、C2、D2
'按Alt+F11打开脚本编辑,点击左侧工作表名称,然后在右侧窗口粘贴如下代码:
Private Sub Worksheet_SelectionChange(ByVal Target As Range): On Error Resume Next
If Target.Count = 1 And Target.Column = 1 And Cells(2, 1) <> 0 Then Cells(2, 2) = Cells(2, 1) * 10: Cells(2, 3) = Cells(2, 1) * 100: Cells(2, 4) = Cells(2, 1) * 1000
If Target.Count = 1 And Target.Column = 2 And Cells(2, 2) <> 0 Then Cells(2, 1) = Cells(2, 2) * 0.1: Cells(2, 3) = Cells(2, 2) * 10: Cells(2, 4) = Cells(2, 2) * 100
If Target.Count = 1 And Target.Column = 3 And Cells(2, 3) <> 0 Then Cells(2, 1) = Cells(2, 3) * 0.01: Cells(2, 2) = Cells(2, 3) * 0.1: Cells(2, 4) = Cells(2, 3) * 10
If Target.Count = 1 And Target.Column = 4 And Cells(2, 4) <> 0 Then Cells(2, 1) = Cells(2, 4) * 0.001: Cells(2, 2) = Cells(2, 4) * 0.01: Cells(2, 3) = Cells(2, 4) * 0.1
If Cells(2, 2) <> "" And Application.IsNumber(Cells(2, 2)) = False Then Range("A2:D2").ClearContents: MsgBox " 你所输入的不是有效数字,请重新输入! "
End Sub
'说明:输入数字敲回车键或者点击鼠标后,光标所停放的单元格应该在当前列中
温馨提示:答案为网友推荐,仅供参考