[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

Question
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

9.8

set focus to excel sheet in VBA

Asked by ClaudeWalker in Access Coding/Macros, Microsoft Access Database, Microsoft Excel Spreadsheet Software

Tags: focus, vba, excel, set

I 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:="TOTALS", LookAt:=xlWhole, LookIn:=xlValues)
                 
                 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
[+][-]07/11/07 02:16 PM, ID: 19466868Accepted Solution

View this solution now by starting your 30-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

About this solution

Zones: Access Coding/Macros, Microsoft Access Database, Microsoft Excel Spreadsheet Software
Tags: focus, vba, excel, set
Sign Up Now!
Solution Provided By: zorvek
Participating Experts: 1
Solution Grade: A
 
 
Loading Advertisement...
20091118-EE-VQP-93