Function 提取3(选择区域数据第1列左右幅 As Range, 数据图K公里列 As Range, Optional 左右幅1左0右 As Integer = 1)
'Sub DD()
Dim rn As Range, rns As Range
'Set rns = Range("b11:b17")
'数据图K公里列 = "K34"
'左右幅1左0右 = 1
If 左右幅1左0右 = 1 Then
ZF = "左幅"
Else
ZF = "右幅"
End If
For Each rn In rns
'MsgBox rn.Offset(0, 0) & "|" & rn.Offset(0, 1) & "|" & rn.Offset(0, 2) & "|" & rn.Offset(0, 3) & "|" & rn.Offset(0, 4)
If rn.Offset(0, 0) = ZF And rn.Offset(0, 1) = 数据图K公里列 And rn.Offset(0, 3) = 数据图K公里列 Then
提取3 = rn.Offset(0, 4) - rn.Offset(0, 2)
Exit For
ElseIf rn.Offset(0, 0) = ZF And 数据图K公里列 = rn.Offset(0, 1) And 数据图K公里列 <> rn.Offset(0, 3) Then
提取3 = 1000 - rn.Offset(0, 2)
Exit For
ElseIf rn.Offset(0, 0) = ZF And 数据图K公里列 = rn.Offset(0, 3) Then
提取3 = rn.Offset(0, 4)
Exit For
ElseIf rn.Offset(0, 0) <> ZF And 数据图K公里列 <> rn.Offset(0, 1) Then
提取3 = ""
Exit For
End If
Next
End Function
'End Function
为什么用函数方式运行执行到循环处就中断退出了,用模块手动方式可正常执行。
'Sub DD()
Dim rn As Range, rns As Range
'Set rns = Range("b11:b17")
'数据图K公里列 = "K34"
'左右幅1左0右 = 1
If 左右幅1左0右 = 1 Then
ZF = "左幅"
Else
ZF = "右幅"
End If
For Each rn In rns
'MsgBox rn.Offset(0, 0) & "|" & rn.Offset(0, 1) & "|" & rn.Offset(0, 2) & "|" & rn.Offset(0, 3) & "|" & rn.Offset(0, 4)
If rn.Offset(0, 0) = ZF And rn.Offset(0, 1) = 数据图K公里列 And rn.Offset(0, 3) = 数据图K公里列 Then
提取3 = rn.Offset(0, 4) - rn.Offset(0, 2)
Exit For
ElseIf rn.Offset(0, 0) = ZF And 数据图K公里列 = rn.Offset(0, 1) And 数据图K公里列 <> rn.Offset(0, 3) Then
提取3 = 1000 - rn.Offset(0, 2)
Exit For
ElseIf rn.Offset(0, 0) = ZF And 数据图K公里列 = rn.Offset(0, 3) Then
提取3 = rn.Offset(0, 4)
Exit For
ElseIf rn.Offset(0, 0) <> ZF And 数据图K公里列 <> rn.Offset(0, 1) Then
提取3 = ""
Exit For
End If
Next
End Function
'End Function
为什么用函数方式运行执行到循环处就中断退出了,用模块手动方式可正常执行。