按条件拆分单元格

我需要在VBA excel中进行复杂的拆分。我想将每个以“ C:”开头,包括“ OCAK”并以“ .JPG”结尾的范围(“ C1”)结尾的片段分割为A1,A2,A3...。

从这

按条件拆分单元格

对此

按条件拆分单元格

我仍在进行研究和测试,但找不到真正可行的解决方案。任何想法将不胜感激。

Private Sub buton_Click()

If Cells(1,"c").Text Like "C:*OCAK*.jpg*" Then
   Dim jpgStart As Long
   jpgStart = InStr(Cells(1,"c").Text,".jpg")
   Dim result As String
   result = Left(Cells(1,jpgStart - 1)
   Cells(1,"c").Offset(0,-2).Value = result
Else
   Cells(1,-2).Value = vbNullString
End If
End Sub
lvcha108 回答:按条件拆分单元格

问题出在实际上的拆分中。在输入中,新行也应用作分隔符。因此,考虑将输入内容更改为如下所示:

readCell = Worksheets(1).Cells(1,"C")

readCell = Replace(readCell,Chr(13) & Chr(10)," ")
readCell = Replace(readCell,vbCrLf,vbNewLine,vbLf," ")

输入固定后,就可以使用单位myArray = Split(readCell)构建一个数组。遍历数组并使用Like "C:*OCAK*.jpg"效果很好:

Public Sub TestMe()

    Dim readCell As String
    readCell = Worksheets(1).Cells(1,"C")

    readCell = Replace(readCell," ")
    readCell = Replace(readCell," ")

    Dim myArray As Variant
    myArray = Split(readCell)

    Dim myVar As Variant
    Dim currentRow As Long: currentRow = 1

    For Each myVar In myArray
        If myVar Like "C:*OCAK*.jpg" Then
            Worksheets(1).Cells(currentRow,"A") = myVar
            currentRow = currentRow + 1
        End If
    Next

End Sub

enter image description here

,

在按钮宏代码中循环c列中的单元格;我必须假设您知道如何进行设置。然后针对该范围内的每个cell

with thisworkbook.worksheets("theNameOfYourSheet")
   dim loopRange As Range
   set loopRange=.Range(.Cells(1,3),.Cells(.UsedRange.Rows.Count,3))
end with
dim cell as Range

for each cell in loopRange
    If cell.text Like "C:*.jpg*" Then
       Dim jpgStart As Long
       jpgStart = Instr(cell.text,".jpg")
       Dim result As String
       result= Left(cell.text,jpgStart-1)
       cell.offset(0,-1).Value=result
    Else
       cell.offset(0,-1).Value = vbNullString
    End If
Next
,
  • vbLf

  • 分隔
  • 由空格字符分隔

  • 根据您的结果进行测试

代码:

Option Explicit
Sub GetOcak()

    Dim arr As Variant

    arr = Split(Cells(1,3).Value,vbLf)

    Dim i As Long
    Dim j As Long

    j = 1

    For i = 0 To UBound(arr)
        If Left(Split(arr(i)," ")(0),7) = "C:\OCAK" And _
           Right(Split(arr(i),4) = ".jpg" Then
            Cells(j,1).Value = Split(arr(i)," ")(0)
            j = j + 1
        End If
    Next i

End Sub
本文链接:https://www.f2er.com/3156951.html

大家都在问