简体   繁体   中英

Access VBA Dynamic Excel Import

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM