String 为什么我的代码忽略下划线?
我正在编写一个非常简单的vba代码,该代码旨在遍历第2行每个单元格中的字母/数字,并仅将数字保留在开头。每个单元格以4到7个数字开头,然后后跟一个字母(1或更多)、一个String 为什么我的代码忽略下划线?,string,excel,vba,loops,String,Excel,Vba,Loops,我正在编写一个非常简单的vba代码,该代码旨在遍历第2行每个单元格中的字母/数字,并仅将数字保留在开头。每个单元格以4到7个数字开头,然后后跟一个字母(1或更多)、一个和一个数字,或者一个下划线和一个字母和一个数字 我遇到的问题是,我的代码只返回一些单元格的正确值。只有具有的单元格才能正确清洁。带下划线的单元格删除\uu后面的所有内容,但保留\u本身,带字母的单元格保留字母,但删除及其后面的所有内容 这是我的代码: Sub getIDs() Dim counter As Integer coun
和一个数字,或者一个下划线和一个字母和一个数字
我遇到的问题是,我的代码只返回一些单元格的正确值。只有具有
的单元格才能正确清洁。带下划线的单元格删除\uu
后面的所有内容,但保留\u
本身,带字母的单元格保留字母,但删除
及其后面的所有内容
这是我的代码:
Sub getIDs()
Dim counter As Integer
counter = 1
Dim rowCounter As Integer
rowCounter = 2
Dim original As String
original = ""
Dim newText As String
newText = ""
Do While Len(Cells(rowCounter, 2)) > 0
Do While counter <= Len(Cells(rowCounter, 2))
If Not IsNumeric((Mid(Cells(rowCounter, 2).Value, counter, 1))) Or Mid(Cells(rowCounter, 2).Value, counter, 1) = "_" Then
Exit Do
Else
counter = counter + 1
End If
Loop
newText = Left(Cells(rowCounter, 2), counter)
Cells(rowCounter, 2) = newText
rowCounter = rowCounter + 1
Loop
End Sub
这将删除点和下划线 我必须跑;没有时间实施删除字母。但这会让你走上正轨。我明天可以继续
Sub lkjhlkjh()
Dim s As String
Dim iUnderscore As Long
Dim iDot As Long
Dim iCut As Long
s = Sheet1.Cells(1, 1)
iUnderscore = InStr(s, "_")
iDot = InStr(s, ".")
If iUnderscore = 0 Then
If iDot = 0 Then
'Don't cut
Else
iCut = iDot
End If
Else
If iDot = 0 Then
iCut = iUnderscore
Else
If iDot > iUnderscore Then
iCut = iUnderscore
Else
iCut = iDot
End If
End If
End If
If iCut > 0 Then s = Left(s, iCut - 1)
Sheet1.Cells(1, 1) = s
End Sub
所以我发现你的代码有两个问题。设置新文本时,第一个计数器实际上需要是计数器-1,因为这是非数字或下划线字符的位置。在计数器点复制将为您提供一个额外字符 第二个问题是需要在内部Do循环之外重置计数器变量,否则将从上一个找到的字符的位置开始。试试这个
Sub getIDs()
Dim counter As Integer
counter = 1
Dim rowCounter As Integer
rowCounter = 2
Dim original As String
original = ""
Dim newText As String
newText = ""
Do While Len(Cells(rowCounter, 2)) > 0
counter = 1
Do While counter <= Len(Cells(rowCounter, 2))
If Not IsNumeric((Mid(Cells(rowCounter, 2).Value, counter, 1))) Or Mid(Cells(rowCounter, 2).Value, counter, 1) = "_" Then
Exit Do
Else
counter = counter + 1
End If
Loop
newText = Left(Cells(rowCounter, 2), counter - 1)
Cells(rowCounter, 2) = newText
rowCounter = rowCounter + 1
Loop
End Sub
Sub-getIDs()
作为整数的Dim计数器
计数器=1
将行计数器设置为整数
行计数器=2
将原件变暗为字符串
原件=“”
将新文本设置为字符串
newText=“”
当Len(单元格(行计数器,2))大于0时执行
计数器=1
Do While counter请向我们展示几个示例:当前代码的输入、所需输出和实际输出。这是否是您忘记重置计数器的原因?
?我不相信您。我已经运行了你的代码,第二和第三段中的句点会像下划线和字母一样进入结果。您不会注意到它,因为您将结果输出到一个单元格中,Excel会为您删除后面的小数点。在左侧使用计数器-1
。同时将内部While
更换为For
的。我忘了重置计数器,并将其调整为计数器-1解决了问题。非常感谢。
Sub getIDs()
Dim counter As Integer
counter = 1
Dim rowCounter As Integer
rowCounter = 2
Dim original As String
original = ""
Dim newText As String
newText = ""
Do While Len(Cells(rowCounter, 2)) > 0
counter = 1
Do While counter <= Len(Cells(rowCounter, 2))
If Not IsNumeric((Mid(Cells(rowCounter, 2).Value, counter, 1))) Or Mid(Cells(rowCounter, 2).Value, counter, 1) = "_" Then
Exit Do
Else
counter = counter + 1
End If
Loop
newText = Left(Cells(rowCounter, 2), counter - 1)
Cells(rowCounter, 2) = newText
rowCounter = rowCounter + 1
Loop
End Sub