1

Here's what I'm trying to do and I apologize if I'm headed the wrong direction. I'm trying to cycle through the filepath's stored in table t_Directory and if the file extension is "xlsx" open the Excel file and update another table called t_SheetInfo with the FileID of the Excel Worksheet and sheet count and the sheet name. Would anyone have a minute to check what I've got so far or steer me in the right direction if there's a more efficient way to do it? I'm not 100% sure that I know what I'm doing. As always, thank you in advance for any help!!

Dim db As DAO.Database
Dim xlApp As Excel.Application
Dim xlWB As Excel.Workbook
Dim xlWS As Excel.Worksheet

Private Sub CycleThroughWorkSheets()
Dim rs As DAO.Recordset
Dim rs2 As DAO.Recordset
Dim sSQL1 As String
Dim sSQL2 As String
Dim sSQL3 As String
Dim rsFilePath As String
Dim rsWSName As String

Set db = CurrentDB

sSQL1 = "SELECT t_Directory.FileID, t_Directory.FilePath FROM t_Directory " & _
        "WHERE (((t_Directory.FileExtension)=""'xlsx'""))"
Set rs = db.OpenRecordset("sSQL1", dbOpenDynaset)
    With rs
        rs.MoveFirst
            Do While Not rs.EOF
                rsFilePath = rs.Fields("[FilePath]")
                OpenWorkBook (rsFilePath)
Set rs2 = db.OpenRecordset("t_SheetInfo", dbOpenDynaset)
    With rs2
        rs2.MoveFirst
            Do While Not rs2.EOF
                rs2.AddNew
                rs2.Fields("FileID") = rs.Fields(1)
                rs2.Fields("[SheetIndex]") = WorkSheetCount(rsFilePath)
                rs2.Fields("[SheetName]") = WorkSheetName(WorkSheetCount)
                rs2.Update
            Next
            Loop
    End With
    End With
Set rs = Nothing
Set rs2 = Nothing
End Sub

Public Function WorkSheetCount(rsFilePath As String) As Integer
Set xlWB = xlApp.Workbooks.Open(rsFilePath)
      WorkSheetCount = xlWB.Sheets.Count(rsFilePath)
      Debug.Print "WorkSheetCount : " & WorkSheetCount
End Function

Public Function WorkSheetName(WorkSheetCount As Integer) As String
Set xlWB = xlApp.Workbooks.Open(rsFilePath)
      WorkSheetName = Worksheets(WorkSheetCount).Name
      Debug.Print "WorkSheetName : " & WorkSheetName
End Function

1 Answer 1

7

Try something on these lines. Step through.

Dim xlApp As New Excel.Application
Dim xlWB As Excel.Workbook
Dim sh As Object ''Some sheets may be charts
Dim rs As DAO.Recordset
Dim rs2 As DAO.Recordset
Dim sSQL1 As String
Dim sSQL2 As String
Dim sSQL3 As String
Dim rsFilePath As String
Dim rsWSName As String

Set db = CurrentDb
xlApp.Visible = True

sSQL1 = "SELECT t_Directory.FileID, t_Directory.FilePath FROM t_Directory " & _
        "WHERE t_Directory.FileExtension='.xlsx'"
Set rs2 = db.OpenRecordset("t_SheetInfo", dbOpenDynaset)

Set rs = db.OpenRecordset(sSQL1, dbOpenDynaset)
Do While Not rs.EOF
    rsFilePath = rs.Fields("[FilePath]")
    Set xlWB = xlApp.Workbooks.Open(rsFilePath)

    For Each sh In xlWB.Sheets
        rs2.AddNew
        rs2.Fields("FileID") = rs.Fields("FileID")
        rs2.Fields("[SheetIndex]") = sh.Index
        rs2.Fields("[SheetName]") = sh.Name
        rs2.Update
    Next
    rs.MoveNext
    xlWB.Close False
Loop
Set rs = Nothing
Set rs2 = Nothing
xlApp.Quit
Sign up to request clarification or add additional context in comments.

2 Comments

Remou, as always, thank you again! I'm going to have to compare your code to mine and make sure that I fully understand it, but that does exactly what I needed it to. Thank you again!!
Thank you @fionnuala - This wasn't the issue I had, but it was the first Google search result for my problem & answered my question!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.