I am able to duplicate a workbook (copy to a desired location) which contains a macro in the background. This duplicate copy also contains the same macro.
My Problem is I do not want this duplicate workbook to have a macro with it. Can anyone tell how to do it?
Thank you in advance!!!
Save your workbook as macro-free, i.e. simply as Excel Workbook. For my Excel 2007 this is done using:
Application.DisplayAlerts = False
ThisWorkbook.CheckCompatibility = False
ThisWorkbook.SaveAs Filename:="D:\DOCUMENTS\Book1.xlsx", FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
Application.DisplayAlerts = True
Correct path & name as you wish.
Read more about SaveAs method: http://msdn.microsoft.com/en-us/library/office/ff841185%28v=office.14%29.aspx
...and available File Formats: http://msdn.microsoft.com/en-us/library/office/ff198017%28v=office.14%29.aspx
Hope that was helpful)
Related
I've finally figured out why my code was crashing. I have this set up as part of my Personal Macro Workbook so when I open a default Book1 I can run it. However, the issue is that since it's running the macro from the PMW the "Sheet.Copy After:=ThisWorkbook.Sheets(1)" is crashing.
How can I make it that the code below running from the PMW would copy the sheets into the default Book1?
Original code below;
Sub GetSheets()
Application.AutoRecover.Enabled = False
LInput:
PL = Application.InputBox("Threshold Report Path", "", "C:\Users\")
Path = PL
Filename = Dir(Path & "*.csv")
Do While Filename <> ""
Workbooks.Open Filename:=Path & Filename, ReadOnly:=True
For Each Sheet In ActiveWorkbook.Sheets
Sheet.Copy After:=ThisWorkbook.Sheets(1)
Next Sheet
Workbooks(Filename).Close
Filename = Dir()
Loop
End Sub
ThisWorkbook refers to the workbook with the macro.
You can refer to it by name:
Sheet.Copy After:=Workbooks("Foo").Sheets(1)
I think you misunderstand the purpose of the Personal Macro Workbook; it shouldn't be auto-running anything. It's not a template. It's a place to store macros that you use often, so that instead of copying the macros to different workbooks, you can leave it in one place an run it from there.
I think what you want is a Personal Template that includes the template worksheet already, so nothing needs to be copied every time you create a new document.
Create a workbook, copy the worksheet in manually, and save it as a template. Avoid auto-run code in the template as well.
See links below for more information.
More information:
What you are trying to use:
Office.com : Create and save all your macros in a single workbook
Office.com : Create and save all your macros in a single workbook
What you should be using:
Office.com : Save a workbook as a template
Makeuseof : How to Quickly Create a Custom Excel Template to Save Time
I am looping through several worksheets in a file, copying information to another sheet (called wsCSV, which is stored in ThisWorkbook) and then I want to create a CSV file from this worksheet. After creating the sheet, the loop should continue to the next sheet.
Here is part of the code which should create the CSV:
' Create CSV file
If iControl = 1 Then
Application.DisplayAlerts = False
wsCSV.Copy
ActiveWorkbook.SaveAs Filename:=sDestin & "\" & sShName & ".csv", FileFormat:=xlCSV, CreateBackup:=True
ActiveWorkbook.Close SaveChanges:=False
Application.DisplayAlerts = True
End If
Anyway, when I proceed the code, SaveAs method starts creating "infinite" file, till it consumes all my disk space. I don't know how is it possible, as the file has 6 rows and 3 columns, so it should have 1-2 kb. I have to stop the process manually. Any thoughts? I tried this code in another macro and it worked OK.
Thanks!
Make sure that your wsCSV worksheet is truly as empty as you think it is. Use Ctrl-End to see what the last used cell is and clean it up if necessary.
I have a workbook where to speed computation (long story) I created a macro to copy out three of the sheets to another file and then another macro to copy them back it.
The macro to copy out works fine, however the macro to copy back in halts after copying in one sheet.
I searched within StackOverflow and found some similar questions but couldn't find an answer which worked. One post thought it was related to Office versions and one to a Shift key issue.
Here is the code:
Application.Calculation = xlCalculateManual
Application.ScreenUpdating = False
Application.DisplayAlerts = True
Application.EnableEvents = False
'
' Set up the workbooks
'
Set ThisWkb = ThisWorkbook
Fname = Application.GetOpenFilename( _
fileFilter:="Excel Macro Files, *.xlsm", _
Title:="Select the Storage File", _
MultiSelect:=False)
Set StorageWbk = Workbooks.Open(Fname)
'
MsgBox ("Beginning process - please click ok to any macro warning - you will see a confirmation when complete")
StorageWbk.Sheets("Sh A").Copy After:=ThisWkb.Sheets(ThisWkb.Sheets.Count)
StorageWbk.Sheets("Sh B").Copy After:=ThisWkb.Sheets(ThisWkb.Sheets.Count)
StorageWbk.Sheets("Sh C").Copy After:=ThisWkb.Sheets(ThisWkb.Sheets.Count)
StorageWbk.Close
I sometimes find that if I then delete the new sheet and run the macro again it sometimes works and reads all three sheets in, but it also sometimes doesn't.
Any help is greatly appreciated.
Quoting both YowE3K and nbayly to give the correct answer from the comments directly here (I had to look for it):
Unhide your hidden Sheets and copy the three links at once using:
StorageWbk.Sheets(Array("Sh A", "Sh B", "Sh C")).Copy After:=ThisWkb.Sheets(ThisWkb.Sheets.Count)
I've figured out most of this myself, but the part that is hanging me up is being able to paste the conditional formatting.
I want to be able to have a workbook open, run a VBA script to open another workbook, copy a range from it, then paste that to the original workbook.
The most success I've had is recording the macro and make this happen:
With the original, target workbook open...
Open the source workbook
Copy range A1:X105
Close source workbook
Paste into worksheet titled "Temp" in target workbook
The problem is that the source workbook contains conditional formatting, and if the source workbook is closed before you paste into the target workbook, the conditional formatting isn't being pasted.
So either I need to find a way to paste the data with the conditional formatting, or I need to be able to switch back to the target workbook before closing the source workbook. This is a process that is going to need to be ran multiple times with different target workbooks, so the VBA code can't refer to a workbook filename for the target. The source workbook will always have the same path though.
Searching the site, I could only find solutions that specified the path for both workbooks.
This is what I have right now:
Sub CopyData()
Application.DisplayAlerts = False
Workbooks.Open filename:="source.xlsx", _
UpdateLinks:=3
Range("A1:X105").Select
Selection.Copy
ActiveWindow.Close
Sheets("Temp").Select
ActiveSheet.Paste
Application.DisplayAlerts = True
End Sub
I suppose what I need to implement into this is to declare the target workbook as a variable. Can someone help with that?
You can just dim the workbook and then copy and paste. After you have completed that you can then using the variable, close the workbook. Code would be as follows:
Sub CopyData()
Application.DisplayAlerts = False
Dim wbSource As Workbook
Set wbSource = Workbooks.Open(Filename:="source.xlsx", UpdateLinks:=3)
wbSource.Sheets(1).Range("A1:X105").Copy
ThisWorkbook.ActiveSheet.Selection.Paste
wbSource.Close
Application.DisplayAlerts = True
End Sub
I don't see how you determine what range you paste on the target workbook but will leave that for another question. The answer by Emily Alden I don't think will work because you can't copy from a source that is closed. Clipboard behaves differently with Excel than with other applications.
Based on new information:
Sub CopyData()
Application.DisplayAlerts = False
Workbooks.Open filename:="source.xlsx", _
UpdateLinks:=3
Range("A1:X105").Select
Selection.Copy
ActiveWindow.Close
Sheets("Temp").Select
ActiveSheet.PasteSpecial xlPasteFormats
Application.DisplayAlerts = True
End Sub
Previous:
From:Copy conditional formatting from one cell to another using VBA?
Sub test()
Sheets("B").[B1].Copy: Sheets("A").[A1:A10].PasteSpecial xlPasteFormats
End Sub
Is a code that will paste the conditional formatting.
Mostly what you need to do is change the order:
Open Source using a Prompt for the User to select file
Copy Range from Source
Paste Range to Original
Close Source
The current active workbook is a macro-enabled one.
After performing some automated data manipulation, I want to save it as a new macro-free workbook (thus .xlsx instead of .xlsm). To do this, I recorded and edited a macro:
Dim newFilePath As string
newFileFullName = "C:\List.xlsx"
ActiveWorkbook.SaveAs Filename:=newFileFullName, FileFormat _
:=xlOpenXMLWorkbook, CreateBackup:=False
Workbooks.Open Filename:=newFileFullName
Windows("List.xlsx").Activate
However, whenever I run this macro, a window keeps popping up, asking for confirmation that I want to change the file format.
How can I suppress this pop-up and default it to "Yes"?
Add Application.DisplayAlerts = False
Thanks #simoco