Exporting 2 tables using 2 queries into one Excel file (2 different tabs)

santa12345

Board Regular
Joined
Dec 2, 2020
Messages
70
Office Version
  1. 365
Platform
  1. Windows
Hello.
I have the following script that is working.

A ID table is opened, txtDist= ID command pulls one ID at a time.
Query output is named output and then transfered to a output tab in a output.xls file
Renames the file and then loops again until it finishes the entire ID table.
I want to keep this but add around ID output .. lets say ID2 which would then output to output2 and transfers to the same excel file but a separate tab (output2) and then renames the file like below and loops.

Any ideas would be greatly appreciated.
Thanks.


----
Private Sub Command1_Click()

DoCmd.SetWarnings 0

Set objFSO = CreateObject("Scripting.FileSystemObject")

Dim rs As DAO.Recordset
Dim db As DAO.Database
Set db = CurrentDb
Set rs = db.OpenRecordset("IDs", dbOpenTable)

rs.MoveFirst

Do Until rs.EOF

txtDist = rs![ID]

'Set objFolder = objFSO.CreateFolder("C:\test\" & txtDist)

DoCmd.OpenQuery "Distributor Output", , acReadOnly
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel8, "output", "C:\output.xls", True

RetVal = Shell("cmd /c copy /y C:\output.xls C:\test\" & txtDist & ".xls", vbHide)
rs.MoveNext

Sleep (50000)
Loop
DoCmd.SetWarnings -1

End Sub
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.

Forum statistics

Threads
1,224,817
Messages
6,181,149
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top