EXCEL制作单位自动换算,期盼高手解答!
例如:米,分米,厘米,毫米的换算。当在米下输入1时自动在分米厘米毫米下出现相应的值,当在分米下输入1时自动在米厘米毫米下出现相应的值(必须在一个表中,就像网页上那种单位换...
例如:米,分米,厘米,毫米 的换算。当在米下输入1时自动在 分米 厘米 毫米下出现相应的值,当在分米下输入1时自动在 米 厘米 毫米下出现相应的值(必须在一个表中,就像网页上那种单位换算,不管输入哪个都会自动换算),希望高手能详细讲解,问题解决另追加高分!谢谢!
展开
2个回答
展开全部
'假设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
'说明:输入数字敲回车键或者点击鼠标后,光标所停放的单元格应该在当前列中
'按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
'说明:输入数字敲回车键或者点击鼠标后,光标所停放的单元格应该在当前列中
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询