|
应网友要求做个历遍工作簿中所有工作表的所有单元格的过程,
但问题出现在最后关不掉Excel程序。查询答案倒是正确的。
附上代码和附件,请各位兄弟帮忙,指教。
- Private Sub Command0_Click()
- Dim Conn As New ADODB.Connection
- Dim rs As New ADODB.Recordset
- Dim i, j, k As Integer
- Dim strName, strSQL, str As String
- Dim xlApp As New Excel.Application
- Dim xlBook As Excel.Workbook
- Dim xlSheet As Excel.Worksheet
- If IsNull(Me.Text3) Then
- MsgBox "Enter then Data to Find,Please!!"
- Me.Text3.SetFocus
- Exit Sub
- End If
- strName = CurrentProject.Path & "\book1.xls"
- Set xlBook = xlApp.Workbooks.Open(strName)
- Conn.Open _
- "Provider=Microsoft.Jet.OLEDB.4.0; Persist Security Info=False;Data Source=" _
- & strName & "; Extended Properties='Excel 8.0;HDR=Yes'"
- For k = 1 To xlBook.Sheets.Count
- strSQL = "select * from [" & xlBook.Sheets(k).Name & "$]"
- rs.Open strSQL, Conn, adOpenKeyset, adLockReadOnly
- Do While Not rs.EOF
- For i = 1 To rs.Fields.Count - 1
- If rs.Fields(i) = Me.Text3 Then
- str = xlBook.Sheets(k).Name & " " & rs.Fields(0) & i
- j = 1
- Exit Do
- End If
- Next
- rs.MoveNext
- Loop
- rs.Close
- If j = 1 Then
- Exit For
- End If
- Next
- If Len(str) <> 0 Then
- MsgBox str
- Else
- MsgBox "Nothing Was Finded"
- End If
- Conn.Close
- Set Conn = Nothing
- Set rs = Nothing
- xlBook.Close
- Set xlBook = Nothing
- xlApp.Quit
- End Sub
复制代码 |
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?注册
x
|