I am fairly new to VBA and have made the following from combining other users code. The code works and successfully copies rows from one sheet into an index of sheets in another workbook if they contain the target sheet's name. However, it runs very slowly do to looping through multiple activate and select functions. I can't work out how to speed it up and not use activate or select, while still making the code work.
Sub Copy_Rows_Sample()
Dim wb1 As Workbook, wb2 As Workbook
Set wb1 = Workbooks.Open("C:\Sample1.xslx")
Set wb2 = Workbooks.Open("C:\Sample2.xslx")
Dim Page1 As Worksheet
Set Page1 = wb2.Worksheets("Page1")
wb1.Worksheets(1).Activate
Excel.Application.DisplayAlerts = False
Dim wksh As Excel.Worksheet
Dim jIndex As Integer
For jIndex = 1 To wb1.Worksheets.Count
Set wksh = wb1.Worksheets(jIndex)
With wksh
Dim ran As Range
For Each ran In Range("A3")
ran = StrConv(ran.Text, vbProperCase)
Next
Z = Split(Range("A3").Value, ",")(0)
ActiveSheet.name = Z
Dim Y As Variant
Set Y = ActiveSheet
Page1.Activate
Dim xRow&, NextRow&, LastRow&
LastRow = Cells.Find(What:="*", After:=Range("A1"), SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
For xRow = 1 To LastRow
NextRow = Y.Cells.Find(What:="*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row + 2
If WorksheetFunction.CountIf(Rows(xRow), "*" & Z & "*") > 0 Then
Rows(xRow).Copy Y.Rows(NextRow)
NextRow = NextRow + 1
End If
Next xRow
Y.Activate
wb1.Worksheets(ActiveSheet.Index Mod Worksheets.Count + 1).Select
End With
Next jIndex
Excel.Application.DisplayAlerts = True
End Sub