搜尋文字放在Range("B2")
Sub 搜尋複製()
Dim FindTxt$, xA As Range, PH$, F, xRR As Range
Dim xB As Workbook, xS As Worksheet, xU As Range, N&
FindTxt = [B2]
If FindTxt = "" Then MsgBox "搜尋文字未輸入! ": Exit Sub
Call 清除: Set xA = [A20]
PH = ThisWorkbook.Path '路徑>>自行改為C:\Temp
Application.ScreenUpdating = False
For Each F In Array("A", "B", "C", "D")
Set xRR = Nothing: N = 1
If Dir(PH & "\" & F & ".csv") = "" Then GoTo 101
Set xB = Workbooks.Open(PH & "\" & F & ".csv")
Set xS = xB.Sheets(1): Set xU = xS.UsedRange
Set xRR = xU.Rows(1)
For i = 2 To xU.Rows.Count
If InStr(xU.Cells(i, 1), FindTxt) > 0 Then
N = N + 1: Set xRR = Union(xRR, xU.Rows(i))
End If
Next i
xA = F & " File Data": xA.Resize(1, 4).Interior.ColorIndex = 4
xRR.Copy xA(2): Set xA = xA(N + 3)
xB.Close 0
101: Next
End Sub
Sub 搜尋複製()
Dim FindTxt$, xA(4) As Range, PH$, F, xRR As Range
Dim xB As Workbook, xS As Worksheet, xU As Range, N&
FindTxt = [B2]
If FindTxt = "" Then MsgBox "搜尋文字未輸入! ": Exit Sub
Call 清除
PH = ThisWorkbook.Path
For i = 1 To 4
Set xA(i) = Range("A" & Array(20, 35, 50, 65)(i - 1))
Next i
Application.ScreenUpdating = False
For Each F In Array("A", "B", "C", "D")
Set xRR = Nothing
If Dir(PH & "\" & F & ".csv") = "" Then GoTo 101
Set xB = Workbooks.Open(PH & "\" & F & ".csv")
Set xS = xB.Sheets(1): Set xU = xS.UsedRange
Set xRR = xU.Rows(1)
For i = 2 To xU.Rows.Count
If InStr(xU.Cells(i, 1), FindTxt) > 0 Then Set xRR = Union(xRR, xU.Rows(i))
Next i
N = N + 1
xA(N) = F & " File Data": xA(N).Resize(1, 4).Interior.ColorIndex = 4
xRR.Copy xA(N)(2)
xB.Close 0
101: Next
End Sub作者: rcyw 時間: 2019-5-5 15:15
Sub 搜尋複製()
Dim FindTxt$, xA(4) As Range, PH$, F, xRR As Range
Dim xB As Workbook, xS As Worksheet, xU As Range, N&, V&
FindTxt = [B2]
If FindTxt = "" Then MsgBox "搜尋文字未輸入! ": Exit Sub
Call 清除
PH = ThisWorkbook.Path
For i = 1 To 4
Set xA(i) = Range("A" & Array(20, 35, 50, 65)(i - 1))
Next i
Application.ScreenUpdating = False
For Each F In Array("A", "B", "C", "D")
Set xRR = Nothing
If Dir(PH & "\" & F & ".csv") = "" Then GoTo 101
Set xB = Workbooks.Open(PH & "\" & F & ".csv")
Set xS = xB.Sheets(1): Set xU = xS.UsedRange
Set xRR = xU.Rows(1): V = 0
For i = 2 To xU.Rows.Count
If InStr(xU.Cells(i, 1), FindTxt) > 0 Then V = V + 1: Set xRR = Union(xRR, xU.Rows(i))
Next i
N = N + 1
xA(N) = F & " File Data": xA(N).Resize(1, 4).Interior.ColorIndex = 4
xRR.Copy xA(N)(2)
With xA(N)(3).Resize(V, xU.Columns.Count)
.Sort Key1:=.Item(1), Order1:=xlAscending, Header:=xlNo
End With
xB.Close 0
101: Next
End Sub