问题出在实际上的拆分中。在输入中,新行也应用作分隔符。因此,考虑将输入内容更改为如下所示:
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
,
在按钮宏代码中循环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