I have a downloaded CSV file that I am trying to open and then save as an .xlsx file using code. I am very very new to VB, and have just modified code that I have found to make things work. Yesterday the code worked great, but today it stalls out on the "wb.SaveAs" line. The odd thing about the error is that the file actually saved to the folder. Any help would be appreciated.
Public Function Upload_New_Orders_Excel()
'the path to the excel workbook
Dim XcelFile As Excel.Application
Dim wb As Excel.Workbook
Dim strExcelPath As String
Set XcelFile = New Excel.Application
Set wb = XcelFile.Workbooks.Open("C:\Users\mpdav\downloads\orders_export_1.csv")
wb.SaveAs FileName:="C:\Users\mpdav\OneDrive\Documents\R&M Hospitality\At Your Door\Daily Reports\orders_export_1.xlsx", FileFormat:=51
wb.Close
Set XcelFile = Nothing
Kill "C:\Users\mpdav\downloads\orders_export_1.csv"
strExcelPath = "C:\Users\mpdav\OneDrive\Documents\R&M Hospitality\At Your Door\Daily Reports\orders_export_1.xlsx"
'import data from excel
Call DoCmd.TransferSpreadsheet(acImport, acSpreadsheetTypeExcel8, "temp_tbl_orders_export", "C:\Users\mpdav\OneDrive\Documents\R&M Hospitality\At Your Door\Daily Reports\orders_export_1.xlsx", True, "A1:BP1000")
Kill "C:\Users\mpdav\OneDrive\Documents\R&M Hospitality\At Your Door\Daily Reports\orders_export_1.xlsx"
End Function
Related
I have some code for exporting subform results to Excel workbook. Code works fine, only one small issue. If I do export, excel file opens If user wants I open. When this Excel file is opened and user wants to do Export again, I receive error 1004.
This error is produced because file is open, and new Excel object want to save a file with same name. What I want is when this happens, just cancel everything and let user know that he must first close this previously created workbook. Here is what I tried:
If Err.Number = 1004 Then
MsgBox "Error. You have opened Excel file, that has same name as this file name should be. Please close that file first !", vbCritical
Cancel = True
Set wb = Nothing ' wb is wb=XcelFile.Workbooks.Add
Set XcelFile = Nothing ' Xcelfile is Xcelfile= New Excel.Application
End If
This code works, when user closes that file, export can be performed - old file is just overwritted. Problem is that Excel application is still opened in Windows Task Manager, so Excel object is not properly closed.
Does anybody have a better solution ?
P.S.: I tried including numbers in file name of Excel, so that It wouldn't be same name, but I can't get It fixed.
EDIT: Here is how I tried changing filename
Dim i as Integer
ExcelFilename = "RESULTS_" & Format(Date, "dd/mm/yyyy") & "_" & i & "_" & ".xlsx"
i = i + 1
"i" doesn't change It's value when I run code once again. How can I make it increment ? This would solve my problem...
I suggest a simple solution: add the time to the file name to prevent conflicts.
ExcelFilename = "RESULTS_" & Format(Now(), "yyyy-mm-dd_hh-nn-ss") & ".xlsx"
For a number that will increment as long as the application is running, try
Static i As Integer
Static variables
You must be very strict in opening the Excel objects and closing them in reverse order - as done in this example:
Public Sub RenameWorkSheet()
Dim xls As Excel.Application
Dim wkb As Excel.Workbook
Dim wks As Excel.Worksheet
Set xls = New Excel.Application
Set wkb = xls.Workbooks.Open("c:\test\workbook1.xlsx")
Set wks = wkb.Worksheets(1)
wks.Name = "My New Name"
wkb.Close True
Set wks = Nothing
Set wkb = Nothing
xls.Quit
Set xls = Nothing
End Sub
I have a PowerPoint macro that should open an Excel file.
Public Sub SortList()
Dim MyFile as String
Dim xlApp as Object
Dim xlWorkBook as Object
Set xlApp = CreateObject("Excel.Application")
With Application.FileDialog(msoFileDialogOpen)
.AllowMultiSelect = False
.Show
MyFile = SelectedItems(1)
End With
xlApp.Visible = True
xlApp.Workbooks.Open MyFile
Set xlWorkBook = xlApp.Workbooks.Open(MyFile)
This code was previously working with no errors. However, I have started receiving the error message
"Run-time error '-2147467259 (80004005)': Method 'Open' of object 'Workbooks' failed."
The error occurs when trying to run the "Set" line of code.
The issue is that I can see that the code is successfully opening the Excel file!
Things I have tried (in about every possible combination):
-Changing the code to this (I thought maybe the program was trying to open the file twice):
xlApp.Visible = True
Set xlWorkBook = xlApp.Workbooks.Open(MyFile)
-Adding ReadOnly:= True to both or either .Open command, at one point this gave me an "Automation error- unspecified error" message which I have never seen before...
-Changing the Set line to this (and variations thereof):
xlApp.Workbooks.Open MyFile
Set xlWorkBook = xlApp.Workbooks(Dir(MyFile))
-Ensuring Excel is completely closed prior to running the code
The frustrating aspect is that this code worked perfectly last week, so I'm also at a loss as to why it would suddenly stop working. Any assistance would be greatly appreciated.
Workbooks.Open can cause issues if you call it on an already open workbook.
So, don't open twice. Remove the following line:
xlApp.Workbooks.Open MyFile
I know it might be a silly mistake but I tried many ways of doing that and none of them worked.
I have this code in place for grabbing data from other excel sheets in a folder and pasting it in a master folder. The issue pops up when I try to use wildcards to look for files where part of the name is variable. In the example below, the file name is Stock_RTC_17.02.2019.xlsx.
However, excel returns an error that the file is not found in the folder for the code below, even though you can see it has found the correct file name. Anyone got a clue on what I'm doing wrong?
Sub copytest() 'Procedure for retrieving data from the sourcefiles
Dim wbTarget, wbSource As Workbook
Dim target As Object
Dim pathSource As String
Dim xlApp As Application
'path where the data source folders are located (please keep all of them in the same directory)
pathSource = "C:\Users\vferraz\Desktop\crm stock\RAPOARTE IMPORTANTE\18.02\Rapoarte pentru Handsets\"
Set wbTarget = ThisWorkbook
Set xlApp = CreateObject("Excel.Application")
xlApp.DisplayAlerts = False
Application.CutCopyMode = False
'Stock RTC
Dim FileName As String
FileName = Dir(pathSource & "Stock_RTC_*.xlsx", vbNormal)
Set wbSource = xlApp.Workbooks.Open(FileName)
wbSource.Sheets(1).UsedRange.Copy
wbSource.Close
Set target = wbTarget.Sheets("Stock Aberon GW TKR")
target.UsedRange.Clear
Range("A1").Select
target.Paste
End Sub
I think pathSource is not the current working directory, so you should write:
Set wbSource = xlApp.Workbooks.Open(pathSource & FileName)
You are missing the path when you try to open the workbook.
Set wbSource = xlApp.Workbooks.Open(pathSource & FileName)
I am getting error Run-time error 3125 'Import$' is not a valid name. Make sure that it does not include invalid characters or punctuation and that it is not too long when I am attempting to import an Excel file to an Access Table. I only receive this error when other Excel files are open otherwise the Excel file is loaded to the table as expected. Here is some of the code I am working with:
Dim f As FileDialog, str As String
Set f = Application.FileDialog(msoFileDialogFilePicker)
f.Show
On Error GoTo Err1
str = f.SelectedItems(1)
Dim xl As Excel.Application
Set xl = New Excel.Application
xl.Visible = True
Dim xlWB As Excel.Workbook
Set xlWB = xl.Workbooks.Open(str)
'manipulate Excel file
'load spreadsheet
On Error GoTo Err2
DoCmd.TransferSpreadsheet acImport, 8, "JobCode", str, True, "Import!"
I originally thought and tried to change the filename, but was unsuccessful. It seems like the code is opening a new version of the import Excel file as Read-Only which might be confusing the Access database on which worksheet to import. But I am not sure why it's doing this and also why it only does this when another Excel file is open. Is this the problem? Does anyone know how to fix this?
Thank you!
I have some code for exporting subform results to Excel workbook. Code works fine, only one small issue. If I do export, excel file opens If user wants I open. When this Excel file is opened and user wants to do Export again, I receive error 1004.
This error is produced because file is open, and new Excel object want to save a file with same name. What I want is when this happens, just cancel everything and let user know that he must first close this previously created workbook. Here is what I tried:
If Err.Number = 1004 Then
MsgBox "Error. You have opened Excel file, that has same name as this file name should be. Please close that file first !", vbCritical
Cancel = True
Set wb = Nothing ' wb is wb=XcelFile.Workbooks.Add
Set XcelFile = Nothing ' Xcelfile is Xcelfile= New Excel.Application
End If
This code works, when user closes that file, export can be performed - old file is just overwritted. Problem is that Excel application is still opened in Windows Task Manager, so Excel object is not properly closed.
Does anybody have a better solution ?
P.S.: I tried including numbers in file name of Excel, so that It wouldn't be same name, but I can't get It fixed.
EDIT: Here is how I tried changing filename
Dim i as Integer
ExcelFilename = "RESULTS_" & Format(Date, "dd/mm/yyyy") & "_" & i & "_" & ".xlsx"
i = i + 1
"i" doesn't change It's value when I run code once again. How can I make it increment ? This would solve my problem...
I suggest a simple solution: add the time to the file name to prevent conflicts.
ExcelFilename = "RESULTS_" & Format(Now(), "yyyy-mm-dd_hh-nn-ss") & ".xlsx"
For a number that will increment as long as the application is running, try
Static i As Integer
Static variables
You must be very strict in opening the Excel objects and closing them in reverse order - as done in this example:
Public Sub RenameWorkSheet()
Dim xls As Excel.Application
Dim wkb As Excel.Workbook
Dim wks As Excel.Worksheet
Set xls = New Excel.Application
Set wkb = xls.Workbooks.Open("c:\test\workbook1.xlsx")
Set wks = wkb.Worksheets(1)
wks.Name = "My New Name"
wkb.Close True
Set wks = Nothing
Set wkb = Nothing
xls.Quit
Set xls = Nothing
End Sub