VBA "SAVE AS" EXTENSION ISSUE

Mr_Phil

Board Regular
Joined
May 28, 2018
Messages
150
Office Version
  1. 365
I have a template that I do a fast paste from the main workbook. Then I want to save the template with a unique name. But, it tosses up an error.

Code:
Sub Export()

Workbooks.Open FileName:=ThisWorkbook.Path & "\RSP PO Form Template New.xlsx"
Workbooks("DISH ORDER WORKSHEET.xlsB").Worksheets("Subcontractor PO Form").Range("H17:H500").Copy
Workbooks("RSP PO Form Template New.xlsx").Worksheets("RSP PO Form").Range("H17").PasteSpecial Paste:=xlPasteValues

  'Saves copy of workbook to filename as value of Q1
  Windows("RSP PO Form Template New.xlsx").Activate
  Dim PathName, FileName As String
  FileName = Range("Q1").Value & ".xlsx"
  If Len(FileName) > 0 Then
    PathName = ThisWorkbook.Path
    If Dir(PathName, vbDirectory) = "" Then MkDir PathName
    ThisWorkbook.SaveAs PathName & "\" & FileName
  Else
    Range("q1").Select
     MsgBox "File name in P1 is not found", vbExclamation, "Not saved": Exit Sub
  End If
  Range("F10").Select

End Sub

I thought it was fairly straightforward but I get this error and am not certain what I did wrong. I got the code from various google searches and put it together.

1663424309325.png
 

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.
Untested:
VBA Code:
Sub Export()
    Application.ScreenUpdating = False
    Dim wb As Workbook
    Set wb = ThisWorkbook
    Workbooks.Open FileName:=wb.Path & "\RSP PO Form Template New.xlsx"
    With Workbooks("RSP PO Form Template New.xlsx").Sheets("RSP PO Form")
        If .Range("Q1").Value <> "" Then
            Workbooks("DISH ORDER WORKSHEET.xlsb").Sheets("Subcontractor PO Form").Range("H17:H500").Copy
            .Range("H17").PasteSpecial Paste:=xlPasteValues
            .Copy
            ActiveWorkbook.SaveAs FileName:=wb.Path & "\" & .Range("Q1").Value & ".xlsx", FileFormat:=51
        Else
            MsgBox "File name in P1 is not found", vbExclamation, "Not saved"
        End If
    End With
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution
Thank you. That did the trick and I will look closer to learn it as opposed to simply using it. Have a great day.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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