数组VBA代码(数据提取)

当前; y正在基于用户窗体的条件提取数据(行),但下面的代码只能在单个工作表上工作一旦工作表上的button可用,我可以得到下面的代码工作,它写入“从“表如预期,但我不能为我的生活得到正确编写的数组的代码。

我已经尝试了包括这里在内的各种不同的代码,但是我没有足够的能力去debugging这个错误。

有人可以帮我在这里,或指出我在正确的方向吗?

Sub CommandButton1_Click() Dim strsearch As String, lastline As Integer, tocopy As Integer strsearch = CStr(InputBox("enter the string to search for")) 'Enter code for all sheets in here... lastline = Range("A65536").End(xlUp).Row j = 1 For i = 1 To lastline For Each c In Range("A" & i & ":Z" & i) If InStr(c.Text, strsearch) Then tocopy = 1 End If Next c If tocopy = 1 Then Rows(i).Copy Destination:=Sheets("Slave").Rows(j) j = j + 1 End If tocopy = 0 Next i End Sub 

有更快的方法来做到这一点,但这只是在表循环中添加

 Sub CommandButton1_Click() Dim strsearch As String, lastline As Long Dim sht as WorkSheet strsearch = CStr(InputBox("enter the string to search for")) j = 1 For Each sht in ThisWorkbook.WorkSheets If sht.Name <> "Slave" Then lastline = sht.Cells(Rows.Count, 1).End(xlUp).Row For i = 1 To lastline For Each c In sht.Range("A" & i & ":Z" & i) If InStr(c.Text, strsearch) Then sht.Rows(i).Copy Destination:=Sheets("Slave").Rows(j) j = j + 1 Exit For 'stop looking! End If Next c Next i End If Next sht End Sub