在字符串数组中查找字符串的索引

johndoe253

在此处输入图片说明在此处输入图片说明在此处输入图片说明

该程序将字符串从数组删除到新的工作表。我找到了字符串“ hello”或“ bye”,但我也希望索引中的字符串位于每个字符串之前。'hello'或'bye'之前的字符串并不总是相同的,那么我该如何使用Index()函数呢?

Sub SplitWithFormat()
    Dim R As Range, C As Range
    Dim i As Long, V As Variant
    Dim varHorizArray As Variant
    Dim rge As Range
    Dim intCol As Integer
   
Set R = Range("d1", Cells(Rows.Count, "d").End(xlUp))
For Each C In R
    With C
        .TextToColumns Destination:=.Range("AD1"), DataType:=xlDelimited, _
        consecutivedelimiter:=True, Tab:=False, semicolon:=True, comma:=False, _
        Space:=True, other:=True, Otherchar:=vbLf

        Set rge = Selection
        varHorizArray = rge
        .Copy
        Range(.Range("AD1"), Cells(.Row, Columns.Count).End(xlToLeft)).PasteSpecial xlPasteFormats
    End With
Next C

Application.CutCopyMode = False

    For intCol = LBound(varHorizArray, 2) To UBound(varHorizArray, 2)
       Debug.Print varHorizArray(1, intCol)
    Next intCol
    
       
    With Application
        .ScreenUpdating = False
        .EnableEvents = False
    End With
    varHorizArray = Array("bye","hello")
    Set NewSh = Worksheets.Add

    With Sheets("Sheet2").Range("AD1:AZ100")

        Rcount = 0

        For i = LBound(varHorizArray) To UBound(varHorizArray)

            
            Set Rng = .find(What:=varHorizArray(i), _
                            After:=.Cells(.Cells.Count), _
                            LookIn:=xlFormulas, _
                            LookAt:=xlPart, _
                            SearchOrder:=xlByRows, _
                            SearchDirection:=xlNext, _
                            MatchCase:=False)
            If Not Rng Is Nothing Then
                FirstAddress = Rng.Address
                Do
                    Rcount = Rcount + 1

                    Rng.Copy NewSh.Range("A" & Rcount)

                    
                    NewSh.Range("A" & Rcount).Value = Rng.Value

                    Set Rng = .FindNext(Rng)
                Loop While Not Rng Is Nothing And Rng.Address <> FirstAddress
            End If
        Next i
    End With

    With Application
        .ScreenUpdating = True
        .EnableEvents = True
    End With

End Sub

用户4039065

尽管通常使用InStr函数在字符串中定位子字符串,但使用Split函数可能会更好地处理多个搜索词

Option Explicit

Sub stripName()
    Dim rw As Long

    With ActiveSheet
        For rw = 1 To .Cells(Rows.Count, "D").End(xlUp).Row
            .Cells(rw, "A") = Split(Split(.Cells(rw, "D").Value2, ", hello")(0), ", bye")(0)
        Next rw
    End With

End Sub

请注意,拆分中使用的搜索词区分大小写。

split_names

经修订的问题的增编:

Option Explicit

Sub stripName()
    Dim rw As Long, s As String

    With ActiveSheet
        For rw = 1 To .Cells(Rows.Count, "D").End(xlUp).Row
            s = Split(.Cells(rw, "D").Value2, ", bye")(0)
            s = Split(s, ", hello")(0)
            s = Split(Chr(32) & s, Chr(32))(UBound(Split(Chr(32) & s, Chr(32))))
            .Cells(rw, "A") = s
        Next rw
    End With

End Sub

split_names2

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

在字符串中查找字符的索引

来自分类Dev

在字符串中查找字符的索引

来自分类Dev

查找字符串中字符的索引位置

来自分类Dev

递归查找字符串的索引

来自分类Dev

递归查找字符串的索引

来自分类Dev

在字符串数组中查找字符的频率

来自分类Dev

如何从数组中查找字符串的索引(前三个字符)

来自分类Dev

在列表中查找字符串的索引

来自分类Dev

在列表中查找字符串的索引

来自分类Dev

查找字符串中模式的所有索引?

来自分类Dev

在Python中查找字符串+整数的索引

来自分类Dev

在 .txt 文件中查找字符串索引

来自分类Dev

按索引查找字符串中的单词

来自分类Dev

在包含值的字符串数组中查找字符串的最后一个索引

来自分类Dev

从文件中查找字符串和字符串的索引到列表中

来自分类Dev

Java:在字符串中查找字符串

来自分类Dev

查找字符串在字符串中的位置

来自分类Dev

Python:在字符串中查找字符串

来自分类Dev

在字符串中查找字符串

来自分类Dev

在字符串中查找字符串的实例

来自分类Dev

在字符串中查找字符串的出现

来自分类Dev

javascript在字符串中查找字符串

来自分类Dev

在多维php数组中查找字符串(键)

来自分类Dev

在目录列表数组中查找字符串

来自分类Dev

在数组中查找字符串

来自分类Dev

在数组中查找字符串-C编程

来自分类Dev

查找字符串中数组值的首次出现

来自分类Dev

在PHP中查找字符串

来自分类Dev

在文件中查找字符串