区分两种不同的字符串并提取它们
distinguish between two different kinds of string and extract them
所以我在 excel 中有一列注释,其中包含像“01/16 14:38 ATND [来自 Dealer/Distributor] JR[=18 的注释=]”和“01/16 14:14 ATND [来自公司的注释] JR2”和“01/16 14:14 ATND [来自公司的注释]公司] TLO 商品延期交货
如您所见,在括号符号后,有一个两个字母或三个字母的代码,代表三种不同的变体,JR、JR2 和 TLO。我写了一个程序,它只区分 JR 和 TLO,但如果它的编号是 JR 和 JR2,则不会提取代码。如果有人可以帮助我,我将不胜感激。
Sub G_ExtractCodes()
Dim LR As Long
LR = Range("A" & Rows.Count).End(xlUp).Row
Dim i As Long
Dim NoteCodes As Range
For i = LR To 2 Step -1
Set NoteCodes = Range("O" & i)
If InStr(NoteCodes, "JR") >= 1 Then
Cells(i, 20) = "JR"
ElseIf InStr(NoteCodes, "JR2") >= 1 Then
Cells(i, 20) = "JR2"
ElseIf InStr(NoteCodes, "TLO") >= 1 Then
Cells(i, 20) = "TLO"
End If
Next i
End Sub
If
语句中的第一个条件比第一个 ElseIf
子句更具限制性,因此 "JR2" 的任何实例都将被 first 测试(仅查找 "JR",ElseIf
未评估)。
翻转你的逻辑,我认为这应该可以解决它:
If InStr(NoteCodes, "JR2") >= 1 Then
Cells(i, 20) = "JR2"
ElseIf InStr(NoteCodes, "JR") >= 1 Then
Cells(i, 20) = "JR"
ElseIf InStr(NoteCodes, "TLO") >= 1 Then
Cells(i, 20) = "TLO"
End If
或者,您可以解析如下代码:
Dim codeValue as String
Dim bracketLocation as Integer
For i = LR To 2 Step -1
Set NoteCodes = Range("O" & i)
'finds the position of the right bracket
bracketLocation = Instr(NoteCodes, "]")
'Lops off any characters up to & including the right square bracket
codeValue = Trim(Mid(NoteCodes, bracketLocation + 1))
'removes any text that appears *after* the code value, if any
If Instr(codeValue, " ") > 0 Then
codeValue = Left(codeValue, Instr(codeValue, " "))
End If
Cells(i, 20).Value = codeValue
'clear out the variable
codeValue = vbNullString
Next
Sub G_ExtractCodes()
sn=cells(1).currentregion.columns(1).offset(,15)
for j=1 to ubound(sn)
If InStr(sn(j,1), "JR") Then
Cells(j, 20) = iif(instr(sn(j,1),"JR2"),"JR2","JR")
ElseIf InStr(sn(j,1), "TL") Then
Cells(j, 20) = iif(instr(sn(j,1),"TL0"),"TL0","TL")
End If
Next
End Sub
所以我在 excel 中有一列注释,其中包含像“01/16 14:38 ATND [来自 Dealer/Distributor] JR[=18 的注释=]”和“01/16 14:14 ATND [来自公司的注释] JR2”和“01/16 14:14 ATND [来自公司的注释]公司] TLO 商品延期交货
如您所见,在括号符号后,有一个两个字母或三个字母的代码,代表三种不同的变体,JR、JR2 和 TLO。我写了一个程序,它只区分 JR 和 TLO,但如果它的编号是 JR 和 JR2,则不会提取代码。如果有人可以帮助我,我将不胜感激。
Sub G_ExtractCodes()
Dim LR As Long
LR = Range("A" & Rows.Count).End(xlUp).Row
Dim i As Long
Dim NoteCodes As Range
For i = LR To 2 Step -1
Set NoteCodes = Range("O" & i)
If InStr(NoteCodes, "JR") >= 1 Then
Cells(i, 20) = "JR"
ElseIf InStr(NoteCodes, "JR2") >= 1 Then
Cells(i, 20) = "JR2"
ElseIf InStr(NoteCodes, "TLO") >= 1 Then
Cells(i, 20) = "TLO"
End If
Next i
End Sub
If
语句中的第一个条件比第一个 ElseIf
子句更具限制性,因此 "JR2" 的任何实例都将被 first 测试(仅查找 "JR",ElseIf
未评估)。
翻转你的逻辑,我认为这应该可以解决它:
If InStr(NoteCodes, "JR2") >= 1 Then
Cells(i, 20) = "JR2"
ElseIf InStr(NoteCodes, "JR") >= 1 Then
Cells(i, 20) = "JR"
ElseIf InStr(NoteCodes, "TLO") >= 1 Then
Cells(i, 20) = "TLO"
End If
或者,您可以解析如下代码:
Dim codeValue as String
Dim bracketLocation as Integer
For i = LR To 2 Step -1
Set NoteCodes = Range("O" & i)
'finds the position of the right bracket
bracketLocation = Instr(NoteCodes, "]")
'Lops off any characters up to & including the right square bracket
codeValue = Trim(Mid(NoteCodes, bracketLocation + 1))
'removes any text that appears *after* the code value, if any
If Instr(codeValue, " ") > 0 Then
codeValue = Left(codeValue, Instr(codeValue, " "))
End If
Cells(i, 20).Value = codeValue
'clear out the variable
codeValue = vbNullString
Next
Sub G_ExtractCodes()
sn=cells(1).currentregion.columns(1).offset(,15)
for j=1 to ubound(sn)
If InStr(sn(j,1), "JR") Then
Cells(j, 20) = iif(instr(sn(j,1),"JR2"),"JR2","JR")
ElseIf InStr(sn(j,1), "TL") Then
Cells(j, 20) = iif(instr(sn(j,1),"TL0"),"TL0","TL")
End If
Next
End Sub