下面的代码从F列中获取字符串并提取并将其转换为G列中的数字。我有一个字符串'J-60'(F列),它当前没有转换。代码应该能够在( - )之前提取任何字符串,但如果它(J - ),那么它应该在破折号后提取字符串。
If InStr(Dn, "-") Then
Dn.Offset(, 1) = Split(Dn, "-")(0)
Else
Select Case UCase(Left(Dn, 1))
Case "J"
If UCase(Left(Dn, 2)) = "J+" Then
Dn.Offset(, 1) = Num
ElseIf UCase(Left(Dn, 2)) = "J-" Then
Dn.Offset(, 1) = Num
End If
End Select
请告知我可能出错的地方。非常感谢。
答案 0 :(得分:1)
您的陈述If InStr(Dn, "-") Then
为时尚早
你需要更好的缩进和类似的东西:
If UCase(Left(Dn, 2)) = "J+" Then
Dn.Offset(, 1) = Num
ElseIf UCase(Left(Dn, 2)) = "J-" Then
Dn.Offset(, 1) = Num
ElseIf InStr(Dn, "-") Then
Dn.Offset(, 1) = Split(Dn, "-")(0)
End If
编辑:处理7889的代码
Dim sCase1$, vSplit As Variant
sCase1 = ""
vSplit = Split(Dn, "-")
If UBound(vSplit) = 1 Then
If IsNumeric(vSplit(0)) And IsNumeric(vSplit(1)) Then
sCase1 = vSplit(0) & vSplit(1)
End If
End If
If sCase1 <> "" Then
Dn.Offset(, 1) = sCase1
ElseIf UCase(Left(Dn, 2)) = "J+" Then
Dn.Offset(, 1) = Num
ElseIf UCase(Left(Dn, 2)) = "J-" Then
Dn.Offset(, 1) = Num
ElseIf InStr(Dn, "-") Then
Dn.Offset(, 1) = Split(Dn, "-")(0)
End If
答案 1 :(得分:0)
感谢@dcromley提供的解决方案,我设法使用您的代码实现以下逻辑,这可以解决问题:
s = UCase(Left(Dn, 2))
If s = "J+" Or s = "J-" Then
s = Right(Dn, Len(Dn) - 2)
If IsNumeric(s) Then
Dn.Offset(, 1) = Int(s)
End If
ElseIf InStr(Dn, "-") Then
Dn.Offset(, 1) = Split(Dn, "-")(0)
End If
由于