Приведенный ниже код открывает электронную таблицу Excel из события кнопки в форме доступа и выполняет поиск строки в элементе управления формы на именованном рабочем листе. Я хотел бы адаптировать этот код для поиска по всем рабочим листам. Книга Excel - это переменная, извлекаемая из элемента управления формы.
```
Private Sub Command132_Click()
On Error GoTo Err_Command132_Click
Dim filename As String
Dim searchstring As String
Dim xlApp As Excel.Application 'Excel object
Dim XlBook As Excel.Workbook 'Workbook object
Dim Xlsheet As Excel.Worksheet 'Worksheet object
Set xlApp = CreateObject("Excel.Application")
searchstring = Me.Matrixsrch
filename = Me.GroupsMatrixLoccntrl
Set XlBook = xlApp.Workbooks.Open(filename)
xlApp.Visible = True
xlApp.ActiveWindow.WindowState = xlMaximized
Set Xlsheet = XlBook.Sheets("GroupMatrix")
With Xlsheet
.Cells.Find(What:=searchstring, After:=.Cells(1, 1), LookIn:=xlvalues, LookAt:=xlPart,
SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False).Activate
End With
Exit_Command132_Click:
Exit Sub
Err_Command132_Click:
MsgBox "Error " & Err.Number & "; " & Err.Description
Debug.Print "Error " & Err.Number & "; " & Err.Description
Resume Exit_Command132_Click
End Sub
I have tried using the code below to iterate through the worksheets. The search is run on the first worksheet and a value found, but subsequently an *Error 91; Object variable or With block variable not set* is generated. Can anyone help me with the iteration please? thanks
For Each Xlsheet In XlBook.Worksheets
With Xlsheet
.Cells.Find(What:=searchstring, After:=.Cells(1, 1), LookIn:=xlvalues, LookAt:=xlPart,
SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False).Activate
End With
Next