Function Save in excel vba is acting as SaveAs - excel

I have a macro, and I am struggeling with saving. The macro:
opens second workbook from SharePoint
unprotects it
unprotects a sheet in the original workbook
copies line(s)
pastes those line(s) in the second workbook
protects the sheet in the second workbook
saves the second workbook
closes it
protects the sheet in original workbook
I have this macro in 4 files, always 1 original file - 1 second file. Two files are without any problems, but the other 2 files, I can't save the second file. It says that there is already a file with this name, and asks if I want to rewrite it.Why does it ask that, when I am using the function .Save?Or, if this cannot be managed, how do I write in the macro that it should choose - yes, rewrite?
Thank you.
I tried to use the combined function Close SaveChanges:=True, but it didnt help.

Related

How can I list all worksheet tab names in another workbook in Excel?

I am working with 40+ workbooks and need a list of every named tab in each one, for a cataloguing project. The only macros and instructions I found require creating a formula or macro for each workbook.
I have one file designated as the macro file. In it are two functions, one to sort tabs by color and one to sort them alphabetically. Both work on whatever workbook I have active, as long as the macro workbook is open in the background.
How can I list all the sheet names in the active workbook, using code from the macro workbook?
I don't mind running the command once for each workbook, but I don't want to create a macro in every file.
You can use Workbook.Sheets to access the sheets of a workbook.
Likewise, you can use Workbooks to access the open workbooks.
You can create a nested pair of For loops to iterate through all workbooks, and then all worksheets.

Copy data and a button with macros to another workbook

I want to copy all the data of the workbook into another workbook,
and one of the worksheets has a button which has a macro within itself.
workbook.worksheets.copy()
code above copy the data successful, but a broken button with it.
When I click the button on the new wb, the message "Sorry we couldn't find..XXXXX. Is it possible it was moved, renamed, or deleted?" popped up.
XXXXX is the path of origin workbook.
And I can't see any module in new workbook.
Is there a method could copy all the contents into the new wb?
If I understood correctly, you want to transfer a macro/module from one workbook to another using a vba macro.
The button obviously wont work if the underlying macro is not transferred.
However, transferring a macro can only be done manually.
There is no way to copy or transfer a macro using another macro in VBA.

Reference to part of filename recalculates to wrong workbook

I have the following formula:
=MID(CELL("filename"),FIND(".xls",CELL("filename"))-4;4)
It looks for the last 4 characters in the filename.
This works correctly, however when I open another workbook, or do a modification in a different workbook and return to this workbook it changed it's calculation result to the last 4 characters of the last used workbook, prior to activating this workbook.
Is there a way (avoiding vba and not messing with calculation to manual) to prevent this?

Copy a sheet from another open workbook into current workbook

I'm trying to write a macro that takes Sheet1 from another, already open, workbook and copy it into a the workbook that will use the data. The macro will be run from the workbook that will be receiving the copied sheet and the file I want the data from will always be named the same.
I've tried the follow code, but it either just copies incorrectly or doesn't work.
Workbooks("FileIWantToCopyFrom.XLSX").Sheets("Sheet1").Copy
ActiveWorkbook.Worksheets("FileIWantToCopyFrom.XLSX").Select
Sheets("Sheet1").Copy After:=Workbooks("Book1").Sheets(1)
Any help would be much appreciated. Thanks!

Once Excel sheets in a workbook saved as .csv, cant get back to .xlsm

I have a workbook with multiple sheets. I press a (ribbon) button and a subroutine saves multiple sheets as .csv, shells out to some java code, then comes back. The problem is: once it comes back, the workbook has been converted to a .csv file (the last one saved) and no further .xlsm operations are possible.
How do I fix this?
Amendment:
Has nothing to do with Java or shell. WHENEVER I do this:
Sheets("someSheet").SaveAs Filename:=someName, FileFormat:=xlCSV
this changes the ENTIRE workbook to someSheet.csv. Then, the behavior of the workbook gets weird. I haven't been able to save as .csv without changing EVERYthing.
Use SaveCopyAs.
It leaves your original workbook as is and saves a copy. Requires 2007 or newer but since you mention a ribbon button that should be fine. From the link:
ActiveWorkbook.SaveCopyAs "C:\TEMP\XXXX.XLS"
http://msdn.microsoft.com/en-us/library/office/bb178003(v=office.12).aspx
In Excel, the Save As operation, creates a new copy of the workbook with the new name and format/style, then closes the old one.
A .csv file doesn't support macros, so your workbook is behaving as designed.
In order to get back to your original workbook after doing the Save As, you will need to re-open the original workbook and then close the new .csv workbook.
Otherwise, you could come up with a custom Export method to just export the data on each sheet to a .csv file, but that seems needlessly complex.

Resources