Need help creating a marco please

myakymiv

New Member
Joined
Dec 20, 2018
Messages
8
Hello, I need help making a macro for my excel sheet: I need a macro that saves a document into a certain folder but when it saves the file it needs to have all the cells be saved as "Values(v)" because currently have the cells get as formulas and other macro wont run cells if they have formulas in them.

Thank you for all your help!
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Code:
Sub copyAll()


Dim Output As Workbook, Source As Workbook
Dim sh As Worksheet
Dim FileName As String
Dim firstCell


Application.ScreenUpdating = False
Set Source = ActiveWorkbook


Set Output = Workbooks.Add
Application.DisplayAlerts = False


Dim i As Integer


ThisWorkbook.Save


For Each sh In Source.Worksheets


    Dim newSheet As Worksheet


    ' select all used cells in the source sheet:
    sh.Activate
    sh.UsedRange.Select
    Application.CutCopyMode = False
    Selection.Copy


    ' create new destination sheet:
    Set newSheet = Output.Worksheets.Add(after:=Output.Worksheets(Output.Worksheets.Count))
    newSheet.Name = sh.Name


    ' make sure the destination sheet is selected with the right cell:
    newSheet.Activate
    firstCell = sh.UsedRange.Cells(1, 1).Address
    newSheet.Range(firstCell).Select


    ' paste the values:
    Range(firstCell).PasteSpecial Paste:=xlPasteColumnWidths
    Range(firstCell).PasteSpecial Paste:=xlPasteFormats
    Range(firstCell).PasteSpecial Paste:=xlPasteValues, _
    Operation:=xlNone, SkipBlanks:=True, Transpose:=False


Next


' delete the sheets that were originally there
While Output.Sheets.Count > Source.Worksheets.Count
  Output.Sheets(1).Delete
Wend
FileName = "Testing WB"
Output.SaveAs "C:\Users\My\Desktop\New folder\" & FileName  '<--- edit path as needed


Workbooks("Testing WB.xlsx").Close  '<--- edit name of workbook


'.Close SaveChanges:=True
Application.ScreenUpdating = True


End Sub
 
Upvote 0
I have a couple of questions, when I put in the file path I want the new file to be saved it just saves straight to my desktop, and also how would I make it so that the original file doesn't save but it stays as a "template"
 
Upvote 0
.
Code:
Option Explicit


Sub copyAll()


Dim Output As Workbook, Source As Workbook
Dim sh As Worksheet
Dim FileName As String
Dim firstCell


Application.ScreenUpdating = False
Set Source = ActiveWorkbook


Set Output = Workbooks.Add
Application.DisplayAlerts = False


Dim i As Integer


For Each sh In Source.Worksheets


    Dim newSheet As Worksheet


    ' select all used cells in the source sheet:
    sh.Activate
    sh.UsedRange.Select
    Application.CutCopyMode = False
    Selection.Copy


    ' create new destination sheet:
    Set newSheet = Output.Worksheets.Add(after:=Output.Worksheets(Output.Worksheets.Count))
    newSheet.Name = sh.Name


    ' make sure the destination sheet is selected with the right cell:
    newSheet.Activate
    firstCell = sh.UsedRange.Cells(1, 1).Address
    newSheet.Range(firstCell).Select


    ' paste the values:
    Range(firstCell).PasteSpecial Paste:=xlPasteColumnWidths
    Range(firstCell).PasteSpecial Paste:=xlPasteFormats
    Range(firstCell).PasteSpecial Paste:=xlPasteValues, _
    Operation:=xlNone, SkipBlanks:=True, Transpose:=False


Next


' delete the sheets that were originally there
While Output.Sheets.Count > Source.Worksheets.Count
  Output.Sheets(1).Delete
Wend
FileName = "Testing WB"
Output.SaveAs Environ("USERPROFILE") & "\Desktop\" & FileName '<--- edit path as needed


Workbooks("Testing WB.xlsx").Close  '<--- edit name of workbook


Application.DisplayAlerts = False
Application.Quit
ActiveWorkbook.Close SaveChanges:=False
Application.DisplayAlerts = True


'.Close SaveChanges:=True
Application.ScreenUpdating = True


End Sub
 
Upvote 0
.
Code:
Option Explicit


Sub copyAll()


Dim Output As Workbook, Source As Workbook
Dim sh As Worksheet
Dim FileName As String
Dim firstCell


Application.ScreenUpdating = False
Set Source = ActiveWorkbook


Set Output = Workbooks.Add
Application.DisplayAlerts = False


Dim i As Integer


For Each sh In Source.Worksheets


    Dim newSheet As Worksheet


    ' select all used cells in the source sheet:
    sh.Activate
    sh.UsedRange.Select
    Application.CutCopyMode = False
    Selection.Copy


    ' create new destination sheet:
    Set newSheet = Output.Worksheets.Add(after:=Output.Worksheets(Output.Worksheets.Count))
    newSheet.Name = sh.Name


    ' make sure the destination sheet is selected with the right cell:
    newSheet.Activate
    firstCell = sh.UsedRange.Cells(1, 1).Address
    newSheet.Range(firstCell).Select


    ' paste the values:
    Range(firstCell).PasteSpecial Paste:=xlPasteColumnWidths
    Range(firstCell).PasteSpecial Paste:=xlPasteFormats
    Range(firstCell).PasteSpecial Paste:=xlPasteValues, _
    Operation:=xlNone, SkipBlanks:=True, Transpose:=False


Next


' delete the sheets that were originally there
While Output.Sheets.Count > Source.Worksheets.Count
  Output.Sheets(1).Delete
Wend
FileName = "Testing WB"
Output.SaveAs Environ("USERPROFILE") & "\Desktop\" & FileName '<--- edit path as needed


Workbooks("Testing WB.xlsx").Close  '<--- edit name of workbook


Application.DisplayAlerts = False
Application.Quit
ActiveWorkbook.Close SaveChanges:=False
Application.DisplayAlerts = True


'.Close SaveChanges:=True
Application.ScreenUpdating = True


End Sub

I appreciate all the help, but every time I put the file patch in I get and error, this is what im typing. am I doing something wrong?

Output.SaveAs Environ("USERPROFILE") & "C:\Users\XBBL28N\Desktop\Test MSIM" & FileName '<--- edit path as needed
 
Upvote 0
.
Sorry .... delete the comment about editing the path.

Simply use : Output.SaveAs Environ("USERPROFILE") & "\Desktop" & FileName

That will place the file on your desktop.
 
Upvote 0
okay so I ran into an issue with that macro, after I run the macro, instead of just that one excel sheet closing all the excel sheets that I have open close and then I have to reopen them all, is there a way that just that one excel sheet that I need closes maybe by name or something?
 
Upvote 0
.
Either comment out or delete these lines of code, toward the bottom of the macro :

Code:
'Application.DisplayAlerts = False
'Application.Quit
'ActiveWorkbook.Close SaveChanges:=False
'Application.DisplayAlerts = True




'.Close SaveChanges:=True
 
Upvote 0

Forum statistics

Threads
1,215,154
Messages
6,123,327
Members
449,098
Latest member
thnirmitha

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