[attach]5290[/attach]
先寫一個輸入完就自行帶出
依您設定的cells(4,10)的位置撰寫
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Count > 1 Then Exit Sub
Select Case Target.Column
Case 10
If Target = "" Or Target.Offset(0, 1) = "" Or Target.Offset(0, 2) = "" Then Exit Sub
If Target = "U2" Then 'U2=0 ~ 65535
If Target.Offset(0, 1) >= 0 And Target.Offset(0, 1) <= 65536 Then '判斷最小值
Min1 = Target.Offset(0, 1)
Else
Min1 = 0
End If
If Target.Offset(0, 2) >= 0 And Target.Offset(0, 2) <= 65536 Then '判斷最大值
Max1 = Target.Offset(0, 2)
Else
Max1 = 65535
End If
Target.Offset(0, 5) = "0 ~ 65535"
ElseIf Target = "U1" Then 'U1=0 ~ 255
If Target.Offset(0, 1) >= 0 And Target.Offset(0, 1) <= 255 Then '判斷最小值
Min1 = Target.Offset(0, 1)
Else
Min1 = 0
End If
If Target.Offset(0, 2) >= 0 And Target.Offset(0, 2) <= 255 Then '判斷最大值
Max1 = Target.Offset(0, 2)
Else
Max1 = 255
End If
Target.Offset(0, 5) = "0 ~ 255"
ElseIf Target = "S1" Then 'S1=-127 ~ +127
If Target.Offset(0, 1) >= -127 And Target.Offset(0, 1) <= 127 Then '判斷最小值
Min1 = Target.Offset(0, 1)
Else
Min1 = -127
End If
If Target.Offset(0, 2) >= -127 And Target.Offset(0, 2) <= 127 Then '判斷最大值
Max1 = Target.Offset(0, 2)
Else
Max1 = 127
End If
Target.Offset(0, 5) = "-127 ~ +127"
Else 'S2=-32767 ~ +32767
If Target.Offset(0, 1) >= -32767 And Target.Offset(0, 1) <= 32767 Then '判斷最小值
Min1 = Target.Offset(0, 1)
Else
Min1 = -32767
End If
If Target.Offset(0, 2) >= -32767 And Target.Offset(0, 2) <= 32767 Then '判斷最大值
Max1 = Target.Offset(0, 2)
Else
Max1 = 32767
End If
Target.Offset(0, 5) = "'-32767 ~ +32767"
End If
Target.Offset(0, 3) = Min1 & "~" & Max1
Case 11
If Target = "" Or Target.Offset(0, -1) = "" Or Target.Offset(0, 1) = "" Then Exit Sub
If Target.Offset(0, -1) = "U2" Then 'U2=0 ~ 65535
If Target >= 0 And Target <= 65535 Then '判斷最小值
Min1 = Target
Else
Min1 = 0
End If
If Target.Offset(0, 1) >= 0 And Target.Offset(0, 1) <= 65535 Then '判斷最大值
Max1 = Target.Offset(0, 1)
Else
Max1 = 65535
End If
Target.Offset(0, 4) = "0 ~ 65535"
ElseIf Target.Offset(0, -1) = "U1" Then 'U1=0 ~ 255
If Target >= 0 And Target <= 255 Then '判斷最小值
Min1 = Target
Else
Min1 = 0
End If
If Target.Offset(0, 1) >= 0 And Target.Offset(0, 1) <= 255 Then '判斷最大值
Max1 = Target.Offset(0, 1)
Else
Max1 = 255
End If
Target.Offset(0, 4) = "0 ~ 255"
ElseIf Target.Offset(0, -1) = "S1" Then 'S1=-127 ~ +127
If Target >= -127 And Target <= 127 Then '判斷最小值
Min1 = Target
Else
Min1 = -127
End If
If Target.Offset(0, 1) >= -127 And Target.Offset(0, 1) <= 127 Then '判斷最大值
Max1 = Target.Offset(0, 1)
Else
Max1 = 127
End If
Target.Offset(0, 4) = "-127 ~ +127"
Else 'S2=-32767 ~ +32767
If Target >= -32767 And Target <= 32767 Then '判斷最小值
Min1 = Target
Else
Min1 = -32767
End If
If Target.Offset(0, 1) >= -32767 And Target.Offset(0, 1) <= 32767 Then '判斷最大值
Max1 = Target.Offset(0, 1)
Else
Max1 = 32767
End If
Target.Offset(0, 4) = "-32767 ~ +32767"
End If
Target.Offset(0, 2) = Min1 & "~" & Max1
Case 12
If Target = "" Or Target.Offset(0, -1) = "" Or Target.Offset(0, -2) = "" Then Exit Sub
If Target.Offset(0, -2) = "U2" Then 'U2=0 ~ 65535
If Target.Offset(0, -1) >= 0 And Target.Offset(0, -1) <= 65535 Then '判斷最小值
Min1 = Target.Offset(0, -1)
Else
Min1 = 0
End If
If Target >= 0 And Target <= 65535 Then '判斷最大值
Max1 = Target
Else
Max1 = 65535
End If
Target.Offset(0, 3) = "0 ~ 65535"
ElseIf Target.Offset(0, -2) = "U1" Then 'U1=0 ~ 255
If Target.Offset(0, -1) >= 0 And Target.Offset(0, -1) <= 255 Then '判斷最小值
Min1 = Target.Offset(0, -1)
Else
Min1 = 0
End If
If Target >= 0 And Target <= 255 Then '判斷最大值
Max1 = Target
Else
Max1 = 255
End If
Target.Offset(0, 3) = "0 ~ 255"
ElseIf Target.Offset(0, -2) = "S1" Then 'S1=-127 ~ +127
If Target.Offset(0, -1) >= -127 And Target.Offset(0, -1) <= 127 Then '判斷最小值
Min1 = Target.Offset(0, -1)
Else
Min1 = -127
End If
If Target >= -127 And Target <= 127 Then '判斷最大值
Max1 = Target
Else
Max1 = 127
End If
Target.Offset(0, 3) = "-127 ~ +127"
Else 'S2=-32767 ~ +32767
If Target.Offset(0, -1) >= -32767 And Target.Offset(0, -1) <= 32767 Then '判斷最小值
Min1 = Target.Offset(0, -1)
Else
Min1 = -32767
End If
If Target >= -32767 And Target <= 32767 Then '判斷最大值
Max1 = Target
Else
Max1 = 32767
End If
Target.Offset(0, 3) = "-32767 ~ +32767"
End If
Target.Offset(0, 1) = Min1 & "~" & Max1
End Select
End Sub作者: a703130 時間: 2011-4-20 18:36
多謝大大,讓我對這方面有新的見解
我有試另外一個方法可以減少code size
程式寫法大概像下面這樣:利用2^X 來判斷
If Mid(Cells(4, 10), 1, 1) = "U" Then
Select Case Mid(Cells(4, 10), 2, 1)
Case Is = 1
bit = 8
Case Is = 2
bit = 16
Case Is = 3
bit = 24
Case Is = 4
bit = 32
End Select
max1 = 2 ^ bit - 1
min1 = 0
Cells(4, 17).Value = min1 & "~" & max1
If Cells(4, 15) > min1 Then
min1 = Cells(4, 15)
End If
If Cells(4, 16) < max1 Then
max1 = Cells(4, 16)
End If
Cells(4, 18).Value = min1 & "~" & max1
ElseIf Mid(Cells(4, 10), 1, 1) = "S" Then
Select Case Mid(Cells(4, 10), 2, 1)
Case Is = 1
bit = 7
Case Is = 2
bit = 15
Case Is = 3
bit = 23
Case Is = 4
bit = 31
End Select
max1 = 2 ^ bit - 1
min1 = 1 - 2 ^ bit
Cells(4, 17).Value = min1 & "~" & max1
If Cells(4, 15) > min1 Then
min1 = Cells(4, 15)
End If
If Cells(4, 16) < max1 Then
max1 = Cells(4, 16)
End If
Cells(4, 18).Value = min1 & "~" & max1
End If
Next作者: hugh0620 時間: 2011-4-20 18:53