Excel Macro to Save Sheets As xlsx

mare23

New Member
Joined
Oct 28, 2020
Messages
22
Office Version
  1. 365
Platform
  1. Windows
Hi all,

I am new to the VBA and Macro world, but I have been trying for a week to figure out a specific code. I need to create a macro that will take that Purchase order template once filled out and save it as an Xlsx file. I have code for PDF below and I need same fo xlsx. Is it possible to save excel for specific columns/rows as in the code below? And also when I click the SAVE button let it save my file without opening it


'Save Invoice as PDF

On Error Resume Next

Sheet2.Range("B2:T65").ExportAsFixedFormat xlTypePDF, Filename:= _
"C:\invoice\" & Sheet2.Range("R4").Value, Openafterpublish:=False
End Sub

thx
Mare
 

Some videos you may like

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)

JLGWhiz

Well-known Member
Joined
Feb 7, 2012
Messages
12,979
Office Version
  1. 2013
Platform
  1. Windows
See if this will do what you want.

VBA Code:
Sub t()
Dim sh As Worksheet, fPath
fPath = ThisWorkbook.Path
With ThisWorkbook
    Set sh = .Sheets.Add
    Sheet2.Range("B2:T65").Copy sh.Range("B2")
    sh.Copy
    ActiveWorkbook.SaveAs fPath & "\" & Sheet2.Range("R4").Value & ".xlsx", 51
    ActiveWorkbook.Close False
    Application.DisplayAlerts = False
    sh.Delete
    Application.DisplayAlerts = True
End With
End Sub
 

mare23

New Member
Joined
Oct 28, 2020
Messages
22
Office Version
  1. 365
Platform
  1. Windows
Great, that’s it but I want to save this xlsx in "C:\invoice\"
 

JLGWhiz

Well-known Member
Joined
Feb 7, 2012
Messages
12,979
Office Version
  1. 2013
Platform
  1. Windows
VBA Code:
fPath = ThisWorkbook.Path

Change to:

VBA Code:
fPath = "C:\Invoice"
 

mare23

New Member
Joined
Oct 28, 2020
Messages
22
Office Version
  1. 365
Platform
  1. Windows

ADVERTISEMENT

Great, thx!
 

mare23

New Member
Joined
Oct 28, 2020
Messages
22
Office Version
  1. 365
Platform
  1. Windows
One more question how to Use PasteSpecial Method to Copy Range and Paste in Another Sheet with Formatting and Column Widths. I need same size as original, without Gridlines and with 70% zoom
 

JLGWhiz

Well-known Member
Joined
Feb 7, 2012
Messages
12,979
Office Version
  1. 2013
Platform
  1. Windows

ADVERTISEMENT

One more question how to Use PasteSpecial Method to Copy Range and Paste in Another Sheet with Formatting and Column Widths. I need same size as original, without Gridlines and with 70% zoom
example:
Sheet1.Range("A2:F20").Copy
VBA Code:
With Sheet2
    .Range("A2").PasteSpecial xlPasteColumnWidths
    .Range("A2").PasteSpecial. xlPasteValuesAndNumberFormats
End With

For reference: XlPasteType enumeration (Excel)
 

mare23

New Member
Joined
Oct 28, 2020
Messages
22
Office Version
  1. 365
Platform
  1. Windows
Sorry I didn't mean another sheet in the same workbook, I mean in new workbook. I need Copy/paste same as I have in original workbook without gridlines and with same size and merge rows/columns...
 

JLGWhiz

Well-known Member
Joined
Feb 7, 2012
Messages
12,979
Office Version
  1. 2013
Platform
  1. Windows
Sorry I didn't mean another sheet in the same workbook, I mean in new workbook. I need Copy/paste same as I have in original workbook without gridlines and with same size and merge rows/columns...
It is the same principle. You just and in the workbook reference in front of the sheet reference.

VBA Code:
Workbooks(2).Sheets(2).PasteSpecial xlPasteColumnWidths
 

Watch MrExcel Video

Forum statistics

Threads
1,127,407
Messages
5,624,587
Members
416,036
Latest member
eloisa manzanarez

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
Top