【问题标题】:Why is my code ignoring underscores?为什么我的代码忽略下划线?
【发布时间】:2014-10-05 18:18:40
【问题描述】:

我正在编写一个非常简单的 vba 代码,旨在遍历第 2 行中每个单元格中的字母/数字,并且只保留开头的数字。每个单元格以 4 到 7 个数字开头,然后是一个字母(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
    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

示例:原始单元格包含这四种类型的信息(数字不同):

Input            Desired output    Actual output  Actual output OK?
----------------|-----------------|--------------|-------------------------
12345_v2.jpg     12345             12345_         No, "_" should be removed
293847.psd       293847            293847         OK
82364382.1.tga   82364382          82364382       OK  
172982C.5.tga    172982            172982C        No, "C" should be removed

【问题讨论】:

  • 向我们展示几个说明性示例:输入、期望输出和当前代码的实际输出。
  • 是不是因为你忘了重置counter
  • 我不相信你。我已经运行了你的代码,第二个和第三个中的句点像下划线和字母一样进入结果。您不会注意到它,因为您将结果输出到单元格中,并且 Excel 会为您删除尾随小数点。在您的Left 中使用counter - 1。还将内部的While 替换为For
  • 我确实忘记重置计数器,将其调整为 counter-1 解决了这个问题。谢谢。

标签: string excel vba loops


【解决方案1】:

所以我发现你的代码有两个问题。当您设置新文本时,第一个计数器确实需要为 counter-1,因为那是非数字或下划线字符的位置。在柜台点复制会给你一个额外的角色。

第二个问题是你需要在内部 Do 循环之外重置 counter 变量,否则你将从上一个最后找到的字符的位置开始。试试这个。

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

【讨论】:

    【解决方案2】:

    这将删除点和下划线。

    我必须跑;没有时间实施删除字母。但这应该让你走上正确的轨道。我明天可能会继续。

    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
    

    【讨论】:

      猜你喜欢
      • 2018-09-07
      • 1970-01-01
      • 1970-01-01
      • 2021-12-14
      • 2021-01-12
      • 1970-01-01
      • 2018-10-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多