I have two files. In the first i wrote a macro that takes as input the second file and open it to read its datas. The problem is that every time i launch the macro the second file open and i don't want that. I think the problem is this:
path = "C:\Users\Me\Desktop\example.xls"
Set openWb = Workbooks.Open(path)
The Workbooks.Open(Path) that open the file. How can i solve the problem? I need only take the example.xls file and read its data without open that file in summary. Thanks a lot
Related
I recently started using python 3.6 (trough Anaconda) so I am sorry if the below will result not that clear. The situation is the following:
I have an excel file, which I use to run a macro in order to obtain results from Prophet. My macro works good but, unfortunately, after several times it has been used excel reaches the limit of memory and I cannot extract the results anymore. The way to solve the problem would be to close excel and then open it again.
To do so, in the same vba macro, I open a python script that allows me to run the macro, save the excel file, close it and open it again. The macro is the below:
import win32com.client
object = win32com.client.Dispatch('Excel.Application')
object.visible=1
object.Run("Central_Run")
print("Macro ran successfully!")
print("Active WB:", object.ActiveWorkbook.Name)
for wb in object.Workbooks:
print("WB:",wb.Name)
wb.Save()
#The loop above saves all the workbooks that are open
object.quit()
#Open the Excel file again
object=win32com.client.Dispatch('Excel.Application')
wb=object.Workbooks.open(‘myfile.xlsm')
The script works, however, I would like to open the excel file, in the second part of the script, without specifying its name (wb=object.Workbooks.open(‘myfile.xlsm')), so that I can use the code for other similar excel files with different name. Ideas that I had were either to open the latest used excel file or to link the script to a specific cell in my excel file so that I need just to modify that cell instead of the script, but, unfortunately, I am not able to do it.
Thanks in advance for the help! :)
You can replace your second print statement with this:
wbName=object.ActiveWorkbook.Name
print("Active WB:", wbName)
and then replace the last line with:
wb=object.Workbooks.open(wbName)
I have written a macro which downloads data from an online directory. The directory contains a file for each date:
29062016.csv
28062016.csv
27062016.csv
.. etc
My macro keeps my spreadsheet up to date with this data and downloads a file if it detects it is missing and copies it all to a single worksheet. So far so good.
I am using the following code within a loop to download each day that a user wishes to update (along with some control variables which keep count of rows).
Dim thisWb, downloadWb As Workbook
Set thisWb = ActiveWorkbook
Set downloadWb = Workbooks.Open("ftp://myusername:mypassword#address.com/directory/" & dateToDownload &".csv")
downloadWb.Worksheets(1).Range("A1:M" & lastRow).Copy Destination:=thisWb.Worksheets("Data").Range("A" & CStr(rows))
downloadWb.Close
My issue is this method is slow as excel has to individually open each file then copy paste, especially when a few days haven't been updated.
I can ask my client to change their data feed so I was wondering if anyone could recommend a method to speed up data download, either by changing the feed or my code? Would it be faster to download and update from a single file containing all data (may become very large over time)? Different format .txt maybe? Would a http request be more efficient?
Thanks
I have a program in an Excel workbook that opens a Powerpoint-File, updates the links within this file and closes it after that. This works fine. Here is my problem: When the links are updated an Excel file with the source data is opened. After Powerpoint is closed this file stays open. I want it to get closed because I repeat this process for many files and I can't end up with hundreds of open Excel files.
I tried the following:
WBKs=Application.Workbooks.count
For i = WBKs to 1 Step -1
If Workbooks(i).Name<>ThisWorkbook.Name then
Workbooks(i).close savechanges:=False
End if
Next i
Now comes the weird part. Whenever I just run my code, WBKs always returns 1 and the Excel file only pops up after the code is finished. If I go through my code in debug mode it works. The workbook pops up as soon as I enter debug mode.
I tried Applicatio.Wait in the hope that the file would show after a second. The file only showed after the code was finished.
I tried a Do While Loop to wait until the file is open. Excel crashes because I never leave the loop.
Edit: I tried DoEvents as suggested. Does not work either.
This is just a workaround, but try using a brute force after x times your macro has run. Store that x somewhere in workbook, save. And kill excel process (all instances, including self) :
Dim sKill As String
sKill = "TASKKILL /F /IM excel.exe"
Shell sKill, vbHide
Found here : VBA script to close every instance of Excel except itself
When running your macro next time, you will use that x as a starting point for next PPT file to update.
I've created a workbook which contains some macros along with it. My task is to save it including macros so that I'll be able to run them on the saved workbook. When I try to save it in ".xlsm" format--which is a standard format to save vba code--it's getting saved however, I'm unable to open the workbook from the desired saved file. The following window is displayed
" Excel cannot open the file "file.xlsm" because the file format or extension is not valid. Verify that the file is not corrupted and that the file extension matches the format of the file"
Can someone help me with this ? I've tried using ".xls",".xltm" formats as well. But, they don't save the vba code.
Try manually changing the file extension to ".xls" and then open it. If it opens, then do a "save as" in Excel and save it as "macro-enabled" (.xlsm).
I need to save several xlsm files as CSV in order to import them in other programs (such as R etc.). Well I've written the necessary import routines in all the other programs and they work very nicely with case 1:
Case 1: Manually saving an xlsm as CSV
If I use this option and manually save each file as CSV and click YES on all prompts, I get a .csv file which looks very similar to a normal Excel file when opened again within excel. It is a standard column view, and nothing comma separated etc. (maybe it is, but it doesn't look that way..)
Case 2: Saving an xlsm from VBA as CSV
Well here I get a completely different file when opened again in Excel. This one looks like a "real" csv file with all values being comma separated.
My questions are:
1. Why is there any difference?
2. How can I programmatically reach Case 2 from VBA? Or is that impossible?
If 2 is impossible I have to rewrite my import code routines to handle the "normal" csv file...not very difficult but still a lot of work and I'm really wondering why there even is a difference..
Q1: I don't think there is a difference, at least not in an example I pulled together. Q2: Try this out:
I've got 3 example XLSM files in C:\stack\folder1 as pictured below:
Each file has a single data sheet, which we'll turn into CSVs:
I'm sure your routine is much more complicated, but to test the CSV output I'm just going to loop through the files and save each as xlCSV:
Option Explicit
Sub TestCSVOutput()
Dim DataBook As Workbook
Dim DataSheet As Worksheet
Dim FilePaths(3) As String
Dim FileIdx As Long
'set up file paths for test
FilePaths(1) = "C:\stack\folder1\test_file_01.xlsm"
FilePaths(2) = "C:\stack\folder1\test_file_02.xlsm"
FilePaths(3) = "C:\stack\folder1\test_file_03.xlsm"
'loop through array and save each file as a CSV
Application.DisplayAlerts = False
For FileIdx = 1 To UBound(FilePaths)
Set DataBook = Workbooks.Open(FilePaths(FileIdx))
Set DataSheet = DataBook.ActiveSheet
DataBook.SaveAs FileFormat:=xlCSV '<~~ the save step
DataBook.Close
Next FileIdx
Application.DisplayAlerts = True
End Sub
Once the script completes, I end up with three CSV files:
Each file is comma-delimited when opened in a text editor: