We help IT Professionals succeed at work.

Access VBA- trying to get data from Excel worksheet, multiple worksheets isse

scbdpm
scbdpm asked
on
All,

I am trying to grab data from an Excel file. The Excel file has three worksheets in the workbook.

I've been successful in doing so except in indicating which of the worksheets to pull the data from.

I am interested in the worksheet called 'sum' (see code below).

My code below only wants to select the data from the worksheet that was the 'selected' one when the workbook was last saved!

I'm trying to loop through all worksheets and just grab 'sum' with the for next loop below.

Using Access 2010

HELP!

  Set oXL = CreateObject("Excel.Application")
    Set oWbk = oXL.Workbooks.Open(strFileName)
    Dim i As Integer, intNumSheets As Integer, intSheet As Integer
    'For i = 1 To Sheets.Count
    '    .Cells(i, 1).Value = Sheets(i).Name
    'Next i
   
     intNumSheets = oWbk.Sheets.Count
    For i = 1 To intNumSheets
    If oWbk.Sheets(i).Name = "sum" Then
        intSheet = i
        Exit For
    End If
       ' MsgBox (i & " - " & oWbk.Sheets(i).Name)
    Next i

   
    'oWst = oWbk.Worksheets(intSheet)
   

        Set oWst = oXL.Worksheets(2)
Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2016

Commented:
try this

Dim i As Integer, intNumSheets As Integer, intSheet As Integer
Dim oXL as object
Set oXL = CreateObject("Excel.Application")
     oXL.Workbooks.Open(strFileName)

with oXL
      For i= 1 to .worksheets.count
             if .worksheets(i).name="sum"  then
                  msgbox "Found worksheet Sum"

                 ' what do you want to do ?
                 'place codes here
             end if
      next
end with

oXL.quit

Author

Commented:
I want to set the worksheete to 'sum'

this isnt' working:
Set oWst = oXL.Worksheets(2)
CERTIFIED EXPERT
Top Expert 2016

Commented:
did you copy and paste the codes i posted above?

Author

Commented:
it's pretty much what I posted.

isn't it?
NorieAnalyst Assistant
CERTIFIED EXPERT

Commented:
Try this.

It will set oWst to the 'sum' worksheet if it's found in the workbook.
Set oXL = CreateObject("Excel.Application")
Set oWbk = oXL.Workbooks.Open(strFileName)

For Each oWSt In oWbk.Worksheets
    If oWSt.Name = "sum" Then Exit For
Next oWSt

' check worksheet has been found

  If Not oWSt Is Nothing Then

       Msgbox "sum worksheet found"

       ' code to do stuff with worksheet
       Msgbox oWSt.Range("A1").Value
Else
       Msgbox "sum worksheet not found"

End If

' close workbook

oXLWbk.Close False

oXL.Quit

Set oXL = Nothing

Open in new window

CERTIFIED EXPERT
Top Expert 2016

Commented:
<it's pretty much what I posted.> definitely NOT.

Author

Commented:
nope, still grabbing the sheet that was saved as in focus (turns out its the third).

I can't imagine this makes a difference but this is a password protected workbook...
NorieAnalyst Assistant
CERTIFIED EXPERT

Commented:
Why don't you just use this line from the original code removing the ' amd adding Set?
Set  oWst = oWbk.Worksheets(intSheet)

Open in new window

Author

Commented:
I used the following and it didn't work:

i = 1

For Each oWst In oWbk.Worksheets
    If oWst.Name = "sum" Then
    intSheet = i
    Exit For
    End If
    i = i + 1
Next oWst

   
' check worksheet has been found

  If Not oWst Is Nothing Then

       MsgBox "sum worksheet found"

       ' code to do stuff with worksheet


   
Set oWst = oWbk.Worksheets(intSheet)
CERTIFIED EXPERT
Top Expert 2016

Commented:
so you don't want to try the codes i posted...

Author

Commented:
that is your code but assining intSheet from i in the for loop....
NorieAnalyst Assistant
CERTIFIED EXPERT

Commented:
capricorn's code will work.

The orginal code will work, if you make the change I suggested.

The code I posted should work too.

If none of the code is working can you post explaining how it isn't working?

Author

Commented:
perhaps I'm missing somethign but this isn't working for me:

<nope, still grabbing the sheet that was saved as "in focus" (turns out its the third).>


code is not grabbing the 'sum' worksheet
CERTIFIED EXPERT
Top Expert 2016

Commented:
<that is your code but assining intSheet from i in the for loop....> that is NOT my codes .please do NOT insists and mixing your codes with what I posted..

just let us know what you want to do
CERTIFIED EXPERT
Top Expert 2016
Commented:
copy this codes SEPARATELY



Dim i As Integer, intNumSheets As Integer, intSheet As Integer
Dim oXL as object
Set oXL = CreateObject("Excel.Application")
     oXL.Workbooks.Open(strFileName)
     oXL.visible=true
with oXL
      For i= 1 to .worksheets.count
             if .worksheets(i).name="sum"  then
                  msgbox "Found worksheet Sum"

                 ' what do you want to do ?
                 'place codes here
                 
                   .worksheets(i).select

             end if
      next
end with
NorieAnalyst Assistant
CERTIFIED EXPERT

Commented:
How do you know the code isn't working?

PS I've checked them all and they all work.

Author

Commented:
sory guys... not sure who to make this more clear......


I need to take data in a worksheet that is called 'sum'


the code I posted in the original question will take the information not from the 'sum' worksheet but whatever worksheet was 'out front' (sorry dont know the technical term) when the workbook was last saved.

I need to get the worksheet 'sum'.
CERTIFIED EXPERT
Top Expert 2016

Commented:
did you see my post at http:#a37716701 ?

Author

Commented:
capricorn1:
yes but I had to add the 'Set' line.

see below:

With oXL
      For i = 1 To .Worksheets.Count
             If .Worksheets(i).Name = "sum" Then
                  MsgBox "Found worksheet Sum"

                 ' what do you want to do ?
                 'place codes here
                 
                   .Worksheets(i).Select
                    Set oWst = oWbk.Worksheets(i)

             End If
      Next
End With

Explore More ContentExplore courses, solutions, and other research materials related to this topic.