Quick VBA Assist, saving to cell reference & as values.

sanantonio

Board Regular
Joined
Oct 26, 2021
Messages
124
Office Version
  1. 365
Platform
  1. Windows
Hi All,

I've a macro I'm borrowing from another macro of mine. The current macro copies a specific worksheet within the macro enabled workbook to a new workbook and saves that workbook.

VBA Code:
Sub step6()
 

Dim theNewWorkbook As Workbook
Dim currentWorkbook As Workbook

'currentWorkbook is the source workbook, create a new workbook referencing to it with theNewWorkbook
Set currentWorkbook = ActiveWorkbook
Set theNewWorkbook = Workbooks.Add

'do the copy (it's better to check if there is already a 'Worksheet 1' in the new workbook. It it exists delete it or rename it
currentWorkbook.Worksheets("Output").Copy before:=theNewWorkbook.Sheets(1)

'Remove default sheets in order to have only the copied sheet inside the new workbook
Application.DisplayAlerts = False
Dim i As Integer
For i = theNewWorkbook.Sheets.Count To 2 Step -1
    theNewWorkbook.Sheets(i).Delete
Next i
Application.DisplayAlerts = True

'Save File as XLSM
saveLocation = "C;\C_Drive\Data_for_" & Format(Date, "mmdd") & ".xlsm"
theNewWorkbook.SaveAs Filename:=saveLocation, FileFormat:=XlFileFormat.xlOpenXMLWorkbookMacroEnabled
theNewWorkbook.Close


 
End Sub

What I ideally need it to do is have the "Save location" and name reference a cell.

And then currently it copies the worksheet over like a ctrl+v and maintains all formulas etc and takes across the hidden columns. How can the code be changed so that it just copies and pastes values? So that the hidden columns and formulas don't follow it across?

Thanks in advance!
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Try this:

VBA Code:
Sub step6()
  Dim theNewWorkbook As Workbook
  Dim currentWorkbook As Workbook
  Dim sPath As String, sName As String
  
  'currentWorkbook is the source workbook, create a new workbook referencing to it with theNewWorkbook
  Set currentWorkbook = ActiveWorkbook
  With currentWorkbook.Worksheets("Output")
    sPath = .Range("A2").Value      ' Save location
    sName = .Range("A3").Value      ' name reference a cell.
    .Copy
  End With
  Set theNewWorkbook = ActiveWorkbook
  
  'Save File as XLSM
  With theNewWorkbook
    .Sheets(1).Cells.EntireColumn.Hidden = False
    .Sheets(1).Cells.Copy
    .Sheets(1).Range("A1").PasteSpecial xlValues
    .SaveAs sPath & sName & ".xlsm", XlFileFormat.xlOpenXMLWorkbookMacroEnabled
    .Close
  End With
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,215,771
Messages
6,126,799
Members
449,337
Latest member
BBV123

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