0

I'm trying to dynamically import an Excel Spreadsheet into Access. The problem is, i need to import a specific sheet (this part sorted out), but I'm having problems importing a sheet whose name changes month to month.

Code example:

DoCmd.TransferSpreadsheet acLink, acSpreadsheetTypeExcel12Xml, "MonthSales", FileName, 1, "Sales_Month!"

On the "Sales_Month!" is where i need it to be dynamic. I can import it directly if the sheet has always the same name. So my question is, can i import it with some sort of wildcard?

For example: "Sales_*!"

Note: This Excel Workbook has several worksheets.

2
  • 2
    In Excel VBA, you could loop through Worksheets and check if the current sheet's name is Like "Sales_*!". Something equivalent is presumably doable in Access VBA. Commented Oct 19, 2018 at 9:04
  • 1
    I totally agree with @Jean-FrançoisCorbett and I upvoted his/her comment. Loop trough all sheets checking all names until you find the one you want and then activate it. Then, once activated, you could use DoCmd.TransferSpreadsheet acLink, acSpreadsheetTypeExcel12Xml, "MonthSales", FileName, 1, ActiveSheet.Name Commented Oct 19, 2018 at 9:20

1 Answer 1

2

This should be simple a solution, looking for Excel Sheet names in a for cycle with Left operator:

Dim xls As New Excel.Application, sht As String, Wkb As Workbook, Wksh As Worksheet
Set Wkb = xls.Workbooks.Open(FileName)

For Each Wksh In Wkb.Worksheets
  if left(Wksh.Name, 6) = "Sales_" then
      DoCmd.TransferSpreadsheet acLink, acSpreadsheetTypeExcel12Xml, "MonthSales", FileName, 1, Wksh.Name
      Exit For
  end if
Next Wksh

Wkb.Close
xls.Quit
Set Wkb = Nothing
Set xls = Nothing
Exit Sub
Sign up to request clarification or add additional context in comments.

1 Comment

For this code to work, I think user must need to add a reference to Microsoft Excel Objects Library, because it is executing it from Access. Also ¿no binding? ¿is not necessary first to do a Set xls = New Excel.Application before doing Set Wkb = xls.Workbooks.Open(FileName)?

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.