删除

互联网 更新时间:2023-04-26 21:00:34

Sid*_*out 7

A simple formula can solve your requirements

=IF(RIGHT(TRIM(A1),2)="||",LEFT(TRIM(A1),LEN(TRIM(A1))-2),A1)

The above formula is based on the below logic.

Check if the right 2 characters are || If "Yes", then take the left characters (LEN - 2) If "No", then return the string as it is.

如果您仍然需要 VBA,请尝试使用此代码一次性更改整个列。此处给出了有关此方法的说明。

出于演示的目的,我假设数据是在列ASheet1。根据情况更改。

Option Explicit

Sub Sample()
    Dim ws As Worksheet
    Dim lrow As Long
    Dim rng As Range
    Dim sAddr As String
    
    Set ws = Sheet1
    
    With ws
        lrow = .Range("A" & .Rows.Count).End(xlUp).Row
        
        Set rng = .Range("A1:A" & lrow)
        sAddr = rng.Address
        
        rng = Evaluate("index(IF(RIGHT(TRIM(" & sAddr & _
                              "),2)=""||"",LEFT(TRIM(" & sAddr & _
                              "),LEN(TRIM(" & sAddr & _
                              "))-2)," & sAddr & _
                              "),)")
    End With
End Sub

在行动

我只将工作表的名称和范围更改为 L 和 L2:L。– 乌尔奇奥拉·希弗 17 分钟前

bra*_*raX 5

有不同的方法可以做到这一点,但这里是一种:

Function FixPipes(val As String) As String
    Dim v As Variant
    
    v = Split(val, "||")
    If Len(v(UBound(v))) > 0 Then
      FixPipes = val
    Else
      FixPipes = Mid$(val, 1, Len(val) - 2)
    End If
End Function

这是另一种方法:

Function FixPipes(val As String) As String
    If Mid$(val, Len(val) - 1, 2) <> "||" Then
      FixPipes = val
    Else
      FixPipes = Mid$(val, 1, Len(val) - 2)
    End If
End Function

用法:

Sub test()
    Debug.Print FixPipes("Testing||Admin||Moderator||")
End Sub

或者:

Sub LoopIt()
    ' remove this line after verifying the sheet name
    MsgBox ActiveSheet.Name

    Dim lIndex As Long
    Dim lastRow As Long
    lastRow = Range("L" & Rows.Count).End(xlUp).Row
    
    For lIndex = 1 To lastRow
      Range("L" & lIndex) = FixPipes(Range("L" & lIndex))
    Next
End Sub

docs.microsoft./en-us/office/vba/language/reference/user-interface-help/split-function

docs.microsoft./en-us/office/vba/language/reference/user-interface-help/mid-function

更多推荐

本文发布于:2023-04-26 21:00:32,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/hyzx/45c1d3aee2a6c277a597ccebf1f10ec0.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!

  • 89739文章数
  • 23101阅读数
  • 0评论数