- For individual users
- Instant access to solutions
- Ask your tech questions
- Start your 30-day Free Trial
Main Topics
Browse All TopicsI am accessing an excel sheet through VBA I want to set the focus to a specific sheet in the excel workbook. Everything works except for the fact that if the workbook opens to a different sheet than what I am looking for I get an error. It looks for a wildcard criteria in a sheet
'Handle "*Expen*" worksheets only
If wks.Name Like "*Expen*" Then
if I debug print it finds the correct sheet but does not set the focus of the sheet. How would I set focus to the sheet so VBA knows where to search.
here is my code
Private Sub Command20_Click()
Dim path As String
Dim filter As String
Dim file As Variant
Dim matches As New Collection
CurrentDb.Execute "DELETE * FROM FileNumbers"
CurrentDb.Execute "INSERT INTO FileNumbers " & _
"SELECT EligibleFileNumbers.* " & _
"FROM EligibleFileNumbers"
Dim cn As New ADODB.Connection
Set cn = CurrentProject.Connection
Dim rs As New ADODB.Recordset
rs.ActiveConnection = cn
rs.Open "SELECT FileNumbers.* FROM FileNumbers", , adOpenStatic, adLockOptimistic
rs.MoveFirst
Dim XLApp As New Excel.Application
Dim wbk As Excel.Workbook 'Excel Workbook
Dim wks As Excel.WorkSheet ' Excel Worksheet
Dim rngFound As Excel.Range 'Excel Range
Dim fn As String
Dim arr(10) As Double
Do While Not rs.EOF
If Left(rs.Fields(0).Value, 5) = "1607W" Then GoTo 102
fn = rs.Fields(0).Value
arr(0) = fn
If Left(fn, 4) = "1607" Or Left(fn, 4) = "1606" Then
path = "S:\Weatherization\" & Left(fn, 4) & " Audits\"
Else
path = "S:\Weatherization\16 Older Audits\" & Left(fn, 4) & " Audits\"
End If
filter = Left(fn, 8) & "*.xls"
file = Dir(path & filter)
While file <> ""
matches.Add path & file
file = Dir()
Wend
If matches.Count = 0 Then
Debug.Print rs.Fields(0).Value
GoTo 102
End If
file = matches.Item(1)
path = file
Debug.Print path
Debug.Print file
Set wbk = XLApp.Workbooks.Open(path)
'Loop through all worksheets
For Each wks In wbk.Worksheets
'Handle "*Expen*" worksheets only
If wks.Name Like "*Expen*" Then
Debug.Print wks.Name
'Perform a search
Set rngFound = wks.Range("A1:G50")
Set rngFound = rngFound.Find(What:="PO NUMBER", LookAt:=xlWhole, LookIn:=xlValues)
Dim frow As Integer
frow = rngFound.Row
Debug.Print "Range found!!" & frow
Set rngFound = rngFound.Find(What:="TOTAL
Dim lrow As Integer
lrow = rngFound.Row - 1
Debug.Print "Range Found" & lrow
For x = frow + 2 To lrow
Debug.Print Cells(x, 1)
arr(1) = Cells(x, 1)
Debug.Print arr(0)
Debug.Print arr(1)
If Cells(x, 1) <> "" Then
For y = 2 To 7
arr(y) = Nz(Cells(x, y), 0)
Debug.Print arr(y)
Next y
CurrentDb.Execute "INSERT INTO Expend ( JobNumber, PO, regmat, hsmat, regcont, hscont, dammat, total ) " & _
"SELECT " & arr(0) & " AS Expr1, " & arr(1) & " AS Expr2, " & arr(2) & " AS Expr3, " & arr(3) & " AS Expr4, " & arr(4) & " AS Expr5, " & arr(5) & " AS Expr6, " & arr(6) & " AS Expr7, " & arr(7) & " AS Expr8 "
End If
Next x
'Show result
End If
Next wks
'Save and close workbook, Quit Excel
wbk.Save
wbk.Close
'Release object variables
Set rngFound = Nothing
Set rngLookHere = Nothing
Set wks = Nothing
Set wbk = Nothing
Set matches = Nothing
102
rs.MoveNext
Loop
XLApp.Quit
Set XLApp = Nothing
End Sub
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Business Accounts
Answer for Membership
by: zorvekPosted on 2007-07-11 at 14:16:04ID: 19466868
To activate a specific sheet:
wbk.Sheets("Sheet Name").Activate
Kevin